lib/telegram/bot/updates_controller.rb in telegram-bot-0.12.4 vs lib/telegram/bot/updates_controller.rb in telegram-bot-0.13.0
- old
+ new
@@ -51,17 +51,24 @@
# process(:help, *args)
#
class UpdatesController < AbstractController::Base # rubocop:disable ClassLength
abstract!
- require 'telegram/bot/updates_controller/session'
- require 'telegram/bot/updates_controller/log_subscriber'
- require 'telegram/bot/updates_controller/instrumentation'
- require 'telegram/bot/updates_controller/reply_helpers'
- autoload :CallbackQueryContext, 'telegram/bot/updates_controller/callback_query_context'
- autoload :MessageContext, 'telegram/bot/updates_controller/message_context'
+ %w[
+ instrumentation
+ log_subscriber
+ reply_helpers
+ rescue
+ session
+ ].each { |file| require "telegram/bot/updates_controller/#{file}" }
+ %w[
+ CallbackQueryContext
+ MessageContext
+ TypedUpdate
+ ].each { |mod| autoload mod, "telegram/bot/updates_controller/#{mod.underscore}" }
+
include AbstractController::Callbacks
# Redefine callbacks with default terminator.
if ActiveSupport::VERSION::MAJOR >= 5
define_callbacks :process_action,
skip_after_callbacks_if_terminated: true
@@ -70,16 +77,16 @@
terminator: ->(_, result) { result == false },
skip_after_callbacks_if_terminated: true
end
include AbstractController::Translation
+ include Rescue
include ReplyHelpers
- prepend Instrumentation
+ include Instrumentation
+
extend Session::ConfigMethods
- autoload :TypedUpdate, 'telegram/bot/updates_controller/typed_update'
-
PAYLOAD_TYPES = %w[
message
edited_message
channel_post
edited_channel_post
@@ -111,15 +118,22 @@
# If command has mention (eg. `/test@SomeBot`), it returns commands only
# for specified username. Set `username` to `true` to accept
# any commands.
def command_from_text(text, username = nil)
return unless text
- match = text.match CMD_REGEX
+ match = text.match(CMD_REGEX)
return unless match
- return if match[3] && username != true && match[3] != username
- [match[1], text.split.drop(1)]
+ mention = match[3]
+ [match[1], text.split.drop(1)] if username == true || !mention || mention == username
end
+
+ def payload_from_update(update)
+ update && PAYLOAD_TYPES.find do |type|
+ item = update[type]
+ return [item, type] if item
+ end
+ end
end
attr_internal_reader :update, :bot, :payload, :payload_type, :is_command
alias_method :command?, :is_command
delegate :username, to: :bot, prefix: true, allow_nil: true
@@ -133,16 +147,10 @@
update = nil
end
@_update = update
@_bot = bot
@_chat, @_from = options && options.values_at(:chat, :from)
-
- payload_data = nil
- update && PAYLOAD_TYPES.find do |type|
- item = update[type]
- payload_data = [item, type] if item
- end
- @_payload, @_payload_type = payload_data
+ @_payload, @_payload_type = self.class.payload_from_update(update)
end
# Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'`
# when there is no such field in payload.
#