зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-06 02:55:50 +03:00
Deprecate telegram_webhooks in favor of telegram_webhook
Этот коммит содержится в:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
- `rescue_from`.
|
- `rescue_from`.
|
||||||
- Support for `credentials` store in Rails 5.2.
|
- Support for `credentials` store in Rails 5.2.
|
||||||
|
- Deprecate `telegram_webhooks` in favor of `telegram_webhook`.
|
||||||
|
It was too complicated and such routes looked ugly.
|
||||||
|
|
||||||
# 0.12.4
|
# 0.12.4
|
||||||
|
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -340,17 +340,19 @@ end
|
|||||||
|
|
||||||
### Routes in Rails app
|
### Routes in Rails app
|
||||||
|
|
||||||
There is `telegram_webhooks` helper for rails app to define routes for webhooks.
|
There is `telegram_webhook` helper for rails app to define routes for webhooks.
|
||||||
It defines routes at `telegram/#{bot.token}` and connects bots with controller.
|
It defines routes at `telegram/#{bot.token}` and connects bots with controller.
|
||||||
For more options see [examples in wiki](https://github.com/telegram-bot-rb/telegram-bot/wiki/Routes-helpers-in-details).
|
For more options see [examples in wiki](https://github.com/telegram-bot-rb/telegram-bot/wiki/Routes-helpers-in-details).
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# Create routes for all Telegram.bots using single controller:
|
# Most off apps would require
|
||||||
telegram_webhooks TelegramController
|
telegram_webhook TelegramController
|
||||||
|
# which is same as
|
||||||
|
telegram_webhook TelegramController, :default
|
||||||
|
|
||||||
# Use different controllers for each bot:
|
# Use different controllers for each bot:
|
||||||
telegram_webhooks chat: TelegramChatController,
|
telegram_webhook TelegramChatController, :chat
|
||||||
auction: TelegramAuctionController
|
telegram_webhook TelegramAuctionController, :auction
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Processesing updates
|
#### Processesing updates
|
||||||
@@ -377,7 +379,7 @@ Use `rake telegram:bot:poller` to run poller in rails app. It automatically load
|
|||||||
changes without restart in development env.
|
changes without restart in development env.
|
||||||
Optionally pass bot id in `BOT` envvar (`BOT=chat`) to specify bot to run poller for.
|
Optionally pass bot id in `BOT` envvar (`BOT=chat`) to specify bot to run poller for.
|
||||||
|
|
||||||
This task requires `telegram_webhooks` helper to be used as it connects bots with controller.
|
This task requires `telegram_webhook` helper to be used as it connects bots with controller.
|
||||||
To run poller in other cases use:
|
To run poller in other cases use:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|||||||
@@ -12,6 +12,15 @@ module Telegram
|
|||||||
# Raised for valid telegram response with 404 status code.
|
# Raised for valid telegram response with 404 status code.
|
||||||
class NotFound < Error; end
|
class NotFound < Error; end
|
||||||
|
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def deprecation_0_14
|
||||||
|
@deprecation ||= begin
|
||||||
|
require 'active_support/deprecation'
|
||||||
|
ActiveSupport::Deprecation.new('0.14', 'Telegram::Bot')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
autoload :Async, 'telegram/bot/async'
|
autoload :Async, 'telegram/bot/async'
|
||||||
autoload :Botan, 'telegram/bot/botan'
|
autoload :Botan, 'telegram/bot/botan'
|
||||||
autoload :Client, 'telegram/bot/client'
|
autoload :Client, 'telegram/bot/client'
|
||||||
|
|||||||
@@ -28,25 +28,19 @@ module Telegram
|
|||||||
# telegram_webhooks TelegramController
|
# telegram_webhooks TelegramController
|
||||||
#
|
#
|
||||||
# # Or pass custom bots usin any of supported config options:
|
# # Or pass custom bots usin any of supported config options:
|
||||||
# telegram_webhooks TelegramController,
|
# telegram_webhooks TelegramController, [
|
||||||
# bot,
|
# bot,
|
||||||
# {token: token, username: username},
|
# {token: token, username: username},
|
||||||
# other_bot_token
|
# other_bot_token,
|
||||||
#
|
# ]
|
||||||
# # Use different controllers for each bot:
|
|
||||||
# telegram_webhooks bot => TelegramChatController,
|
|
||||||
# other_bot => TelegramAuctionController
|
|
||||||
#
|
|
||||||
# # telegram_webhooks creates named routes. See
|
|
||||||
# # RoutesHelper.route_name_for_bot for more info.
|
|
||||||
# # You can override this options or specify others:
|
|
||||||
# telegram_webhooks TelegramController, as: :my_webhook
|
|
||||||
# telegram_webhooks bot => [TelegramChatController, as: :chat_webhook],
|
|
||||||
# other_bot => TelegramAuctionController,
|
|
||||||
# admin_chat: TelegramAdminChatController
|
|
||||||
#
|
|
||||||
# TODO: Deprecate it in favor of telegram_webhook.
|
|
||||||
def telegram_webhooks(controllers, bots = nil, **options)
|
def telegram_webhooks(controllers, bots = nil, **options)
|
||||||
|
Bot.deprecation_0_14.deprecation_warning(:telegram_webhooks, <<-TXT.strip_heredoc)
|
||||||
|
It brings unnecessary complexity and encourages writeng less readable code.
|
||||||
|
Please use telegram_webhook method instead.
|
||||||
|
It's signature `telegram_webhook(controller, bot = :default, **options)`.
|
||||||
|
Multiple-bot environments now requires calling this method in a loop
|
||||||
|
or using statement for each bot.
|
||||||
|
TXT
|
||||||
unless controllers.is_a?(Hash)
|
unless controllers.is_a?(Hash)
|
||||||
bots = bots ? Array.wrap(bots) : Telegram.bots.values
|
bots = bots ? Array.wrap(bots) : Telegram.bots.values
|
||||||
controllers = Hash[bots.map { |x| [x, controllers] }]
|
controllers = Hash[bots.map { |x| [x, controllers] }]
|
||||||
@@ -59,8 +53,15 @@ module Telegram
|
|||||||
|
|
||||||
# Define route which processes requests using given controller and bot.
|
# Define route which processes requests using given controller and bot.
|
||||||
#
|
#
|
||||||
# See telegram_webhooks for examples.
|
# telegram_webhook TelegramController, bot
|
||||||
def telegram_webhook(controller, bot, **options)
|
#
|
||||||
|
# telegram_webhook TelegramController
|
||||||
|
# # same as:
|
||||||
|
# telegram_webhook TelegramController, :default
|
||||||
|
#
|
||||||
|
# # pass additional options
|
||||||
|
# telegram_webhook TelegramController, :default, as: :custom_route_name
|
||||||
|
def telegram_webhook(controller, bot = :default, **options)
|
||||||
bot = Client.wrap(bot)
|
bot = Client.wrap(bot)
|
||||||
params = {
|
params = {
|
||||||
to: Middleware.new(bot, controller),
|
to: Middleware.new(bot, controller),
|
||||||
|
|||||||
@@ -75,9 +75,9 @@ RSpec.configure do |config|
|
|||||||
require 'telegram/bot/routes_helper'
|
require 'telegram/bot/routes_helper'
|
||||||
extend Telegram::Bot::RoutesHelper
|
extend Telegram::Bot::RoutesHelper
|
||||||
|
|
||||||
telegram_webhooks default: DefaultBotController,
|
telegram_webhook DefaultBotController, :default
|
||||||
other: OtherBotController,
|
telegram_webhook OtherBotController, :other
|
||||||
named: NamedBotController
|
telegram_webhook NamedBotController, :named
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -40,6 +40,62 @@ RSpec.describe Telegram::Bot::RoutesHelper do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#telegram_webhook' do
|
||||||
|
subject { mapper.telegram_webhook(*input) }
|
||||||
|
let(:mapper) { double(:mapper).tap { |x| x.extend described_class } }
|
||||||
|
let(:bots) { {default: bot, other: other_bot} }
|
||||||
|
let(:controller) { double(:controller, name: :controller) }
|
||||||
|
let(:other_controller) { double(:other_controller, name: :other_controller) }
|
||||||
|
before { allow(Telegram).to receive(:bots) { bots } }
|
||||||
|
|
||||||
|
def assert_routes(bot, controller, route_name, options) # rubocop:disable AbcSize
|
||||||
|
expected_path = options.delete(:path) || "telegram/#{bot.token}"
|
||||||
|
expect(mapper).to receive(:post) do |path, params|
|
||||||
|
expect(path).to eq expected_path
|
||||||
|
middleware = params[:to]
|
||||||
|
expect(middleware.controller).to eq(controller)
|
||||||
|
expect(middleware.bot.token).to eq(bot.token)
|
||||||
|
expect(middleware.bot.username).to eq(bot.username)
|
||||||
|
expect(params[:as]).to eq route_name
|
||||||
|
expect(params).to include(options) if options
|
||||||
|
end
|
||||||
|
subject
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when called with controller' do
|
||||||
|
let(:input) { [controller, option: :val] }
|
||||||
|
|
||||||
|
it 'creates routes for default bot and this controller' do
|
||||||
|
assert_routes bot, controller, 'default_telegram_webhook', option: :val
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'and bot does not have configured token' do
|
||||||
|
let(:bot) { create_bot(nil) }
|
||||||
|
it 'creates routes for default bot and this controller' do
|
||||||
|
assert_routes bot, controller, 'default_telegram_webhook', option: :val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'and bot has colon in token' do
|
||||||
|
let(:bot) { create_bot('some:token') }
|
||||||
|
it 'replaces colon with underscore' do
|
||||||
|
assert_routes bot, controller, 'default_telegram_webhook',
|
||||||
|
option: :val,
|
||||||
|
path: 'telegram/some_token'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when called with controller and smth castable to bot' do
|
||||||
|
let(:input) { [controller, 'custom_token', option: :val] }
|
||||||
|
|
||||||
|
it 'creates routes for every created bot and controller' do
|
||||||
|
assert_routes create_bot('custom_token'), controller, 'telegram_webhook',
|
||||||
|
option: :val
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#telegram_webhooks' do
|
describe '#telegram_webhooks' do
|
||||||
subject { mapper.telegram_webhooks(*input) }
|
subject { mapper.telegram_webhooks(*input) }
|
||||||
let(:mapper) { double(:mapper).tap { |x| x.extend described_class } }
|
let(:mapper) { double(:mapper).tap { |x| x.extend described_class } }
|
||||||
@@ -47,6 +103,7 @@ RSpec.describe Telegram::Bot::RoutesHelper do
|
|||||||
let(:controller) { double(:controller, name: :controller) }
|
let(:controller) { double(:controller, name: :controller) }
|
||||||
let(:other_controller) { double(:other_controller, name: :other_controller) }
|
let(:other_controller) { double(:other_controller, name: :other_controller) }
|
||||||
before { allow(Telegram).to receive(:bots) { bots } }
|
before { allow(Telegram).to receive(:bots) { bots } }
|
||||||
|
around { |ex| Telegram::Bot.deprecation_0_14.silence { ex.run } }
|
||||||
|
|
||||||
def assert_routes(*expected) # rubocop:disable AbcSize
|
def assert_routes(*expected) # rubocop:disable AbcSize
|
||||||
expected.each do |(bot, controller, route_name, options)|
|
expected.each do |(bot, controller, route_name, options)|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user