class Telegram::Bot::UpdatesController
Base class to create update processors. With callbacks, session and helpers.
Public methods ending with `!` handle messages with commands. Message text is automatically parsed into method arguments. Be sure to use default values and splat arguments in every action method to not get errors, when user sends command without necessary args / with extra args.
def start!(token = nil, *) if token # ... else # ... end end def help!(*) respond_with :message, text: end
To process plain text messages (without commands) or other updates just define public method with name of payload type. By default they receive payload as an argument, but some of them are called with more usefuk args:
def message(message) respond_with :message, text: "Echo: #{message['text']}" end def inline_query(query, offset) answer_inline_query results_for_query(query, offset), is_personal: true end
To process update run:
ControllerClass.dispatch(bot, update)
There is also ability to run action without update:
ControllerClass.new(bot, from: telegram_user, chat: telegram_chat). process(:help, *args)
Constants
- PAYLOAD_TYPES
Public Class Methods
Initialize controller and process update.
# File lib/telegram/bot/updates_controller.rb, line 108 def dispatch(*args) new(*args).dispatch end
Second argument can be either update object with hash access & string keys or Hash with `:from` or `:chat` to override this values and assume that update is nil.
# File lib/telegram/bot/updates_controller.rb, line 126 def initialize(bot = nil, update = nil) if update.is_a?(Hash) && (update.key?(:from) || update.key?(:chat)) options = update update = nil end @_update = update @_bot = bot @_chat, @_from = options && options.values_at(:chat, :from) @_payload, @_payload_type = self.class.payload_from_update(update) end
# File lib/telegram/bot/updates_controller.rb, line 112 def payload_from_update(update) update && PAYLOAD_TYPES.find do |type| item = update[type] return [item, type] if item end end
Public Instance Methods
# File lib/telegram/bot/updates_controller.rb, line 209 def action_for_callback_query [payload_type, [payload['data']]] end
# File lib/telegram/bot/updates_controller.rb, line 205 def action_for_chosen_inline_result [payload_type, [payload['result_id'], payload['query']]] end
# File lib/telegram/bot/updates_controller.rb, line 197 def action_for_default_payload [payload_type, [payload]] end
# File lib/telegram/bot/updates_controller.rb, line 201 def action_for_inline_query [payload_type, [payload['query'], payload['offset']]] end
Calculates action name and args for payload. Uses `action_for_#{payload_type}` methods. If this method doesn't return anything it uses fallback with action same as payload type. Returns array `[action, args]`.
# File lib/telegram/bot/updates_controller.rb, line 189 def action_for_payload if payload_type send("action_for_#{payload_type}") || action_for_default_payload else [:unsupported_payload_type, []] end end
# File lib/telegram/bot/updates_controller.rb, line 213 def action_for_poll_answer [payload_type, [payload['poll_id'], payload['option_ids']]] end
Silently ignore unsupported messages to not fail when user crafts an update with usupported command, callback query context, etc.
# File lib/telegram/bot/updates_controller.rb, line 219 def action_missing(action, *_args) logger.debug { "The action '#{action}' is not defined in #{self.class.name}" } if logger nil end
There are multiple ways how action name is calculated for update (see Commands
, MessageContext
, etc.). This method represents the way how action was calculated for current udpate.
Some of possible values are `:payload, :command, :message_context`.
# File lib/telegram/bot/updates_controller.rb, line 180 def action_type action_options[:type] || :payload end
Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'` when there is no such field in payload.
Can be overriden with `chat` option for initialize.
# File lib/telegram/bot/updates_controller.rb, line 141 def chat @_chat ||= if payload if payload.is_a?(Hash) payload['chat'] || payload['message'] && payload['message']['chat'] else payload.try(:chat) || payload.try(:message).try!(:chat) end end end
Processes current update.
# File lib/telegram/bot/updates_controller.rb, line 159 def dispatch action, args = action_for_payload process(action, *args) end
Accessor to `'from'` field of payload. Can be overriden with `from` option for initialize.
# File lib/telegram/bot/updates_controller.rb, line 154 def from @_from ||= payload.is_a?(Hash) ? payload['from'] : payload.try(:from) end
It provides support for passing array as action, where first vaule is action name and second is action metadata. This metadata is stored inside action_options
# File lib/telegram/bot/updates_controller.rb, line 169 def process(action, *args) action, options = action if action.is_a?(Array) @_action_options = options || {} super end