Add ruby example (#83)

This commit is contained in:
Vladislav Yashin 2018-03-19 23:01:29 +03:00 committed by Aliaksei Levin
parent 4753979481
commit 20f2088690
4 changed files with 82 additions and 1 deletions

View File

@ -145,7 +145,7 @@ But for most use cases we suggest to use the JSON interface, which can be easily
See [td_json_client](https://core.telegram.org/tdlib/docs/td__json__client_8h.html) and [td_log](https://core.telegram.org/tdlib/docs/td__log_8h.html) documentation for detailed JSON interface description,
scheme [td_api.tl](https://github.com/tdlib/td/blob/master/td/generate/scheme/td_api.tl) or autogenerated [HTML documentation](https://core.telegram.org/tdlib/docs/td__api_8h.html) for the list of all available `TDLib` methods and classes.
See [example/python/tdjson_example.py](https://github.com/tdlib/td/tree/master/example/python/tdjson_example.py) for an example of its usage.
See [example/python/tdjson_example.py](https://github.com/tdlib/td/tree/master/example/python/tdjson_example.py) and [example/ruby/example.rb](https://github.com/tdlib/td/tree/master/example/ruby/example.rb) for an example of such usage.
<a name="license"></a>
## License

3
example/ruby/Gemfile Normal file
View File

@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'tdlib-ruby'

17
example/ruby/Gemfile.lock Normal file
View File

@ -0,0 +1,17 @@
GEM
remote: https://rubygems.org/
specs:
concurrent-ruby (1.0.5)
dry-configurable (0.7.0)
concurrent-ruby (~> 1.0)
tdlib-ruby (0.2.0)
dry-configurable (~> 0.7)
PLATFORMS
ruby
DEPENDENCIES
tdlib-ruby
BUNDLED WITH
1.16.1

61
example/ruby/example.rb Normal file
View File

@ -0,0 +1,61 @@
require 'tdlib-ruby'
TD.configure do |config|
config.lib_path = 'path/to/dir_containing_lobtdjson'
# You should obtain your own api_id and api_hash from https://my.telegram.org/apps
config.client.api_id = 12345
config.client.api_hash = '1234567890abcdefghigklmnopqrstuv'
end
TD::Api.set_log_verbosity_level(1)
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