Class: Telegram::Bot::Client
- Inherits:
-
Object
- Object
- Telegram::Bot::Client
- Defined in:
- lib/telegram/bot/client.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_updates ⇒ Object
-
#initialize(token, h = {}) ⇒ Client
constructor
A new instance of Client.
- #listen(&block) ⇒ Object
-
#listen_for(minutes = 15, &block) ⇒ Object
Listen for a given period of time (in minutes).
- #run {|_self| ... } ⇒ Object
-
#stop ⇒ Object
This can also be written in any calling code (the throw does not have to appear within the static scope of the catch), but is included here for completeness.
Constructor Details
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
4 5 6 |
# File 'lib/telegram/bot/client.rb', line 4 def api @api end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/telegram/bot/client.rb', line 5 def logger @logger end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/telegram/bot/client.rb', line 4 def @options end |
Class Method Details
.run(*args, &block) ⇒ Object
7 8 9 |
# File 'lib/telegram/bot/client.rb', line 7 def self.run(*args, &block) new(*args).run(&block) end |
Instance Method Details
#fetch_updates ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/telegram/bot/client.rb', line 48 def fetch_updates response = api.getUpdates() return unless response['ok'] response['result'].each do |data| update = Types::Update.new(data) @options[:offset] = update.update_id.next = update. () yield end rescue Faraday::Error::TimeoutError retry end |
#listen(&block) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/telegram/bot/client.rb', line 21 def listen(&block) logger.info('Starting bot') catch(:stop) { loop { fetch_updates(&block) } } end |
#listen_for(minutes = 15, &block) ⇒ Object
Listen for a given period of time (in minutes)
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/telegram/bot/client.rb', line 35 def listen_for(minutes = 15, &block) counter = 15 * 60 interval = 5 # Check every 5 seconds interval_timer = 1 # must start at 1 now = Time.now while Time.now - now < counter if interval_timer % interval == 0 #Every 5 attempts the activity will process fetch_updates(&block) end interval_timer = interval_timer + 1 end end |
#run {|_self| ... } ⇒ Object
17 18 19 |
# File 'lib/telegram/bot/client.rb', line 17 def run yield self end |
#stop ⇒ Object
This can also be written in any calling code (the throw does not have to appear within the static scope of the catch), but is included here for completeness
30 31 32 |
# File 'lib/telegram/bot/client.rb', line 30 def stop throw :stop end |