From 20f2088690eaebb049bbf0cc60aff8c2368ff926 Mon Sep 17 00:00:00 2001 From: Vladislav Yashin Date: Mon, 19 Mar 2018 23:01:29 +0300 Subject: [PATCH] Add ruby example (#83) --- README.md | 2 +- example/ruby/Gemfile | 3 ++ example/ruby/Gemfile.lock | 17 +++++++++++ example/ruby/example.rb | 61 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 example/ruby/Gemfile create mode 100644 example/ruby/Gemfile.lock create mode 100644 example/ruby/example.rb diff --git a/README.md b/README.md index 9e9a6529a..00e94cabf 100644 --- a/README.md +++ b/README.md @@ -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. ## License diff --git a/example/ruby/Gemfile b/example/ruby/Gemfile new file mode 100644 index 000000000..3a38ffc0a --- /dev/null +++ b/example/ruby/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'tdlib-ruby' diff --git a/example/ruby/Gemfile.lock b/example/ruby/Gemfile.lock new file mode 100644 index 000000000..22954eddf --- /dev/null +++ b/example/ruby/Gemfile.lock @@ -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 diff --git a/example/ruby/example.rb b/example/ruby/example.rb new file mode 100644 index 000000000..4b29dfd53 --- /dev/null +++ b/example/ruby/example.rb @@ -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