Class: Telegram::Bot::UpdatesController
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- Telegram::Bot::UpdatesController
- Includes:
- AbstractController::Callbacks, AbstractController::Translation, Instrumentation
- Defined in:
- lib/telegram/bot/updates_controller.rb,
lib/telegram/bot/updates_controller/log_subscriber.rb,
lib/telegram/bot/updates_controller/instrumentation.rb
Defined Under Namespace
Modules: Instrumentation Classes: LogSubscriber
Constant Summary collapse
- PAYLOAD_TYPES =
%w( message inline_query chosen_inline_result ).freeze
- CMD_REGEX =
%r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i
- CONFLICT_CMD_REGEX =
Regexp.new("^(#{PAYLOAD_TYPES.join('|')}|\\d)")
Class Method Summary collapse
-
.action_for_command(cmd) ⇒ Object
Overrid it to filter or transform commands.
-
.command_from_text(text, username = nil) ⇒ Object
Fetches command from text message.
- .dispatch(*args) ⇒ Object
Instance Method Summary collapse
-
#action_for_payload ⇒ Object
Calculates action name and args for payload.
-
#action_missing ⇒ Object
Silently ignore unsupported messages.
- #dispatch ⇒ Object
-
#initialize(bot = nil, update = nil) ⇒ UpdatesController
constructor
A new instance of UpdatesController.
- #reply_with(type, params) ⇒ Object
Methods included from Instrumentation
instrument, prepended, #process_action
Constructor Details
#initialize(bot = nil, update = nil) ⇒ UpdatesController
Returns a new instance of UpdatesController.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/telegram/bot/updates_controller.rb', line 54 def initialize(bot = nil, update = nil) @_update = update @_bot = bot update && PAYLOAD_TYPES.find do |type| item = update[type] next unless item @_payload = item @_payload_type = type end end |
Class Method Details
.action_for_command(cmd) ⇒ Object
Overrid it to filter or transform commands. Default implementation is to convert to downcase and add ‘on_` prefix for conflicting commands.
31 32 33 34 |
# File 'lib/telegram/bot/updates_controller.rb', line 31 def action_for_command(cmd) cmd.downcase! cmd.match(CONFLICT_CMD_REGEX) ? "on_#{cmd}" : cmd end |
.command_from_text(text, username = nil) ⇒ Object
Fetches command from text message. All subsequent words are returned as arguments. If command has mention (eg. ‘/test@SomeBot`), it returns commands only for specified username. Set `username` to `true` to accept any commands.
41 42 43 44 45 46 47 |
# File 'lib/telegram/bot/updates_controller.rb', line 41 def command_from_text(text, username = nil) return unless text match = text.match CMD_REGEX return unless match return if match[3] && username != true && match[3] != username [match[1], text.split(' ').drop(1)] end |
.dispatch(*args) ⇒ Object
24 25 26 |
# File 'lib/telegram/bot/updates_controller.rb', line 24 def dispatch(*args) new(*args).dispatch end |
Instance Method Details
#action_for_payload ⇒ Object
Calculates action name and args for payload. If payload is a message with command, then returned action is an action for this command. Otherwise it’s the same as payload type. Returns array ‘[is_command?, action, args]`.
75 76 77 78 79 80 81 82 |
# File 'lib/telegram/bot/updates_controller.rb', line 75 def action_for_payload case payload_type when 'message' cmd, args = self.class.command_from_text(payload['text'], bot_username) cmd &&= self.class.action_for_command(cmd) [true, cmd, args] if cmd end || [false, payload_type, [payload]] end |
#action_missing ⇒ Object
Silently ignore unsupported messages. Params are ‘action, *args`.
86 87 |
# File 'lib/telegram/bot/updates_controller.rb', line 86 def action_missing(*) end |
#dispatch ⇒ Object
66 67 68 69 |
# File 'lib/telegram/bot/updates_controller.rb', line 66 def dispatch @_is_command, action, args = action_for_payload process(action, *args) end |
#reply_with(type, params) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/telegram/bot/updates_controller.rb', line 93 def reply_with(type, params) method = "send_#{type}" params = params.merge( chat_id: chat['id'], reply_to_message: payload['message_id'], ) bot.public_send(method, params) end |