Module: Dialog::TDLib

Defined in:
lib/tdlib/auth.rb,
lib/tdlib/func.rb,
lib/tdlib/init.rb

Class Method Summary collapse

Class Method Details

.bot_auth(tgSess) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



26
27
28
29
30
# File 'lib/tdlib/func.rb', line 26

def close(tgSess)
#   ensure
    tgSess.close
#   end
end

.error_handlerObject



16
17
18
19
20
21
22
23
24
# 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

.proxyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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.,
      'password' => Dialog.config.proxy.pass
    }
  }    
  return proxy
end

.tdlibAuth(mode, tgSess) ⇒ Object



16
17
18
19
20
21
22
23
# 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

.tdlibInitObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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