1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-04 02:05:52 +03:00

Add UpdatesController#webhook_request

Этот коммит содержится в:
Max Melentiev
2023-04-16 20:15:45 +01:00
родитель ca20bb0533
Коммит cbe7186c44
12 изменённых файлов: 76 добавлений и 12 удалений

Просмотреть файл

@@ -16,7 +16,7 @@ module Telegram
def call(env)
request = ActionDispatch::Request.new(env)
update = request.request_parameters
controller.dispatch(bot, update)
controller.dispatch(bot, update, request)
[200, {}, ['']]
end

Просмотреть файл

@@ -117,21 +117,24 @@ module Telegram
end
end
attr_internal_reader :update, :bot, :payload, :payload_type
attr_internal_reader :bot, :payload, :payload_type, :update, :webhook_request
delegate :username, to: :bot, prefix: true, allow_nil: true
# Second argument can be either update object with hash access & string
# `update` 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.
def initialize(bot = nil, update = nil)
# ActionDispatch::Request object is passed in `webhook_request` when bot running
# in webhook mode.
def initialize(bot = nil, update = nil, webhook_request = nil)
if update.is_a?(Hash) && (update.key?(:from) || update.key?(:chat))
options = update
update = nil
end
@_update = update
@_bot = bot
@_update = update
@_chat, @_from = options && options.values_at(:chat, :from)
@_payload, @_payload_type = self.class.payload_from_update(update)
@_webhook_request = webhook_request
end
# Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'`

Просмотреть файл

@@ -13,8 +13,9 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
x.extend Telegram::Bot::UpdatesController::Testing
end
end
let(:controller_args) { [bot, deep_stringify(update)] }
let(:controller_args) { [bot, deep_stringify(update), webhook_request] }
let(:update) { {payload_type => payload} }
let(:webhook_request) { nil }
let(:payload_type) { :some_type }
let(:payload) { double(:payload) }
let(:bot) { Telegram::Bot::ClientStub.new(bot_name) }
@@ -22,8 +23,8 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
let(:session) { controller.send(:session) }
# Process update.
def dispatch(update = self.update, bot = self.bot)
controller.dispatch_again(bot, deep_stringify(update))
def dispatch(update = self.update, bot = self.bot, webhook_request = self.webhook_request)
controller.dispatch_again(bot, deep_stringify(update), webhook_request)
end
# Same as `.as_json` but mocks-friendly.

Просмотреть файл

@@ -5,9 +5,9 @@ module Telegram
IVARS_TO_KEEP = %i[@_session].freeze
# Perform multiple dispatches on same instance.
def dispatch_again(bot = nil, update = nil)
def dispatch_again(bot = nil, update = nil, webhook_request = nil)
recycle!
initialize(bot, update)
initialize(bot, update, webhook_request)
dispatch
end

Просмотреть файл

@@ -4,7 +4,7 @@ module Telegram
# Include this module to type cast update to Virtus model
# using `telegram-bot-types` gem (install this gem first).
module TypedUpdate
def initialize(bot = nil, update = nil)
def initialize(bot = nil, update = nil, webhook_request = nil)
update = Types::Update.new(update) if update && !update.is_a?(Types::Update)
super
end