Class: TD::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tdlib/client.rb

Overview

Simple client for TDLib.

Examples:

TD.configure do |config|
  config.lib_path = 'path_to_tdlibjson'
  config.encryption_key = 'your_encryption_key'

  config.client.api_id = your_api_id
  config.client.api_hash = 'your_api_hash'
end

client = TD::Client.new

begin
  state = nil

  client.on('updateAuthorizationState') do |update|
    next unless update.dig('authorization_state', '@type') == 'authorizationStateWaitPhoneNumber'
    state = :wait_phone
  end

  client.on('updateAuthorizationState') do |update|
    next unless update.dig('authorization_state', '@type') == 'authorizationStateWaitCode'
    state = :wait_code
  end

  client.on('updateAuthorizationState') do |update|
    next unless update.dig('authorization_state', '@type') == 'authorizationStateReady'
    state = :ready
  end

  loop do
    case state
    when :wait_phone
      p 'Please, enter your phone number:'
      phone = STDIN.gets.strip
      params = {
        '@type' => 'setAuthenticationPhoneNumber',
        'phone_number' => phone
      }
      client.broadcast_and_receive(params)
    when :wait_code
      p 'Please, enter code from SMS:'
      code = STDIN.gets.strip
      params = {
        '@type' => 'checkAuthenticationCode',
        'code' => code
      }
      client.broadcast_and_receive(params)
    when :ready
      @me = client.broadcast_and_receive('@type' => 'getMe')
      break
    end
  end

ensure
  client.close
end

p @me

Constant Summary collapse

TIMEOUT =
10

Instance Method Summary collapse

Constructor Details

#initialize(td_client = TD::Api.client_create, update_manager = TD::UpdateManager.new(td_client), **extra_config) ⇒ Client

Returns a new instance of Client.



63
64
65
66
67
68
69
70
# File 'lib/tdlib/client.rb', line 63

def initialize(td_client = TD::Api.client_create,
               update_manager = TD::UpdateManager.new(td_client),
               **extra_config)
  @td_client = td_client
  @update_manager = update_manager
  @config = TD.config.client.to_h.merge(extra_config)
  authorize
end

Instance Method Details

#authorization_stateHash

Returns current authorization state (it's offline request)

Returns:

  • (Hash)


118
119
120
# File 'lib/tdlib/client.rb', line 118

def authorization_state
  broadcast_and_receive('@type' => 'getAuthorizationState')
end

#broadcast(query) {|update| ... } ⇒ Object

Sends asynchronous request to the TDLib client

Parameters:

  • query (Hash)

Yields:

  • (update)

    yields update to the block as soon as it's received



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tdlib/client.rb', line 75

def broadcast(query)
  if block_given?
    extra = TD::Utils.generate_extra(query)
    handler = ->(update) do
      return unless update['@extra'] == extra
      yield update
      @update_manager.remove_handler(handler)
    end
    @update_manager.add_handler(handler)
    query['@extra'] = extra
  end
  TD::Api.client_send(@td_client, query)
end

#broadcast_and_receive(query) ⇒ Hash

Sends asynchronous request to the TDLib client and returns received update synchronously

Parameters:

  • query (Hash)

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tdlib/client.rb', line 92

def broadcast_and_receive(query)
  result = nil
  extra = TD::Utils.generate_extra(query)
  handler = ->(update) { result = update if update['@extra'] == extra }
  @update_manager.add_handler(handler)
  query['@extra'] = extra
  TD::Api.client_send(@td_client, query)
  Timeout.timeout(TIMEOUT) do
    loop do
      if result
        @update_manager.remove_handler(handler)
        return result
      end
    end
  end
end

#closeObject

Stops update manager and destroys TDLib client



134
135
136
137
# File 'lib/tdlib/client.rb', line 134

def close
  @update_manager.stop
  TD::Api.client_destroy(@td_client)
end

#execute(query) ⇒ Object

Synchronously executes TDLib request Only a few requests can be executed synchronously

Parameters:

  • query (Hash)


112
113
114
# File 'lib/tdlib/client.rb', line 112

def execute(query)
  TD::Api.client_execute(@td_client, query)
end

#on(update_type) {|update| ... } ⇒ Object

Binds passed block as a handler for updates with type of update_type

Parameters:

  • update_type (String)

Yields:

  • (update)

    yields update to the block as soon as it's received



125
126
127
128
129
130
131
# File 'lib/tdlib/client.rb', line 125

def on(update_type, &_)
  handler = ->(update) do
    return unless update['@type'] == update_type
    yield update
  end
  @update_manager.add_handler(handler)
end