Class: MTProto::Client::API

Inherits:
Object
  • Object
show all
Defined in:
lib/mtproto/client/api.rb,
lib/mtproto/client/api/sign_in.rb,
lib/mtproto/client/api/get_users.rb,
lib/mtproto/client/api/send_code.rb,
lib/mtproto/client/api/get_dialogs.rb,
lib/mtproto/client/api/get_history.rb,
lib/mtproto/client/api/get_contacts.rb,
lib/mtproto/client/api/send_message.rb,
lib/mtproto/client/api/check_password.rb,
lib/mtproto/client/api/get_updates_state.rb,
lib/mtproto/client/api/export_login_token.rb,
lib/mtproto/client/api/import_login_token.rb,
lib/mtproto/client/api/get_updates_difference.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ API

Returns a new instance of API.



19
20
21
# File 'lib/mtproto/client/api.rb', line 19

def initialize(client)
  @client = client
end

Instance Method Details

#check_password(password:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mtproto/client/api/check_password.rb', line 12

def check_password(password:)
  pwd = rpc_call(TL::GetPassword.new, TL::AccountPassword).body

  raise 'Account does not have 2FA enabled' unless pwd.has_password

  srp_result = Crypto::SRP.compute_check(
    algo: pwd.current_algo,
    srp_b: pwd.srp_b,
    srp_id: pwd.srp_id,
    password: password
  )

  result = rpc_call(
    TL::CheckPassword.new(
      srp_id: srp_result[:srp_id],
      a: srp_result[:a],
      m1: srp_result[:m1]
    ),
    TL::Authorization
  ).body

  if result.authorization? && result.user_id
    @client.update_user(user_id: result.user_id, access_hash: result.access_hash)
  end

  result
end

#export_login_token(except_ids: []) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mtproto/client/api/export_login_token.rb', line 9

def (except_ids: [])
  result = rpc_call(
    TL::ExportLoginToken.new(
      api_id: @client.api_id,
      api_hash: @client.api_hash,
      except_ids: except_ids
    ),
    TL::LoginToken
  ).body

  if result.success? && result.user_id
    @client.update_user(user_id: result.user_id, access_hash: result.access_hash)
  end

  result
end

#get_contacts(hash: 0) ⇒ Object



9
10
11
# File 'lib/mtproto/client/api/get_contacts.rb', line 9

def get_contacts(hash: 0)
  rpc_call(TL::GetContacts.new(hash: hash), TL::Contacts).body
end

#get_dialogs(offset_date: 0, offset_id: 0, offset_peer: nil, limit: 100, exclude_pinned: false, folder_id: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mtproto/client/api/get_dialogs.rb', line 9

def get_dialogs(offset_date: 0, offset_id: 0, offset_peer: nil, limit: 100, exclude_pinned: false, folder_id: nil)
  rpc_call(
    TL::GetDialogs.new(
      offset_date: offset_date, offset_id: offset_id,
      offset_peer: offset_peer, limit: limit,
      exclude_pinned: exclude_pinned, folder_id: folder_id
    ),
    TL::Dialogs
  ).body
end

#get_history(peer:, offset_id: 0, offset_date: 0, add_offset: 0, limit: 100, max_id: 0, min_id: 0) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/mtproto/client/api/get_history.rb', line 9

def get_history(peer:, offset_id: 0, offset_date: 0, add_offset: 0, limit: 100, max_id: 0, min_id: 0)
  rpc_call(
    TL::GetHistory.new(
      peer: peer, offset_id: offset_id, offset_date: offset_date,
      add_offset: add_offset, limit: limit, max_id: max_id, min_id: min_id
    ),
    TL::Messages
  ).body
end

#get_updates_difference(pts:, date:, qts:, pts_limit: nil, qts_limit: nil, pts_total_limit: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mtproto/client/api/get_updates_difference.rb', line 9

def get_updates_difference(pts:, date:, qts:, pts_limit: nil, qts_limit: nil, pts_total_limit: nil)
  rpc_call(
    TL::GetDifference.new(
      pts: pts, date: date, qts: qts,
      pts_limit: pts_limit, qts_limit: qts_limit,
      pts_total_limit: pts_total_limit
    ),
    TL::UpdatesDifference
  ).body
end

#get_updates_stateObject



9
10
11
# File 'lib/mtproto/client/api/get_updates_state.rb', line 9

def get_updates_state
  rpc_call(TL::GetState.new, TL::UpdatesState).body
end

#get_usersObject



9
10
11
# File 'lib/mtproto/client/api/get_users.rb', line 9

def get_users
  rpc_call(TL::GetUsers.new, TL::Users).body
end

#import_login_token(token:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mtproto/client/api/import_login_token.rb', line 9

def (token:)
  result = rpc_call(
    TL::ImportLoginToken.new(token: token),
    TL::LoginToken
  ).body

  if result.success? && result.user_id
    @client.update_user(user_id: result.user_id, access_hash: result.access_hash)
  end

  result
end

#send_code(phone_number) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/mtproto/client/api/send_code.rb', line 9

def send_code(phone_number)
  rpc_call(
    TL::SendCode.new(
      phone_number: phone_number,
      api_id: @client.api_id,
      api_hash: @client.api_hash
    ),
    TL::SentCode
  ).body
end

#send_message(peer:, message:, random_id: nil) ⇒ Object



9
10
11
12
# File 'lib/mtproto/client/api/send_message.rb', line 9

def send_message(peer:, message:, random_id: nil)
  rpc_call(TL::SendMessage.new(peer: peer, message: message, random_id: random_id),
           TL::UpdateShortSentMessage).body
end

#sign_in(phone_number:, phone_code_hash:, phone_code:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mtproto/client/api/sign_in.rb', line 9

def (phone_number:, phone_code_hash:, phone_code:)
  result = rpc_call(
    TL::SignIn.new(
      phone_number: phone_number,
      phone_code_hash: phone_code_hash,
      phone_code: phone_code
    ),
    TL::Authorization
  ).body

  if result.authorization? && result.user_id
    @client.update_user(user_id: result.user_id, access_hash: result.access_hash)
  end

  result
end