Class: MTProto::Client::API
- Inherits:
-
Object
- Object
- MTProto::Client::API
- 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/export_authorization.rb,
lib/mtproto/client/api/import_authorization.rb,
lib/mtproto/client/api/get_channel_difference.rb,
lib/mtproto/client/api/get_updates_difference.rb,
lib/mtproto/client/api/get_bot_callback_answer.rb,
lib/mtproto/client/api/import_bot_authorization.rb
Instance Method Summary collapse
- #check_password(password:) ⇒ Object
- #export_authorization(dc_id:) ⇒ Object
- #export_login_token(except_ids: []) ⇒ Object
-
#get_bot_callback_answer(peer:, msg_id:, data: nil) ⇒ Object
messages.getBotCallbackAnswer — press the inline callback button carrying
dataon messagemsg_idinpeer. -
#get_channel_difference(channel:, pts:, limit: 100, force: false) ⇒ Object
updates.getChannelDifference — pull a channel/supergroup's backlog since
pts(seed it from the dialog's pts). - #get_contacts(hash: 0) ⇒ Object
- #get_dialogs(offset_date: 0, offset_id: 0, offset_peer: nil, limit: 100, exclude_pinned: false, folder_id: nil) ⇒ Object
- #get_history(peer:, offset_id: 0, offset_date: 0, add_offset: 0, limit: 100, max_id: 0, min_id: 0) ⇒ Object
- #get_updates_difference(pts:, date:, qts:, pts_limit: nil, qts_limit: nil, pts_total_limit: nil) ⇒ Object
- #get_updates_state ⇒ Object
- #get_users ⇒ Object
- #import_authorization(id:, bytes:) ⇒ Object
- #import_bot_authorization(bot_auth_token) ⇒ Object
- #import_login_token(token:) ⇒ Object
-
#initialize(client) ⇒ API
constructor
A new instance of API.
- #send_code(phone_number) ⇒ Object
-
#send_message(peer:, message: '', random_id: nil, reply_to: nil, entities: nil, parse_mode: nil, rich_markdown: nil, quote_text: nil, quote_offset: nil) ⇒ Object
Send a text message.
- #sign_in(phone_number:, phone_code_hash:, phone_code:) ⇒ Object
Constructor Details
#initialize(client) ⇒ API
Returns a new instance of API.
24 25 26 |
# File 'lib/mtproto/client/api.rb', line 24 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. && result.user_id @client.update_user(user_id: result.user_id, access_hash: result.access_hash) end result end |
#export_authorization(dc_id:) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/mtproto/client/api/export_authorization.rb', line 9 def (dc_id:) rpc_call( TL::ExportAuthorization.new(dc_id: dc_id), TL::ExportedAuthorization ).body 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 export_login_token(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_bot_callback_answer(peer:, msg_id:, data: nil) ⇒ Object
messages.getBotCallbackAnswer — press the inline callback button carrying
data on message msg_id in peer. The server delivers an
updateBotCallbackQuery to the bot and blocks until the bot answers (or the
~5s timeout), then returns the bot's botCallbackAnswer. This is the client
half of "bots manage bots": the caller drives another bot's inline keyboard.
14 15 16 17 18 19 |
# File 'lib/mtproto/client/api/get_bot_callback_answer.rb', line 14 def get_bot_callback_answer(peer:, msg_id:, data: nil) rpc_call( TL::GetBotCallbackAnswer.new(peer: peer, msg_id: msg_id, data: data), TL::BotCallbackAnswer ).body end |
#get_channel_difference(channel:, pts:, limit: 100, force: false) ⇒ Object
updates.getChannelDifference — pull a channel/supergroup's backlog since
pts (seed it from the dialog's pts). Returns a parsed ChannelDifference.
force re-fetches even without a pending gap.
12 13 14 15 16 17 18 19 |
# File 'lib/mtproto/client/api/get_channel_difference.rb', line 12 def get_channel_difference(channel:, pts:, limit: 100, force: false) rpc_call( TL::GetChannelDifference.new( channel: channel, pts: pts, limit: limit, force: force ), TL::ChannelDifference ).body 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_state ⇒ Object
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_users ⇒ Object
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_authorization(id:, bytes:) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/mtproto/client/api/import_authorization.rb', line 9 def (id:, bytes:) result = rpc_call( TL::ImportAuthorization.new(id: id, bytes: bytes), TL::Authorization ).body if result. && result.user_id @client.update_user(user_id: result.user_id, access_hash: result.access_hash) end result end |
#import_bot_authorization(bot_auth_token) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mtproto/client/api/import_bot_authorization.rb', line 9 def (bot_auth_token) result = rpc_call( TL::ImportBotAuthorization.new( api_id: @client.api_id, api_hash: @client.api_hash, bot_auth_token: bot_auth_token ), TL::Authorization ).body if result. && result.user_id @client.update_user(user_id: result.user_id, access_hash: result.access_hash) end result 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 import_login_token(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, reply_to: nil, entities: nil, parse_mode: nil, rich_markdown: nil, quote_text: nil, quote_offset: nil) ⇒ Object
Send a text message. Formatting can be supplied three ways:
- `entities:` — explicit formatting spans;
- `parse_mode: :markdown` — parse Markdown out of `message` on the client
(the plain text + derived entities are sent);
- `rich_markdown:` — Telegram's server-side "Rich Formatting": the raw
Markdown string is sent as inputRichMessageMarkdown and the SERVER formats
it (`message`/`entities` are ignored). Note: server-gated (premium).
A reply_to (with optional quote_text/quote_offset) replies to a message,
optionally pinning the quoted fragment.
19 20 21 22 23 24 25 26 27 |
# File 'lib/mtproto/client/api/send_message.rb', line 19 def (peer:, message: '', random_id: nil, reply_to: nil, entities: nil, parse_mode: nil, rich_markdown: nil, quote_text: nil, quote_offset: nil) , entities = Markdown.parse() if parse_mode && entities.nil? && rich_markdown.nil? rpc_call( TL::SendMessage.new(peer: peer, message: , random_id: random_id, reply_to: reply_to, entities: entities, rich_markdown: rich_markdown, quote_text: quote_text, quote_offset: quote_offset), 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 sign_in(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. && result.user_id @client.update_user(user_id: result.user_id, access_hash: result.access_hash) end result end |