Class: MTProto::Client
- Inherits:
-
Object
- Object
- MTProto::Client
- Defined in:
- lib/mtproto/client.rb,
lib/mtproto/client/rpc.rb
Defined Under Namespace
Classes: RPC
Instance Attribute Summary collapse
-
#access_hash ⇒ Object
readonly
Returns the value of attribute access_hash.
-
#api_hash ⇒ Object
Returns the value of attribute api_hash.
-
#api_id ⇒ Object
Returns the value of attribute api_id.
-
#app_version ⇒ Object
Returns the value of attribute app_version.
-
#auth_key ⇒ Object
readonly
Returns the value of attribute auth_key.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#dc_number ⇒ Object
readonly
Returns the value of attribute dc_number.
-
#device_model ⇒ Object
Returns the value of attribute device_model.
-
#lang_code ⇒ Object
Returns the value of attribute lang_code.
-
#lang_pack ⇒ Object
Returns the value of attribute lang_pack.
-
#server_key ⇒ Object
readonly
Returns the value of attribute server_key.
-
#server_salt ⇒ Object
readonly
Returns the value of attribute server_salt.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#system_lang_code ⇒ Object
Returns the value of attribute system_lang_code.
-
#system_version ⇒ Object
Returns the value of attribute system_version.
-
#time_offset ⇒ Object
readonly
Returns the value of attribute time_offset.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
- #call(body, content_related: true) ⇒ Object
- #connect! ⇒ Object
- #connected? ⇒ Boolean
- #disconnect! ⇒ Object
- #exchange_keys! ⇒ Object
- #init_connection! ⇒ Object
-
#initialize(api_id: nil, api_hash: nil, host:, port: 443, public_key: nil, dc_number: nil, test_mode: false, timeout: 10) ⇒ Client
constructor
A new instance of Client.
- #load_auth_data(auth_data) ⇒ Object
- #rpc ⇒ Object
- #save_auth_data ⇒ Object
- #update_user(user_id:, access_hash: nil) ⇒ Object
Constructor Details
#initialize(api_id: nil, api_hash: nil, host:, port: 443, public_key: nil, dc_number: nil, test_mode: false, timeout: 10) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mtproto/client.rb', line 21 def initialize(api_id: nil, api_hash: nil, host:, port: 443, public_key: nil, dc_number: nil, test_mode: false, timeout: 10) raise ArgumentError, "host is required" if host.nil? || host.empty? raise ArgumentError, "dc_number must be positive. Use test_mode: true for test DCs" if dc_number && dc_number < 0 codec = Transport::AbridgedPacketCodec.new @connection = Transport::TCPConnection.new(host, port, codec) @public_key = public_key @dc_number = dc_number @test_mode = test_mode @timeout = timeout @server_key = nil @auth_key = nil @server_salt = nil @time_offset = 0 @session = nil @connection_initialized = false @user_id = nil @access_hash = nil # Client configuration defaults @api_id = api_id || 0 @api_hash = api_hash || '' @device_model = 'Ruby MTProto' @system_version = RUBY_DESCRIPTION @app_version = '0.1.0' @system_lang_code = 'en' @lang_pack = '' @lang_code = 'en' end |
Instance Attribute Details
#access_hash ⇒ Object (readonly)
Returns the value of attribute access_hash.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def access_hash @access_hash end |
#api_hash ⇒ Object
Returns the value of attribute api_hash.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def api_hash @api_hash end |
#api_id ⇒ Object
Returns the value of attribute api_id.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def api_id @api_id end |
#app_version ⇒ Object
Returns the value of attribute app_version.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def app_version @app_version end |
#auth_key ⇒ Object (readonly)
Returns the value of attribute auth_key.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def auth_key @auth_key end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def connection @connection end |
#dc_number ⇒ Object (readonly)
Returns the value of attribute dc_number.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def dc_number @dc_number end |
#device_model ⇒ Object
Returns the value of attribute device_model.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def device_model @device_model end |
#lang_code ⇒ Object
Returns the value of attribute lang_code.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def lang_code @lang_code end |
#lang_pack ⇒ Object
Returns the value of attribute lang_pack.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def lang_pack @lang_pack end |
#server_key ⇒ Object (readonly)
Returns the value of attribute server_key.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def server_key @server_key end |
#server_salt ⇒ Object (readonly)
Returns the value of attribute server_salt.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def server_salt @server_salt end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def session @session end |
#system_lang_code ⇒ Object
Returns the value of attribute system_lang_code.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def system_lang_code @system_lang_code end |
#system_version ⇒ Object
Returns the value of attribute system_version.
19 20 21 |
# File 'lib/mtproto/client.rb', line 19 def system_version @system_version end |
#time_offset ⇒ Object (readonly)
Returns the value of attribute time_offset.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def time_offset @time_offset end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def timeout @timeout end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
18 19 20 |
# File 'lib/mtproto/client.rb', line 18 def user_id @user_id end |
Instance Method Details
#call(body, content_related: true) ⇒ Object
81 82 83 |
# File 'lib/mtproto/client.rb', line 81 def call(body, content_related: true) rpc.call(body, content_related: ) end |
#connect! ⇒ Object
51 52 53 |
# File 'lib/mtproto/client.rb', line 51 def connect! @connection.connect! end |
#connected? ⇒ Boolean
55 56 57 |
# File 'lib/mtproto/client.rb', line 55 def connected? @connection&.connected? end |
#disconnect! ⇒ Object
59 60 61 |
# File 'lib/mtproto/client.rb', line 59 def disconnect! @connection.close if @connection end |
#exchange_keys! ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mtproto/client.rb', line 63 def exchange_keys! raise ArgumentError, "public_key is required for auth key generation" if @public_key.nil? || @public_key.empty? generator = AuthKeyGenerator.new(@connection, @public_key, @dc_number, test_mode: @test_mode, timeout: @timeout) result = generator.generate @auth_key = generator.auth_key @server_salt = generator.server_salt @time_offset = generator.time_offset @session = Session.new result end |
#init_connection! ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/mtproto/client.rb', line 85 def init_connection! return if @connection_initialized query = Type::RPC::InvokeWithLayer.build( layer: 214, query: Type::RPC::InitConnection.build( api_id: api_id, device_model: device_model, system_version: system_version, app_version: app_version, system_lang_code: system_lang_code, lang_pack: lang_pack, lang_code: lang_code, query: Type::RPC::Help::GetConfig.build ) ) response = rpc.call_sync(query) config = Type::RPC::Help::Config.parse(response) @connection_initialized = true config end |
#load_auth_data(auth_data) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mtproto/client.rb', line 123 def load_auth_data(auth_data) raise ArgumentError, 'auth_data must be a Hash' unless auth_data.is_a?(Hash) raise ArgumentError, 'auth_key is required' unless auth_data[:auth_key] raise ArgumentError, 'server_salt is required' unless auth_data[:server_salt] @auth_key = Base64.strict_decode64(auth_data[:auth_key]) @server_salt = auth_data[:server_salt] @time_offset = auth_data[:time_offset] || 0 @user_id = auth_data[:user_id] @access_hash = auth_data[:access_hash] @session = Session.new true end |
#save_auth_data ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mtproto/client.rb', line 110 def save_auth_data raise 'Cannot save auth_data: auth_key not set' unless @auth_key { auth_key: Base64.strict_encode64(@auth_key), server_salt: @server_salt, user_id: @user_id, access_hash: @access_hash, dc_number: @dc_number, time_offset: @time_offset } end |
#update_user(user_id:, access_hash: nil) ⇒ Object
139 140 141 142 |
# File 'lib/mtproto/client.rb', line 139 def update_user(user_id:, access_hash: nil) @user_id = user_id @access_hash = access_hash end |