module Dialog::TDLib

Public Class Methods

bot_auth(tgSess) click to toggle source
# File lib/tdlib/auth.rb, line 41
def bot_auth(tgSess)
  state = nil
  
  tgSess.on('updateAuthorizationState') do |update|
    next unless update.dig('authorization_state', '@type') == 'authorizationStateWaitPhoneNumber'
    state = :wait_token
  end  
  
  tgSess.on('updateAuthorizationState') do |update|
    next unless update.dig('authorization_state', '@type') == 'authorizationStateReady'
    state = :ready
  end
  
  loop do
    case state
    when :wait_token
      Dialog.logger.info 'Please enter bot token:'
      token = STDIN.gets.strip      
      params = {
        '@type' => 'checkAuthenticationBotToken',
        'token' => token
      }
      tgSess.broadcast_and_receive(params).tap(&error_handler)
    
    when :ready
      @me = tgSess.broadcast_and_receive('@type' => 'getMe')
      break    

    end 
  end
  close(tgSess)
end
close(tgSess) click to toggle source
# File lib/tdlib/func.rb, line 26
def close(tgSess)
#   ensure
    tgSess.close
#   end
end
error_handler() click to toggle source
# File lib/tdlib/func.rb, line 16
def error_handler
  proc do |result|
    if result['@type'] == 'error'
      msg = "Failed to process request: #{result['code']} #{result['message']}"
      p msg
      raise msg
    end
  end
end
proxy() click to toggle source
# File lib/tdlib/auth.rb, line 26
def proxy
  proxy = {
    '@type' => 'addProxy',
    'server'   => Dialog.config.proxy.host,
    'port'     => Dialog.config.proxy.port,
    'enable'   => true,
    'type' => {
      '@type'    => 'proxyTypeSocks5', 
      'username' => Dialog.config.proxy.login,
      'password' => Dialog.config.proxy.pass
    }
  }    
  return proxy
end
tdlibAuth(mode, tgSess) click to toggle source
# File lib/tdlib/auth.rb, line 16
def tdlibAuth(mode, tgSess)
  case mode
  when 'bot'
    bot_auth(tgSess)
  when 'user'
    user_auth(tgSess)
  end
end
tdlibInit() click to toggle source
# File lib/tdlib/init.rb, line 16
def tdlibInit
  name = Dialog.config.naming.instance
  FileUtils.mkdir_p("#{Dialog.config.path.dialogData}/tdlib/#{name}")        
  config = {
      api_id: Dialog.config.tdlib.id,
      api_hash: Dialog.config.tdlib.hash_s,
      device_model: Dialog.config.naming.app,
      database_directory: "#{Dialog.config.path.dialogData}/tdlib/#{name}",
      files_directory:    "#{Dialog.config.path.dialogData}/tdlib/#{name}",
    }
          
  TD.configure do |config|
    config.lib_path = '/usr/lib64'
  end
  
  TD::Api.set_log_verbosity_level(Dialog.config.log.tdlib.to_i)  
  handler = TD::Client.new(**config)
  
  return handler
end
user_auth(tgSess) click to toggle source
# File lib/tdlib/auth.rb, line 76
def user_auth(tgSess)
  state = nil
  
  tgSess.broadcast(proxy)  

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

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

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

  loop do
    p state
    case state
    when :wait_phone
      Dialog.logger.error 'Please enter phone number:'
      phone = STDIN.gets.strip
      params = {
        '@type' => 'setAuthenticationPhoneNumber',
        'phone_number' => phone
      }
      tgSess.broadcast_and_receive(params).tap(&error_handler)
      
    when :wait_code
      Dialog.logger.error 'Please enter code from SMS:'
      code = STDIN.gets.strip
      params = {
        '@type' => 'checkAuthenticationCode',
        'code' => code
      }
      tgSess.broadcast_and_receive(params).tap(&error_handler)
      
    when :wait_password
      Dialog.logger.error 'Please enter password:'
      password = STDIN.gets.strip
      params = {
        '@type' => 'checkAuthenticationPassword',
        'password' => password
      }
      tgSess.broadcast_and_receive(params).tap(&error_handler) 
    when :ready
      @me = tgSess.broadcast_and_receive('@type' => 'getMe')
      Dialog.logger.error 'TDLib cli init success!'          
      break
    end
  end

  close(tgSess)
end