Skip to content

Commit

Permalink
Add ruby example (tdlib#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladislav-yashin authored and levlam committed Mar 19, 2018
1 parent 4753979 commit 20f2088
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions example/ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'tdlib-ruby'
17 changes: 17 additions & 0 deletions example/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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 changes: 61 additions & 0 deletions example/ruby/example.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 20f2088

Please sign in to comment.