Class: Botkit::Telegram::Bot
- Inherits:
-
Bot
- Object
- Bot
- Botkit::Telegram::Bot
- Defined in:
- lib/botkit/telegram/bot.rb
Instance Attribute Summary collapse
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
-
#booted_at ⇒ Object
readonly
Returns the value of attribute booted_at.
-
#offset ⇒ Object
Returns the value of attribute offset.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(api_token:) ⇒ Bot
constructor
A new instance of Bot.
- #prepare_message(message) ⇒ Object
- #reply_message(message, reply, options = {}) ⇒ Object
- #send_message(message, options = {}) ⇒ Object
Constructor Details
#initialize(api_token:) ⇒ Bot
Returns a new instance of Bot.
9 10 11 12 13 14 |
# File 'lib/botkit/telegram/bot.rb', line 9 def initialize(api_token:) super() @api_token = api_token @booted_at = Time.now.utc.to_i @offset = 0 end |
Instance Attribute Details
#api_token ⇒ Object (readonly)
Returns the value of attribute api_token.
6 7 8 |
# File 'lib/botkit/telegram/bot.rb', line 6 def api_token @api_token end |
#booted_at ⇒ Object (readonly)
Returns the value of attribute booted_at.
6 7 8 |
# File 'lib/botkit/telegram/bot.rb', line 6 def booted_at @booted_at end |
#offset ⇒ Object
Returns the value of attribute offset.
7 8 9 |
# File 'lib/botkit/telegram/bot.rb', line 7 def offset @offset end |
Instance Method Details
#call ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/botkit/telegram/bot.rb', line 16 def call context = self response = Aitch.post do url "https://api.telegram.org/bot#{context.api_token}/getUpdates" params offset: context.offset, allowed_updates: "message" expect: 200 end = response.data["result"] return if .empty? self.offset = .last["update_id"] + 1 .reject {|| .key?("edited_message") } .select {|| .dig("message", "date") >= booted_at } .map(&method(:prepare_message)) .each(&method(:handle_incoming_message)) end |
#prepare_message(message) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/botkit/telegram/bot.rb', line 37 def () params = (.dig("message", "text")) params = params.merge(raw: , channel_id: .dig("message", "chat", "id"), id: .dig("message", "message_id")) Message.new(**params) end |
#reply_message(message, reply, options = {}) ⇒ Object
60 61 62 |
# File 'lib/botkit/telegram/bot.rb', line 60 def (, reply, = {}) (reply, .merge(reply_to_message_id: .id)) end |
#send_message(message, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/botkit/telegram/bot.rb', line 45 def (, = {}) context = self = { text: .text, chat_id: .channel_id }.merge() Aitch.post do url "https://api.telegram.org/bot#{context.api_token}/sendMessage" params expect: 200 end end |