зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 17:55:52 +03:00
Сравнить коммиты
27 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
2eb2670ed2 | ||
|
|
6a5e642470 | ||
|
|
4ff5a58b98 | ||
|
|
ffb13321b4 | ||
|
|
3bdc4f5ae0 | ||
|
|
5603016e4a | ||
|
|
fa6b9a825e | ||
|
|
bdff6b2e1c | ||
|
|
f39a451192 | ||
|
|
9af6de8d36 | ||
|
|
c86b2ef09b | ||
|
|
bfcb1c5f41 | ||
|
|
88c8d4fd4e | ||
|
|
06253c940e | ||
|
|
eb24816e1e | ||
|
|
a67a643c7b | ||
|
|
28db729320 | ||
|
|
7f1f2bb884 | ||
|
|
88ae24b7e8 | ||
|
|
3a876a42df | ||
|
|
66991d3653 | ||
|
|
5407c5028f | ||
|
|
b41032eee9 | ||
|
|
707e532963 | ||
|
|
45e8a7283f | ||
|
|
70a1ed8a8f | ||
|
|
384d203b59 |
8
.codeclimate.yml
Обычный файл
8
.codeclimate.yml
Обычный файл
@@ -0,0 +1,8 @@
|
|||||||
|
checks:
|
||||||
|
method-complexity:
|
||||||
|
config:
|
||||||
|
threshold: 6 # should be just fine
|
||||||
|
|
||||||
|
plugins:
|
||||||
|
rubocop:
|
||||||
|
enabled: true
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
# Rubocop does not support 2.0
|
||||||
|
Gemspec/RequiredRubyVersion: {Enabled: false}
|
||||||
|
|
||||||
Rails: {Enabled: true}
|
Rails: {Enabled: true}
|
||||||
|
|
||||||
Layout/AlignParameters:
|
Layout/AlignParameters:
|
||||||
|
|||||||
32
.travis.yml
32
.travis.yml
@@ -1,10 +1,34 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
cache: bundler
|
cache: bundler
|
||||||
rvm:
|
rvm:
|
||||||
- 2.2.3
|
- 2.5
|
||||||
|
- 2.4
|
||||||
gemfile:
|
gemfile:
|
||||||
- gemfiles/rails_42.gemfile
|
- gemfiles/rails_52.gemfile
|
||||||
- gemfiles/rails_50.gemfile
|
- gemfiles/rails_51.gemfile
|
||||||
- gemfiles/rails_51.gemfile
|
matrix:
|
||||||
|
include:
|
||||||
|
# old rails
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_50.gemfile
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_42.gemfile
|
||||||
|
# old rubies
|
||||||
|
- rvm: 2.3
|
||||||
|
gemfile: gemfiles/rails_52.gemfile
|
||||||
|
# future rails
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_60.gemfile
|
||||||
|
# Allow rails 6 to fail while master branch is used
|
||||||
|
allow_failures:
|
||||||
|
- rvm: 2.5
|
||||||
|
gemfile: gemfiles/rails_60.gemfile
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
|
||||||
|
# for 2.5.0 until 2.5.1 is released:
|
||||||
|
# https://github.com/travis-ci/travis-ci/issues/8978
|
||||||
|
# https://github.com/travis-ci/travis-ci/issues/8969#issuecomment-354135622
|
||||||
|
before_install:
|
||||||
|
- gem update --system
|
||||||
|
- gem install bundler
|
||||||
|
|||||||
12
Appraisals
12
Appraisals
@@ -1,3 +1,15 @@
|
|||||||
|
appraise 'rails-60' do
|
||||||
|
git 'https://github.com/rails/rails.git' do
|
||||||
|
gem 'actionpack', '~> 6.0.0.alpha'
|
||||||
|
gem 'railties', '~> 6.0.0.alpha'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise 'rails-52' do
|
||||||
|
gem 'actionpack', '~> 5.2.0.beta2'
|
||||||
|
gem 'railties', '~> 5.2.0.beta2'
|
||||||
|
end
|
||||||
|
|
||||||
appraise 'rails-51' do
|
appraise 'rails-51' do
|
||||||
gem 'actionpack', '~> 5.1'
|
gem 'actionpack', '~> 5.1'
|
||||||
gem 'railties', '~> 5.1'
|
gem 'railties', '~> 5.1'
|
||||||
|
|||||||
19
CHANGELOG.md
19
CHANGELOG.md
@@ -1,6 +1,21 @@
|
|||||||
# Unreleased
|
# 0.13.1
|
||||||
|
|
||||||
# 0.12.2
|
- Extracted typed response mappings to telegram-bot-types gem.
|
||||||
|
It now provides definitions for all API v3.5 methods.
|
||||||
|
- Fix #chat for non-message updates with TypedUpdates enabled
|
||||||
|
|
||||||
|
# 0.13.0
|
||||||
|
|
||||||
|
- `rescue_from`.
|
||||||
|
- 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
|
||||||
|
|
||||||
|
- Fix spec helpers for callback queries.
|
||||||
|
|
||||||
|
# 0.12.3
|
||||||
|
|
||||||
- New methods from Bot API v3.5
|
- New methods from Bot API v3.5
|
||||||
- Collect all api helper-methods in Client::ApiHelper module.
|
- Collect all api helper-methods in Client::ApiHelper module.
|
||||||
|
|||||||
4
Gemfile
4
Gemfile
@@ -8,13 +8,13 @@ group :development do
|
|||||||
gem 'pry-byebug', '~> 3.2.0'
|
gem 'pry-byebug', '~> 3.2.0'
|
||||||
gem 'sdoc', '~> 0.4.1'
|
gem 'sdoc', '~> 0.4.1'
|
||||||
|
|
||||||
gem 'telegram-bot-types', '~> 0.3.0'
|
gem 'telegram-bot-types', '~> 0.6.0'
|
||||||
|
|
||||||
gem 'rspec', '~> 3.5.0'
|
gem 'rspec', '~> 3.5.0'
|
||||||
gem 'rspec-its', '~> 1.1.0'
|
gem 'rspec-its', '~> 1.1.0'
|
||||||
gem 'rspec-rails', '~> 3.5.0'
|
gem 'rspec-rails', '~> 3.5.0'
|
||||||
|
|
||||||
gem 'rubocop', '~> 0.51.0'
|
gem 'rubocop', '~> 0.52.1'
|
||||||
|
|
||||||
gem 'coveralls', '~> 0.8.2', require: false
|
gem 'coveralls', '~> 0.8.2', require: false
|
||||||
end
|
end
|
||||||
|
|||||||
35
README.md
35
README.md
@@ -11,7 +11,7 @@ in development, but you can use poller in production if you want.
|
|||||||
|
|
||||||
Package contains:
|
Package contains:
|
||||||
|
|
||||||
- Ligthweight client for bot API (with fast and thread-safe
|
- Lightweight client for bot API (with fast and thread-safe
|
||||||
[httpclient](https://github.com/nahi/httpclient) under the hood).
|
[httpclient](https://github.com/nahi/httpclient) under the hood).
|
||||||
- Controller with message parser: define methods for commands, not `case` branches.
|
- Controller with message parser: define methods for commands, not `case` branches.
|
||||||
- Middleware and routes helpers for production env.
|
- Middleware and routes helpers for production env.
|
||||||
@@ -130,6 +130,9 @@ can represent actions as separate methods keeping source much more readable and
|
|||||||
New instance of controller is instantiated for each update.
|
New instance of controller is instantiated for each update.
|
||||||
This way every update is processed in isolation from others.
|
This way every update is processed in isolation from others.
|
||||||
|
|
||||||
|
Bot controllers like usual rails controllers provides features like callbacks,
|
||||||
|
`rescue_from` and instrumentation.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
||||||
# use callbacks like in any other controllers
|
# use callbacks like in any other controllers
|
||||||
@@ -325,29 +328,37 @@ Callback queries without prefix stay untouched.
|
|||||||
# This one handles `set_value:%{something}`.
|
# This one handles `set_value:%{something}`.
|
||||||
def set_value_callback_query(new_value = nil, *)
|
def set_value_callback_query(new_value = nil, *)
|
||||||
save_this(value)
|
save_this(value)
|
||||||
answer_callback_query('Saved!)
|
answer_callback_query('Saved!')
|
||||||
end
|
end
|
||||||
|
|
||||||
# And this one is for `make_cool:%{something}`
|
# And this one is for `make_cool:%{something}`
|
||||||
def make_cool_callback_query(thing = nil, *)
|
def make_cool_callback_query(thing = nil, *)
|
||||||
do_it(thing)
|
do_it(thing)
|
||||||
answer_callback_query("#{thing} is cool now! Like a callback query context.")
|
answer_callback_query("#{thing} is cool now! Like a callback query context.")
|
||||||
end
|
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).
|
|
||||||
|
|
||||||
```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
|
||||||
|
|
||||||
|
# Defined route is named and its name depends on `Telegram.bots`.
|
||||||
|
# For single bot it will use 'telegram_webhook',
|
||||||
|
# for multiple bots it uses bot's key in the `Telegram.bots` as prefix
|
||||||
|
# (eg. `chat_telegram_webhook`).
|
||||||
|
# You can override this with `as` option:
|
||||||
|
telegram_webhook TelegramController, as: :custom_telegram_webhook
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Processesing updates
|
#### Processesing updates
|
||||||
@@ -360,7 +371,7 @@ There are several options to run it automatically:
|
|||||||
- Use poller (described in the next section).
|
- Use poller (described in the next section).
|
||||||
|
|
||||||
To run action without update (ex., send notifications from jobs),
|
To run action without update (ex., send notifications from jobs),
|
||||||
you can call `#process` directly. In this case controller can be initialized
|
you can call `#process` directly. In this case controller can be initialized
|
||||||
with `:from` and/or `:chat` options instead of `update` object:
|
with `:from` and/or `:chat` options instead of `update` object:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
@@ -374,7 +385,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
|
||||||
@@ -523,7 +534,7 @@ Yes, it's threadsafe too.
|
|||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
After checking out the repo, run `bin/setup` to install dependencies.
|
After checking out the repo, run `bin/setup` to install dependencies and git hooks.
|
||||||
Then, run `appraisal rake spec` to run the tests.
|
Then, run `appraisal rake spec` to run the tests.
|
||||||
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
gem "rubocop", "~> 0.51.0"
|
gem "rubocop", "~> 0.52.1"
|
||||||
gem "coveralls", "~> 0.8.2", require: false
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
gem "rubocop", "~> 0.51.0"
|
gem "rubocop", "~> 0.52.1"
|
||||||
gem "coveralls", "~> 0.8.2", require: false
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ group :development do
|
|||||||
gem "pry", "~> 0.10.1"
|
gem "pry", "~> 0.10.1"
|
||||||
gem "pry-byebug", "~> 3.2.0"
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
gem "sdoc", "~> 0.4.1"
|
gem "sdoc", "~> 0.4.1"
|
||||||
gem "telegram-bot-types", "~> 0.3.0"
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
gem "rspec", "~> 3.5.0"
|
gem "rspec", "~> 3.5.0"
|
||||||
gem "rspec-its", "~> 1.1.0"
|
gem "rspec-its", "~> 1.1.0"
|
||||||
gem "rspec-rails", "~> 3.5.0"
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
gem "rubocop", "~> 0.51.0"
|
gem "rubocop", "~> 0.52.1"
|
||||||
gem "coveralls", "~> 0.8.2", require: false
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
21
gemfiles/rails_52.gemfile
Обычный файл
21
gemfiles/rails_52.gemfile
Обычный файл
@@ -0,0 +1,21 @@
|
|||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "actionpack", "~> 5.2.0.beta2"
|
||||||
|
gem "railties", "~> 5.2.0.beta2"
|
||||||
|
|
||||||
|
group :development do
|
||||||
|
gem "appraisal", "~> 2.2"
|
||||||
|
gem "pry", "~> 0.10.1"
|
||||||
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
|
gem "sdoc", "~> 0.4.1"
|
||||||
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
|
gem "rspec", "~> 3.5.0"
|
||||||
|
gem "rspec-its", "~> 1.1.0"
|
||||||
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
gem "rubocop", "~> 0.52.1"
|
||||||
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
|
end
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
23
gemfiles/rails_60.gemfile
Обычный файл
23
gemfiles/rails_60.gemfile
Обычный файл
@@ -0,0 +1,23 @@
|
|||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
git "https://github.com/rails/rails.git" do
|
||||||
|
gem "actionpack", "~> 6.0.0.alpha"
|
||||||
|
gem "railties", "~> 6.0.0.alpha"
|
||||||
|
end
|
||||||
|
|
||||||
|
group :development do
|
||||||
|
gem "appraisal", "~> 2.2"
|
||||||
|
gem "pry", "~> 0.10.1"
|
||||||
|
gem "pry-byebug", "~> 3.2.0"
|
||||||
|
gem "sdoc", "~> 0.4.1"
|
||||||
|
gem "telegram-bot-types", "~> 0.6.0"
|
||||||
|
gem "rspec", "~> 3.5.0"
|
||||||
|
gem "rspec-its", "~> 1.1.0"
|
||||||
|
gem "rspec-rails", "~> 3.5.0"
|
||||||
|
gem "rubocop", "~> 0.52.1"
|
||||||
|
gem "coveralls", "~> 0.8.2", require: false
|
||||||
|
end
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
@@ -5,9 +5,22 @@ module Telegram
|
|||||||
|
|
||||||
module Bot
|
module Bot
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
class NotFound < Error; end
|
|
||||||
|
# Raised for valid telegram response with 403 status code.
|
||||||
class Forbidden < Error; end
|
class Forbidden < Error; end
|
||||||
|
|
||||||
|
# Raised for valid telegram response with 404 status code.
|
||||||
|
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'
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ require 'httpclient'
|
|||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
class Client
|
class Client
|
||||||
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
URL_TEMPLATE = 'https://api.telegram.org/bot%<token>s/'.freeze
|
||||||
|
|
||||||
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
||||||
extend Initializers
|
extend Initializers
|
||||||
@@ -36,6 +36,18 @@ module Telegram
|
|||||||
def prepare_async_args(action, body = {})
|
def prepare_async_args(action, body = {})
|
||||||
[action.to_s, Async.prepare_hash(prepare_body(body))]
|
[action.to_s, Async.prepare_hash(prepare_body(body))]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def error_for_response(response)
|
||||||
|
result = JSON.parse(response.body) rescue nil # rubocop:disable RescueModifier
|
||||||
|
return Error.new(response.reason) unless result
|
||||||
|
message = result['description'] || '-'
|
||||||
|
# This errors are raised only for valid responses from Telegram
|
||||||
|
case response.status
|
||||||
|
when 403 then Forbidden.new(message)
|
||||||
|
when 404 then NotFound.new(message)
|
||||||
|
else Error.new("#{response.reason}: #{message}")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :client, :token, :username, :base_uri
|
attr_reader :client, :token, :username, :base_uri
|
||||||
@@ -44,23 +56,13 @@ module Telegram
|
|||||||
@client = HTTPClient.new
|
@client = HTTPClient.new
|
||||||
@token = token || options[:token]
|
@token = token || options[:token]
|
||||||
@username = username || options[:username]
|
@username = username || options[:username]
|
||||||
@base_uri = format URL_TEMPLATE, self.token
|
@base_uri = format(URL_TEMPLATE, token: self.token)
|
||||||
end
|
end
|
||||||
|
|
||||||
def request(action, body = {})
|
def request(action, body = {})
|
||||||
res = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
response = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
||||||
status = res.status
|
raise self.class.error_for_response(response) if response.status >= 300
|
||||||
return JSON.parse(res.body) if status < 300
|
JSON.parse(response.body)
|
||||||
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
|
||||||
err_msg = result && result['description'] || '-'
|
|
||||||
if result
|
|
||||||
# This errors are raised only for valid responses from Telegram
|
|
||||||
case status
|
|
||||||
when 403 then raise Forbidden, err_msg
|
|
||||||
when 404 then raise NotFound, err_msg
|
|
||||||
end
|
|
||||||
end
|
|
||||||
raise Error, "#{res.reason}: #{err_msg}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Endpoint for low-level request. For easy host highjacking & instrumentation.
|
# Endpoint for low-level request. For easy host highjacking & instrumentation.
|
||||||
|
|||||||
@@ -1,35 +1,23 @@
|
|||||||
|
require 'telegram/bot/client/api_helper'
|
||||||
|
require 'active_support/core_ext/string/inflections'
|
||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
class Client
|
class Client
|
||||||
# Actions with type-casted results. Install `telegram-bot-types` gem first.
|
# Actions with type-casted results. Install `telegram-bot-types` gem first.
|
||||||
module TypedResponse
|
module TypedResponse
|
||||||
{
|
# First we define methods for every available api method to return `result`
|
||||||
getFile: :File,
|
# field instead of object.
|
||||||
getMe: :User,
|
ApiHelper.methods_list.each do |method|
|
||||||
getUpdates: [:Update],
|
define_method(method.to_s.underscore) do |*args|
|
||||||
getUserProfilePhotos: :UserProfilePhotos,
|
super(*args)['result']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
forwardMessage: :Message,
|
# And then override some of them which has castable results.
|
||||||
sendAudio: :Message,
|
Types::Response::WRAPPED_METHODS.each do |method, type|
|
||||||
sendDocument: :Message,
|
define_method(method.to_s.underscore) do |*args|
|
||||||
sendLocation: :Message,
|
Types::Response.wrap(super(*args)['result'], type)
|
||||||
sendMessage: :Message,
|
|
||||||
sendPhoto: :Message,
|
|
||||||
sendSticker: :Message,
|
|
||||||
sendVideo: :Message,
|
|
||||||
sendVoice: :Message,
|
|
||||||
}.each do |method, type|
|
|
||||||
next unless type
|
|
||||||
if type.is_a?(Array)
|
|
||||||
type_class = Types.const_get(type.first)
|
|
||||||
define_method(method.to_s.underscore) do |*args|
|
|
||||||
request(method, *args)['result'].map { |x| type_class.new(x) }
|
|
||||||
end
|
|
||||||
else
|
|
||||||
type_class = Types.const_get(type)
|
|
||||||
define_method(method.to_s.underscore) do |*args|
|
|
||||||
type_class.new request(method, *args)['result']
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
require 'active_support/core_ext/hash/keys'
|
require 'active_support/core_ext/hash/keys'
|
||||||
require 'active_support/core_ext/hash/transform_values'
|
|
||||||
require 'active_support/core_ext/hash/indifferent_access'
|
require 'active_support/core_ext/hash/indifferent_access'
|
||||||
|
|
||||||
module Telegram
|
module Telegram
|
||||||
@@ -43,7 +42,7 @@ module Telegram
|
|||||||
|
|
||||||
# Hash of botan clients made from #bots.
|
# Hash of botan clients made from #bots.
|
||||||
def botans
|
def botans
|
||||||
@botans ||= bots.transform_values(&:botan)
|
@botans ||= bots.map { |k, v| [k, v.botan] }.to_h
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns config for .bots method. By default uses `telegram['bots']` section
|
# Returns config for .bots method. By default uses `telegram['bots']` section
|
||||||
@@ -53,8 +52,9 @@ module Telegram
|
|||||||
def bots_config
|
def bots_config
|
||||||
@bots_config ||=
|
@bots_config ||=
|
||||||
if defined?(Rails.application)
|
if defined?(Rails.application)
|
||||||
secrets = Rails.application.secrets.
|
app = Rails.application
|
||||||
fetch(:telegram, {}).with_indifferent_access
|
store = app.respond_to?(:credentials) ? app.credentials : app.secrets
|
||||||
|
secrets = store.fetch(:telegram, {}).with_indifferent_access
|
||||||
secrets.fetch(:bots, {}).symbolize_keys.tap do |config|
|
secrets.fetch(:bots, {}).symbolize_keys.tap do |config|
|
||||||
default = secrets[:bot]
|
default = secrets[:bot]
|
||||||
config[:default] = default if default
|
config[:default] = default if default
|
||||||
|
|||||||
@@ -28,40 +28,49 @@ 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
|
|
||||||
#
|
|
||||||
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] }]
|
||||||
end
|
end
|
||||||
controllers.each do |bot, controller|
|
controllers.each do |bot, controller|
|
||||||
bot = Client.wrap(bot)
|
|
||||||
controller, bot_options = controller if controller.is_a?(Array)
|
controller, bot_options = controller if controller.is_a?(Array)
|
||||||
params = {
|
telegram_webhook(controller, bot, options.merge(bot_options || {}))
|
||||||
to: Middleware.new(bot, controller),
|
|
||||||
as: RoutesHelper.route_name_for_bot(bot),
|
|
||||||
format: false,
|
|
||||||
}.merge!(options).merge!(bot_options || {})
|
|
||||||
post("telegram/#{RoutesHelper.escape_token bot.token}", params)
|
|
||||||
UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Define route which processes requests using given controller and bot.
|
||||||
|
#
|
||||||
|
# telegram_webhook TelegramController, bot
|
||||||
|
#
|
||||||
|
# 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)
|
||||||
|
params = {
|
||||||
|
to: Middleware.new(bot, controller),
|
||||||
|
as: RoutesHelper.route_name_for_bot(bot),
|
||||||
|
format: false,
|
||||||
|
}.merge!(options)
|
||||||
|
post("telegram/#{RoutesHelper.escape_token bot.token}", params)
|
||||||
|
UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
RSpec.shared_context 'telegram/bot/integration' do
|
RSpec.shared_context 'telegram/bot/integration' do
|
||||||
let(:bot) { Telegram.bot }
|
let(:bot) { Telegram.bot }
|
||||||
|
let(:default_message_options) { {from: from, chat: chat} }
|
||||||
|
let(:from) { {id: from_id} }
|
||||||
let(:from_id) { 123 }
|
let(:from_id) { 123 }
|
||||||
|
let(:chat) { {id: chat_id} }
|
||||||
let(:chat_id) { 456 }
|
let(:chat_id) { 456 }
|
||||||
let(:default_message_options) { {from: {id: from_id}, chat: {id: chat_id}} }
|
|
||||||
let(:controller_path) do
|
let(:controller_path) do
|
||||||
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
||||||
Rails.application.routes.url_helpers.public_send("#{route_name}_path")
|
Rails.application.routes.url_helpers.public_send("#{route_name}_path")
|
||||||
|
|||||||
@@ -53,12 +53,19 @@ module Telegram
|
|||||||
class UpdatesController < AbstractController::Base # rubocop:disable ClassLength
|
class UpdatesController < AbstractController::Base # rubocop:disable ClassLength
|
||||||
abstract!
|
abstract!
|
||||||
|
|
||||||
require 'telegram/bot/updates_controller/session'
|
%w[
|
||||||
require 'telegram/bot/updates_controller/log_subscriber'
|
instrumentation
|
||||||
require 'telegram/bot/updates_controller/instrumentation'
|
log_subscriber
|
||||||
require 'telegram/bot/updates_controller/reply_helpers'
|
reply_helpers
|
||||||
autoload :CallbackQueryContext, 'telegram/bot/updates_controller/callback_query_context'
|
rescue
|
||||||
autoload :MessageContext, 'telegram/bot/updates_controller/message_context'
|
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
|
include AbstractController::Callbacks
|
||||||
# Redefine callbacks with default terminator.
|
# Redefine callbacks with default terminator.
|
||||||
@@ -72,11 +79,11 @@ module Telegram
|
|||||||
end
|
end
|
||||||
|
|
||||||
include AbstractController::Translation
|
include AbstractController::Translation
|
||||||
|
include Rescue
|
||||||
include ReplyHelpers
|
include ReplyHelpers
|
||||||
prepend Instrumentation
|
include Instrumentation
|
||||||
extend Session::ConfigMethods
|
|
||||||
|
|
||||||
autoload :TypedUpdate, 'telegram/bot/updates_controller/typed_update'
|
extend Session::ConfigMethods
|
||||||
|
|
||||||
PAYLOAD_TYPES = %w[
|
PAYLOAD_TYPES = %w[
|
||||||
message
|
message
|
||||||
@@ -113,10 +120,17 @@ module Telegram
|
|||||||
# any commands.
|
# any commands.
|
||||||
def command_from_text(text, username = nil)
|
def command_from_text(text, username = nil)
|
||||||
return unless text
|
return unless text
|
||||||
match = text.match CMD_REGEX
|
match = text.match(CMD_REGEX)
|
||||||
return unless match
|
return unless match
|
||||||
return if match[3] && username != true && match[3] != username
|
mention = match[3]
|
||||||
[match[1], text.split.drop(1)]
|
[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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -135,13 +149,7 @@ module Telegram
|
|||||||
@_update = update
|
@_update = update
|
||||||
@_bot = bot
|
@_bot = bot
|
||||||
@_chat, @_from = options && options.values_at(:chat, :from)
|
@_chat, @_from = options && options.values_at(:chat, :from)
|
||||||
|
@_payload, @_payload_type = self.class.payload_from_update(update)
|
||||||
payload_data = nil
|
|
||||||
update && PAYLOAD_TYPES.find do |type|
|
|
||||||
item = update[type]
|
|
||||||
payload_data = [item, type] if item
|
|
||||||
end
|
|
||||||
@_payload, @_payload_type = payload_data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'`
|
# Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'`
|
||||||
@@ -149,7 +157,14 @@ module Telegram
|
|||||||
#
|
#
|
||||||
# Can be overriden with `chat` option for #initialize.
|
# Can be overriden with `chat` option for #initialize.
|
||||||
def chat
|
def chat
|
||||||
@_chat ||= payload.try! { |x| x['chat'] || x['message'] && x['message']['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
|
end
|
||||||
|
|
||||||
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ module Telegram
|
|||||||
# Most methods are taken from ActionController::Instrumentation,
|
# Most methods are taken from ActionController::Instrumentation,
|
||||||
# some are slightly modified.
|
# some are slightly modified.
|
||||||
module Instrumentation
|
module Instrumentation
|
||||||
class << self
|
extend ActiveSupport::Concern
|
||||||
def prepended(base)
|
|
||||||
base.send :config_accessor, :logger
|
|
||||||
base.extend ClassMethods
|
|
||||||
end
|
|
||||||
|
|
||||||
|
included do
|
||||||
|
config_accessor :logger
|
||||||
|
end
|
||||||
|
|
||||||
|
class << self
|
||||||
def instrument(action, *args, &block)
|
def instrument(action, *args, &block)
|
||||||
ActiveSupport::Notifications.instrument(
|
ActiveSupport::Notifications.instrument(
|
||||||
"#{action}.updates_controller.bot.telegram",
|
"#{action}.updates_controller.bot.telegram",
|
||||||
|
|||||||
20
lib/telegram/bot/updates_controller/rescue.rb
Обычный файл
20
lib/telegram/bot/updates_controller/rescue.rb
Обычный файл
@@ -0,0 +1,20 @@
|
|||||||
|
require 'active_support/rescuable'
|
||||||
|
|
||||||
|
module Telegram
|
||||||
|
module Bot
|
||||||
|
class UpdatesController
|
||||||
|
module Rescue
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
include ActiveSupport::Rescuable
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def process_action(*)
|
||||||
|
super
|
||||||
|
rescue Exception => exception # rubocop:disable RescueException
|
||||||
|
rescue_with_handler(exception) || raise
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -35,7 +35,7 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
|||||||
def deep_stringify(input)
|
def deep_stringify(input)
|
||||||
case input
|
case input
|
||||||
when Array then input.map(&method(__callee__))
|
when Array then input.map(&method(__callee__))
|
||||||
when Hash then input.transform_keys(&:to_s).transform_values(&method(__callee__))
|
when Hash then input.map { |k, v| [k.to_s, deep_stringify(v)] }.to_h
|
||||||
else input
|
else input
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -41,40 +41,54 @@ module Telegram
|
|||||||
|
|
||||||
def start
|
def start
|
||||||
return if running
|
return if running
|
||||||
@running = true
|
begin
|
||||||
log { 'Started bot poller.' }
|
@running = true
|
||||||
while running
|
log { 'Started bot poller.' }
|
||||||
begin
|
run
|
||||||
fetch_updates do |update|
|
rescue Interrupt
|
||||||
controller.dispatch(bot, update)
|
nil # noop
|
||||||
end
|
ensure
|
||||||
rescue Interrupt
|
@running = false
|
||||||
@running = false
|
|
||||||
rescue StandardError => e
|
|
||||||
logger.error { ([e.message] + e.backtrace).join("\n") } if logger
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
log { 'Stop polling bot updates.' }
|
log { 'Stoped polling bot updates.' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
while running
|
||||||
|
updates = fetch_updates
|
||||||
|
process_updates(updates) if updates && updates.any?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Method to stop poller from other thread.
|
||||||
def stop
|
def stop
|
||||||
return unless running
|
return unless running
|
||||||
log { 'Killing polling thread.' }
|
log { 'Stopping polling bot updates.' }
|
||||||
@running = false
|
@running = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_updates
|
def fetch_updates(offset = self.offset)
|
||||||
response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) }
|
response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) }
|
||||||
updates = response.is_a?(Array) ? response : response['result']
|
response.is_a?(Array) ? response : response['result']
|
||||||
return unless updates && updates.any?
|
rescue Timeout::Error
|
||||||
|
log { 'Fetch timeout' }
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_updates(updates)
|
||||||
reload! do
|
reload! do
|
||||||
updates.each do |update|
|
updates.each do |update|
|
||||||
@offset = update['update_id'] + 1
|
@offset = update['update_id'] + 1
|
||||||
yield update
|
process_update(update)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue Timeout::Error
|
rescue StandardError => e
|
||||||
log { 'Fetch timeout' }
|
logger.error { ([e.message] + e.backtrace).join("\n") } if logger
|
||||||
|
end
|
||||||
|
|
||||||
|
# Override this method to setup custom error collector.
|
||||||
|
def process_update(update)
|
||||||
|
controller.dispatch(bot, update)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reload!
|
def reload!
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
VERSION = '0.12.3'.freeze
|
VERSION = '0.13.1'.freeze
|
||||||
|
|
||||||
def self.gem_version
|
def self.gem_version
|
||||||
Gem::Version.new VERSION
|
Gem::Version.new VERSION
|
||||||
|
|||||||
@@ -15,14 +15,21 @@ class TestApplication < Rails::Application
|
|||||||
config.eager_load = false
|
config.eager_load = false
|
||||||
config.log_level = :debug
|
config.log_level = :debug
|
||||||
config.action_dispatch.show_exceptions = false
|
config.action_dispatch.show_exceptions = false
|
||||||
secrets[:secret_key_base] = 'test'
|
|
||||||
secrets[:telegram] = {
|
telegram_config = {
|
||||||
bot: 'default_token',
|
bot: 'default_token',
|
||||||
bots: {
|
bots: {
|
||||||
other: {token: 'other_token'},
|
other: {token: 'other_token'},
|
||||||
named: {token: 'named_token', username: 'TestBot'},
|
named: {token: 'named_token', username: 'TestBot'},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if respond_to?(:credentials)
|
||||||
|
credentials.config[:telegram] = telegram_config
|
||||||
|
else
|
||||||
|
secrets[:secret_key_base] = 'test'
|
||||||
|
secrets[:telegram] = telegram_config
|
||||||
|
end
|
||||||
end
|
end
|
||||||
Rails.application.initialize!
|
Rails.application.initialize!
|
||||||
|
|
||||||
@@ -68,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
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ $LOAD_PATH.unshift GEM_ROOT.join('lib')
|
|||||||
require 'telegram/bot'
|
require 'telegram/bot'
|
||||||
require 'telegram/bot/updates_controller/rspec_helpers'
|
require 'telegram/bot/updates_controller/rspec_helpers'
|
||||||
require 'telegram/bot/types'
|
require 'telegram/bot/types'
|
||||||
require 'active_support/core_ext/object/json'
|
require 'active_support/json'
|
||||||
|
|
||||||
Dir[GEM_ROOT.join('spec/support/**/*.rb')].each { |f| require f }
|
Dir[GEM_ROOT.join('spec/support/**/*.rb')].each { |f| require f }
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
|
|
||||||
describe '#get_me' do
|
describe '#get_me' do
|
||||||
subject { bot.get_me }
|
subject { bot.get_me }
|
||||||
before { expect(bot).to receive(:request).with(:getMe) { response } }
|
before { expect(bot).to receive(:request).with('getMe') { response } }
|
||||||
let(:response) { {'ok' => true, 'result' => {'id' => user_id}} }
|
let(:response) { {'ok' => true, 'result' => {'id' => user_id}} }
|
||||||
let(:user_id) { 123 }
|
let(:user_id) { 123 }
|
||||||
it { should be_instance_of Telegram::Bot::Types::User }
|
it { should be_instance_of Telegram::Bot::Types::User }
|
||||||
@@ -17,7 +17,7 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
|
|
||||||
describe '#get_updates' do
|
describe '#get_updates' do
|
||||||
subject { bot.get_updates }
|
subject { bot.get_updates }
|
||||||
before { expect(bot).to receive(:request).with(:getUpdates) { response } }
|
before { expect(bot).to receive(:request).with('getUpdates') { response } }
|
||||||
let(:response) { {'ok' => true, 'result' => [{'update_id' => update_id}]} }
|
let(:response) { {'ok' => true, 'result' => [{'update_id' => update_id}]} }
|
||||||
let(:update_id) { 123 }
|
let(:update_id) { 123 }
|
||||||
it { should be_instance_of Array }
|
it { should be_instance_of Array }
|
||||||
@@ -29,4 +29,16 @@ RSpec.describe Telegram::Bot::Client::TypedResponse do
|
|||||||
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#delete_webhook' do
|
||||||
|
subject { bot.delete_webhook }
|
||||||
|
before { expect(bot).to receive(:request).with('deleteWebhook') { response } }
|
||||||
|
let(:response) { {'ok' => true, 'result' => true} }
|
||||||
|
it { should eq true }
|
||||||
|
|
||||||
|
context 'on error' do
|
||||||
|
let(:response) { raise Telegram::Bot::Error }
|
||||||
|
it { expect { subject }.to raise_error Telegram::Bot::Error }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ RSpec.describe Telegram::Bot::Client do
|
|||||||
describe '#request' do
|
describe '#request' do
|
||||||
subject { -> { instance.request(action, request_body) } }
|
subject { -> { instance.request(action, request_body) } }
|
||||||
let(:action) { :some_action }
|
let(:action) { :some_action }
|
||||||
let(:url) { "#{format described_class::URL_TEMPLATE, token}#{action}" }
|
let(:url) { "#{format(described_class::URL_TEMPLATE, token: token)}#{action}" }
|
||||||
let(:request_body) { double(:body) }
|
let(:request_body) { double(:body) }
|
||||||
let(:prepared_body) { double(:prepared_body) }
|
let(:prepared_body) { double(:prepared_body) }
|
||||||
let(:response) { HTTP::Message.new_response(body).tap { |x| x.status = status } }
|
let(:response) { HTTP::Message.new_response(body).tap { |x| x.status = status } }
|
||||||
|
|||||||
@@ -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)|
|
||||||
|
|||||||
@@ -69,4 +69,44 @@ RSpec.describe 'Integrations helper', :telegram_bot do
|
|||||||
it { should respond_with_message "Start: #{args[0...-1].inspect}, option: 1" }
|
it { should respond_with_message "Start: #{args[0...-1].inspect}, option: 1" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'callback queries', :callback_query do
|
||||||
|
let(:controller) do
|
||||||
|
Class.new(Telegram::Bot::UpdatesController) do
|
||||||
|
include Telegram::Bot::UpdatesController::CallbackQueryContext
|
||||||
|
|
||||||
|
def callback_query(data = nil, *)
|
||||||
|
answer_callback_query "data: #{data}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def context_callback_query(data = nil, *)
|
||||||
|
answer_callback_query "data: #{data}", extra: :param
|
||||||
|
end
|
||||||
|
|
||||||
|
def answer_and_edit_callback_query(data = nil, *)
|
||||||
|
answer_callback_query "data: #{data}"
|
||||||
|
edit_message :text, text: 'edited-text', extra: :param
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#callback_query' do
|
||||||
|
let(:data) { 'unknown:command' }
|
||||||
|
it { should answer_callback_query("data: #{data}") }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#context_callback_query' do
|
||||||
|
let(:data) { 'context:test:payload' }
|
||||||
|
it { should answer_callback_query('data: test:payload', extra: :param) }
|
||||||
|
it { should_not edit_current_message(:text) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#answer_and_edit_callback_query' do
|
||||||
|
let(:data) { 'answer_and_edit:test:payload' }
|
||||||
|
it do
|
||||||
|
should answer_callback_query(/test:payload/).
|
||||||
|
and edit_current_message(:text, text: /edited/, extra: :param)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
91
spec/telegram/bot/updates_controller/instrumentation_spec.rb
Обычный файл
91
spec/telegram/bot/updates_controller/instrumentation_spec.rb
Обычный файл
@@ -0,0 +1,91 @@
|
|||||||
|
RSpec.describe Telegram::Bot::UpdatesController::Instrumentation do
|
||||||
|
include_context 'telegram/bot/updates_controller'
|
||||||
|
|
||||||
|
subject { -> { dispatch } }
|
||||||
|
let(:update) do
|
||||||
|
build_update :message, default_message_options.merge(text: '/start')
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:controller_class) do
|
||||||
|
Class.new(Telegram::Bot::UpdatesController) do
|
||||||
|
def start(*)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:action_scope) { 'updates_controller.bot.telegram' }
|
||||||
|
let(:events) { Hash.new { |h, k| h[k] = [] } }
|
||||||
|
let!(:subscriber) do
|
||||||
|
ActiveSupport::Notifications.subscribe(/#{action_scope}/) do |action, *args|
|
||||||
|
events[action] << args
|
||||||
|
end
|
||||||
|
end
|
||||||
|
after { ActiveSupport::Notifications.unsubscribe(subscriber) }
|
||||||
|
|
||||||
|
def actions_list(*prefixes)
|
||||||
|
prefixes.map { |x| action_name(x) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def action_name(prefix)
|
||||||
|
"#{prefix}.#{action_scope}"
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#process_action' do
|
||||||
|
it 'fires start_processing and process_action events' do
|
||||||
|
should change(events, :keys).by(actions_list(:start_processing, :process_action))
|
||||||
|
|
||||||
|
action = action_name(:start_processing)
|
||||||
|
expect(events[action].size).to eq(1)
|
||||||
|
expect(events[action][0].last).to include(update: update)
|
||||||
|
|
||||||
|
action = action_name(:process_action)
|
||||||
|
expect(events[action].size).to eq(1)
|
||||||
|
expect(events[action][0].last).to include(update: update)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#halted_callback_hook' do
|
||||||
|
let(:controller_class) do
|
||||||
|
super().try(:class_eval) do |x|
|
||||||
|
x.before_action :halter_method
|
||||||
|
|
||||||
|
def halter_method
|
||||||
|
require 'active_support/version'
|
||||||
|
if ActiveSupport::VERSION::MAJOR >= 5
|
||||||
|
throw :abort
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fires halted_callback event' do
|
||||||
|
should change(events, :keys).
|
||||||
|
by(actions_list(:start_processing, :halted_callback, :process_action))
|
||||||
|
|
||||||
|
action = action_name(:halted_callback)
|
||||||
|
expect(events[action].size).to eq(1)
|
||||||
|
expect(events[action][0].last).to include(filter: :halter_method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#respond_with' do
|
||||||
|
before do
|
||||||
|
def controller.start(*)
|
||||||
|
respond_with :message, text: 'sample response'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fires respond_with event' do
|
||||||
|
should change(events, :keys).
|
||||||
|
by(actions_list(:start_processing, :respond_with, :process_action))
|
||||||
|
|
||||||
|
action = action_name(:respond_with)
|
||||||
|
expect(events[action].size).to eq(1)
|
||||||
|
expect(events[action][0].last).to include(type: :message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
31
spec/telegram/bot/updates_controller/rescue_spec.rb
Обычный файл
31
spec/telegram/bot/updates_controller/rescue_spec.rb
Обычный файл
@@ -0,0 +1,31 @@
|
|||||||
|
RSpec.describe Telegram::Bot::UpdatesController::Rescue do
|
||||||
|
include_context 'telegram/bot/updates_controller'
|
||||||
|
|
||||||
|
describe '#process_action' do
|
||||||
|
subject { -> { dispatch_message("/#{command} x y z") } }
|
||||||
|
|
||||||
|
let(:controller_class) do
|
||||||
|
Class.new(Telegram::Bot::UpdatesController) do
|
||||||
|
rescue_from ArgumentError, with: -> { respond_with :message, text: 'Rescued' }
|
||||||
|
|
||||||
|
def rescuable(*)
|
||||||
|
raise ArgumentError, 'rescuable'
|
||||||
|
end
|
||||||
|
|
||||||
|
def not_rescuable(*)
|
||||||
|
raise 'not_rescuable'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when exception is rescued' do
|
||||||
|
let(:command) { :rescuable }
|
||||||
|
it { should respond_with_message 'Rescued' }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when exception is not rescued' do
|
||||||
|
let(:command) { :not_rescuable }
|
||||||
|
it { should raise_error('not_rescuable') }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -9,17 +9,40 @@ RSpec.describe Telegram::Bot::UpdatesController::TypedUpdate do
|
|||||||
|
|
||||||
context 'when `update` is a virtus model' do
|
context 'when `update` is a virtus model' do
|
||||||
subject { controller }
|
subject { controller }
|
||||||
%w[
|
unique_types = Telegram::Bot::UpdatesController::PAYLOAD_TYPES - %w[
|
||||||
message
|
edited_message
|
||||||
inline_query
|
channel_post
|
||||||
chosen_inline_result
|
edited_channel_post
|
||||||
].each do |type|
|
]
|
||||||
|
unique_types.each do |type|
|
||||||
context "with #{type}" do
|
context "with #{type}" do
|
||||||
type_class = Telegram::Bot::Types.const_get(type.camelize)
|
type_class = Telegram::Bot::Types.const_get(type.camelize)
|
||||||
let(:payload_type) { type }
|
let(:payload_type) { type }
|
||||||
let(:payload) { {} }
|
let(:payload) do
|
||||||
|
{}.tap do |result|
|
||||||
|
result[:chat] = chat if type_class.instance_methods.include?(:chat)
|
||||||
|
result[:from] = from if type_class.instance_methods.include?(:from)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
let(:chat) { {id: 'chat_id'} }
|
||||||
|
let(:from) { {id: 'from_id'} }
|
||||||
its(:payload_type) { should eq payload_type }
|
its(:payload_type) { should eq payload_type }
|
||||||
its(:payload) { should be_instance_of type_class }
|
its(:payload) { should be_instance_of type_class }
|
||||||
|
|
||||||
|
if type_class.instance_methods.include?(:chat)
|
||||||
|
# Virtus does not support ==. :(
|
||||||
|
its(:chat) { should be_instance_of Telegram::Bot::Types::Chat }
|
||||||
|
its('chat.to_hash') { should include chat }
|
||||||
|
else
|
||||||
|
its(:chat) { should eq nil }
|
||||||
|
end
|
||||||
|
|
||||||
|
if type_class.instance_methods.include?(:from)
|
||||||
|
its(:from) { should be_instance_of Telegram::Bot::Types::User }
|
||||||
|
its('from.to_hash') { should include from }
|
||||||
|
else
|
||||||
|
its(:from) { should eq nil }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -8,12 +8,32 @@ RSpec.describe Telegram::Bot::UpdatesPoller do
|
|||||||
it { should be }
|
it { should be }
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#fetch_updates' do
|
describe '#process_updates' do
|
||||||
subject { -> { instance.fetch_updates(&block) } }
|
subject { -> { instance.process_updates(updates) } }
|
||||||
let(:block) { ->(x) { expect(x).to eq expected_results.shift } }
|
let(:block) { ->(x) { expect(x).to eq expected_results.shift } }
|
||||||
let(:results) { [{update_id: 12}, {update_id: 34}] }
|
let(:updates) { [{update_id: 12}, {update_id: 34}].as_json }
|
||||||
let(:expected_results) { results.as_json }
|
let(:processed_updates) { [] }
|
||||||
let(:request_result) { {ok: true, result: results}.as_json }
|
before do
|
||||||
|
allow(controller).to receive(:dispatch) do |bot, update|
|
||||||
|
expect(bot).to eq self.bot
|
||||||
|
processed_updates << update
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it { should change(instance, :offset).to(updates.last['update_id'] + 1) }
|
||||||
|
it { should change(self, :processed_updates).to(updates) }
|
||||||
|
|
||||||
|
context 'with typed response' do
|
||||||
|
let(:updates) { super().map { |x| Telegram::Bot::Types::Update.new(x) } }
|
||||||
|
it { should change(instance, :offset).to(updates.last['update_id'] + 1) }
|
||||||
|
it { should change(self, :processed_updates).to(updates) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#fetch_updates' do
|
||||||
|
subject { instance.fetch_updates }
|
||||||
|
let(:updates) { [{update_id: 12}, {update_id: 34}] }
|
||||||
|
let(:request_result) { {ok: true, result: updates}.as_json }
|
||||||
before do
|
before do
|
||||||
allow(bot).to receive(:get_updates) do
|
allow(bot).to receive(:get_updates) do
|
||||||
expect(bot.async).to be_falsy
|
expect(bot.async).to be_falsy
|
||||||
@@ -21,19 +41,17 @@ RSpec.describe Telegram::Bot::UpdatesPoller do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should change(instance, :offset).to(results.last[:update_id] + 1) }
|
it { should eq updates.as_json }
|
||||||
it { should change { expected_results }.to([]) }
|
|
||||||
|
|
||||||
context 'with typed response' do
|
context 'with typed response' do
|
||||||
let(:request_result) { results.as_json.map { |x| Telegram::Bot::Types::Update.new(x) } }
|
let(:updates) { super().map { |x| Telegram::Bot::Types::Update.new(x.as_json) } }
|
||||||
let(:expected_results) { request_result.dup }
|
let(:request_result) { updates }
|
||||||
it { should change(instance, :offset).to(results.last[:update_id] + 1) }
|
it { should eq updates }
|
||||||
it { should change { expected_results }.to([]) }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when bot is in async mode' do
|
context 'when bot is in async mode' do
|
||||||
let(:bot) { Telegram::Bot::Client.new('token', async: Class.new) }
|
let(:bot) { Telegram::Bot::Client.new('token', async: Class.new) }
|
||||||
it { should change { expected_results }.to([]) }
|
it { should eq updates.as_json }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,6 +23,6 @@ Gem::Specification.new do |spec|
|
|||||||
spec.add_dependency 'activesupport', '>= 4.0', '< 6.0'
|
spec.add_dependency 'activesupport', '>= 4.0', '< 6.0'
|
||||||
spec.add_dependency 'httpclient', '~> 2.7'
|
spec.add_dependency 'httpclient', '~> 2.7'
|
||||||
|
|
||||||
spec.add_development_dependency 'bundler', '~> 1.11'
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
||||||
spec.add_development_dependency 'rake', '~> 10.0'
|
spec.add_development_dependency 'rake', '~> 10.0'
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user