telegram-bot 0.3.0 → 0.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.codeclimate.yml +8 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +35 -7
- data/.travis.yml +26 -1
- data/Appraisals +31 -0
- data/CHANGELOG.md +161 -0
- data/Gemfile +8 -3
- data/README.md +463 -73
- data/bin/fetch-telegram-methods +41 -0
- data/bin/setup +1 -0
- data/gemfiles/rails_42.gemfile +21 -0
- data/gemfiles/rails_50.gemfile +21 -0
- data/gemfiles/rails_51.gemfile +21 -0
- data/gemfiles/rails_52.gemfile +21 -0
- data/gemfiles/rails_60.gemfile +21 -0
- data/gemfiles/rails_61.gemfile +23 -0
- data/lib/tasks/telegram-bot.rake +13 -7
- data/lib/telegram/bot/async.rb +157 -0
- data/lib/telegram/bot/client/api_helper.rb +31 -0
- data/lib/telegram/bot/client/api_methods.txt +75 -0
- data/lib/telegram/bot/client/typed_response.rb +26 -0
- data/lib/telegram/bot/client.rb +79 -0
- data/lib/telegram/bot/client_stub.rb +52 -0
- data/lib/telegram/bot/config_methods.rb +72 -0
- data/lib/telegram/bot/debug_client.rb +23 -0
- data/lib/telegram/bot/initializers.rb +21 -0
- data/lib/telegram/bot/middleware.rb +7 -5
- data/lib/telegram/bot/railtie.rb +4 -2
- data/lib/telegram/bot/routes_helper.rb +24 -33
- data/lib/telegram/bot/rspec/callback_query_helpers.rb +39 -0
- data/lib/telegram/bot/rspec/client_matchers.rb +151 -0
- data/lib/telegram/bot/rspec/integration/poller.rb +14 -0
- data/lib/telegram/bot/rspec/integration/rack.rb +24 -0
- data/lib/telegram/bot/rspec/integration/rails.rb +28 -0
- data/lib/telegram/bot/rspec/integration/shared.rb +14 -0
- data/lib/telegram/bot/rspec/integration.rb +10 -0
- data/lib/telegram/bot/rspec/message_helpers.rb +26 -0
- data/lib/telegram/bot/rspec.rb +16 -0
- data/lib/telegram/bot/updates_controller/callback_query_context.rb +37 -0
- data/lib/telegram/bot/updates_controller/commands.rb +44 -0
- data/lib/telegram/bot/updates_controller/instrumentation.rb +15 -8
- data/lib/telegram/bot/updates_controller/log_subscriber.rb +4 -4
- data/lib/telegram/bot/updates_controller/message_context.rb +78 -0
- data/lib/telegram/bot/updates_controller/reply_helpers.rb +74 -0
- data/lib/telegram/bot/updates_controller/rescue.rb +20 -0
- data/lib/telegram/bot/updates_controller/rspec_helpers.rb +41 -0
- data/lib/telegram/bot/updates_controller/session.rb +99 -0
- data/lib/telegram/bot/updates_controller/testing.rb +35 -0
- data/lib/telegram/bot/updates_controller/translation.rb +47 -0
- data/lib/telegram/bot/updates_controller/typed_update.rb +14 -0
- data/lib/telegram/bot/updates_controller.rb +179 -60
- data/lib/telegram/bot/updates_poller.rb +57 -27
- data/lib/telegram/bot/version.rb +2 -2
- data/lib/telegram/bot.rb +22 -84
- data/telegram-bot.gemspec +8 -5
- metadata +65 -17
- data/lib/telegram/bottable.rb +0 -41
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'active_support/core_ext/hash/keys'
|
|
2
|
+
|
|
3
|
+
module Telegram
|
|
4
|
+
module Bot
|
|
5
|
+
module Initializers
|
|
6
|
+
# Accepts different options to initialize bot.
|
|
7
|
+
def wrap(input, **options)
|
|
8
|
+
case input
|
|
9
|
+
when Symbol then by_id(input) or raise "#{name} #{input.inspect} not configured"
|
|
10
|
+
when self then input
|
|
11
|
+
when Hash then new(**input.symbolize_keys, **options)
|
|
12
|
+
else new(input, **options)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def by_id(_id)
|
|
17
|
+
raise 'Not implemented'
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
require 'active_support/concern'
|
|
2
|
-
require '
|
|
3
|
-
require '
|
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
|
3
|
+
require 'active_support/json'
|
|
4
|
+
require 'action_dispatch'
|
|
4
5
|
|
|
5
6
|
module Telegram
|
|
6
|
-
|
|
7
|
+
module Bot
|
|
7
8
|
class Middleware
|
|
8
9
|
attr_reader :bot, :controller
|
|
9
10
|
|
|
@@ -13,9 +14,10 @@ module Telegram
|
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def call(env)
|
|
16
|
-
|
|
17
|
+
request = ActionDispatch::Request.new(env)
|
|
18
|
+
update = request.request_parameters
|
|
17
19
|
controller.dispatch(bot, update)
|
|
18
|
-
[200, {}, '']
|
|
20
|
+
[200, {}, ['']]
|
|
19
21
|
end
|
|
20
22
|
|
|
21
23
|
def inspect
|
data/lib/telegram/bot/railtie.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'telegram/bot/routes_helper'
|
|
2
2
|
|
|
3
3
|
module Telegram
|
|
4
|
-
|
|
4
|
+
module Bot
|
|
5
5
|
class Railtie < Rails::Railtie
|
|
6
6
|
config.telegram_updates_controller = ActiveSupport::OrderedOptions.new
|
|
7
7
|
|
|
@@ -18,13 +18,15 @@ module Telegram
|
|
|
18
18
|
|
|
19
19
|
ActiveSupport.on_load('telegram.bot.updates_controller') do
|
|
20
20
|
self.logger = options.logger || Rails.logger
|
|
21
|
+
self.session_store = options.session_store if options.session_store
|
|
21
22
|
end
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
initializer 'telegram.bot.updates_controller.add_ar_runtime' do
|
|
25
26
|
ActiveSupport.on_load('telegram.bot.updates_controller') do
|
|
26
27
|
if defined?(ActiveRecord::Railties::ControllerRuntime)
|
|
27
|
-
|
|
28
|
+
prepend ActiveRecord::Railties::ControllerRuntime
|
|
29
|
+
extend ActiveRecord::Railties::ControllerRuntime::ClassMethods
|
|
28
30
|
end
|
|
29
31
|
end
|
|
30
32
|
end
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
require 'telegram/bot'
|
|
2
|
+
require 'active_support/core_ext/array/wrap'
|
|
2
3
|
|
|
3
4
|
module Telegram
|
|
4
|
-
|
|
5
|
+
module Bot
|
|
5
6
|
module RoutesHelper
|
|
6
7
|
class << self
|
|
7
8
|
# Returns route name for given bot. Result depends on `Telegram.bots`.
|
|
@@ -15,43 +16,33 @@ module Telegram
|
|
|
15
16
|
name && "#{name}_telegram_webhook"
|
|
16
17
|
end || 'telegram_webhook'
|
|
17
18
|
end
|
|
19
|
+
|
|
20
|
+
# Replaces colon with underscore so rails don't treat it as
|
|
21
|
+
# route parameter.
|
|
22
|
+
def escape_token(token)
|
|
23
|
+
token && token.tr(':', '_')
|
|
24
|
+
end
|
|
18
25
|
end
|
|
19
26
|
|
|
20
|
-
#
|
|
21
|
-
# telegram_webhooks TelegramController
|
|
27
|
+
# Define route which processes requests using given controller and bot.
|
|
22
28
|
#
|
|
23
|
-
#
|
|
24
|
-
# telegram_webhooks TelegramController,
|
|
25
|
-
# bot,
|
|
26
|
-
# {token: token, username: username},
|
|
27
|
-
# other_bot_token
|
|
29
|
+
# telegram_webhook TelegramController, bot
|
|
28
30
|
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
31
|
+
# telegram_webhook TelegramController
|
|
32
|
+
# # same as:
|
|
33
|
+
# telegram_webhook TelegramController, :default
|
|
32
34
|
#
|
|
33
|
-
# #
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
controllers.each do |bot, controller|
|
|
45
|
-
bot = Bot.wrap(bot)
|
|
46
|
-
controller, bot_options = controller if controller.is_a?(Array)
|
|
47
|
-
params = {
|
|
48
|
-
to: Middleware.new(bot, controller),
|
|
49
|
-
as: RoutesHelper.route_name_for_bot(bot),
|
|
50
|
-
format: false,
|
|
51
|
-
}.merge!(options).merge!(bot_options || {})
|
|
52
|
-
post("telegram/#{bot.token}", params)
|
|
53
|
-
UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
|
|
54
|
-
end
|
|
35
|
+
# # pass additional options
|
|
36
|
+
# telegram_webhook TelegramController, :default, as: :custom_route_name
|
|
37
|
+
def telegram_webhook(controller, bot = :default, **options)
|
|
38
|
+
bot = Client.wrap(bot)
|
|
39
|
+
params = {
|
|
40
|
+
to: Middleware.new(bot, controller),
|
|
41
|
+
as: RoutesHelper.route_name_for_bot(bot),
|
|
42
|
+
format: false,
|
|
43
|
+
}.merge!(options)
|
|
44
|
+
post("telegram/#{RoutesHelper.escape_token bot.token}", params)
|
|
45
|
+
UpdatesPoller.add(bot, controller) if Telegram.bot_poller_mode?
|
|
55
46
|
end
|
|
56
47
|
end
|
|
57
48
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require 'telegram/bot/rspec'
|
|
2
|
+
require 'telegram/bot/rspec/message_helpers'
|
|
3
|
+
|
|
4
|
+
# Shared helpers for testing callback query updates.
|
|
5
|
+
RSpec.shared_context 'telegram/bot/callback_query' do
|
|
6
|
+
include_context 'telegram/bot/message_helpers'
|
|
7
|
+
|
|
8
|
+
subject { -> { dispatch callback_query: payload } }
|
|
9
|
+
let(:payload) { {id: callback_query_id, from: from, message: message, data: data} }
|
|
10
|
+
let(:callback_query_id) { 11 }
|
|
11
|
+
let(:message_id) { 22 }
|
|
12
|
+
let(:message) { {message_id: message_id, chat: chat, text: 'message text'} }
|
|
13
|
+
let(:data) { raise '`let(:data) { "callback query data here" }` is required' }
|
|
14
|
+
|
|
15
|
+
# Matcher to check that origin message got edited.
|
|
16
|
+
def edit_current_message(type, options = {})
|
|
17
|
+
description = 'edit current message'
|
|
18
|
+
options = options.merge(
|
|
19
|
+
message_id: message[:message_id],
|
|
20
|
+
chat_id: chat_id,
|
|
21
|
+
)
|
|
22
|
+
Telegram::Bot::RSpec::ClientMatchers::MakeTelegramRequest.new(
|
|
23
|
+
bot, :"editMessage#{type.to_s.camelize}", description: description
|
|
24
|
+
).with(hash_including(options))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Matcher to check that callback query is answered.
|
|
28
|
+
def answer_callback_query(text = Regexp.new(''), options = {})
|
|
29
|
+
description = "answer callback query with #{text.inspect}"
|
|
30
|
+
text = a_string_matching(text) if text.is_a?(Regexp)
|
|
31
|
+
options = options.merge(
|
|
32
|
+
callback_query_id: payload[:id],
|
|
33
|
+
text: text,
|
|
34
|
+
)
|
|
35
|
+
Telegram::Bot::RSpec::ClientMatchers::MakeTelegramRequest.new(
|
|
36
|
+
bot, :answerCallbackQuery, description: description
|
|
37
|
+
).with(hash_including(options))
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
module RSpec
|
|
4
|
+
# Proxy that uses RSpec::Mocks::ArgListMatcher when it's available.
|
|
5
|
+
# Otherwise just performs `#==` match.
|
|
6
|
+
#
|
|
7
|
+
# Also allows to check argumets with custom block.
|
|
8
|
+
class ArgListMatcher
|
|
9
|
+
attr_reader :expected, :expected_proc
|
|
10
|
+
|
|
11
|
+
def initialize(*args, &block)
|
|
12
|
+
@expected_proc = block if block_given?
|
|
13
|
+
@expected =
|
|
14
|
+
if mocks_matcher?
|
|
15
|
+
::RSpec::Mocks::ArgumentListMatcher.new(*args)
|
|
16
|
+
else
|
|
17
|
+
args
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def args_match?(*actual)
|
|
22
|
+
if expected_proc
|
|
23
|
+
expected_proc[*actual]
|
|
24
|
+
true
|
|
25
|
+
elsif mocks_matcher?
|
|
26
|
+
expected.args_match?(*actual)
|
|
27
|
+
else
|
|
28
|
+
expected == actual
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def args
|
|
33
|
+
mocks_matcher? ? expected.args : expected
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def mocks_matcher?
|
|
37
|
+
defined?(::RSpec::Mocks::ArgumentListMatcher)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def to_s
|
|
41
|
+
if mocks_matcher?
|
|
42
|
+
expected.expected_args.inspect
|
|
43
|
+
elsif expected_proc
|
|
44
|
+
'(proc matcher)'
|
|
45
|
+
else
|
|
46
|
+
expected.inspect
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Matchers to test requests to Telegram API.
|
|
52
|
+
#
|
|
53
|
+
# Complex matchers requires `rspec-mocks` to be installed.
|
|
54
|
+
module ClientMatchers
|
|
55
|
+
class MakeTelegramRequest < ::RSpec::Matchers::BuiltIn::BaseMatcher
|
|
56
|
+
EXPECTATION_TYPES = {
|
|
57
|
+
exactly: :==,
|
|
58
|
+
at_most: :>=,
|
|
59
|
+
at_least: :<=,
|
|
60
|
+
}.freeze
|
|
61
|
+
|
|
62
|
+
attr_reader :performed_requests, :description
|
|
63
|
+
|
|
64
|
+
def initialize(bot, action, description: nil)
|
|
65
|
+
@bot = bot
|
|
66
|
+
@action = action
|
|
67
|
+
@description = description || "make #{action} telegram request"
|
|
68
|
+
exactly(1)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def matches?(proc) # rubocop:disable AbcSize
|
|
72
|
+
raise ArgumentError, 'matcher only supports block expectations' unless proc.is_a?(Proc)
|
|
73
|
+
original_requests_count = bot.requests[action].count
|
|
74
|
+
proc.call
|
|
75
|
+
@performed_requests = bot.requests[action].drop(original_requests_count)
|
|
76
|
+
@matching_requests_count = performed_requests.count do |request|
|
|
77
|
+
!arg_list_matcher || arg_list_matcher.args_match?(request)
|
|
78
|
+
end
|
|
79
|
+
expectation_method = EXPECTATION_TYPES[expectation_type]
|
|
80
|
+
expected_number.public_send(expectation_method, matching_requests_count)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def with(*args, &block)
|
|
84
|
+
@arg_list_matcher = ArgListMatcher.new(*args, &block)
|
|
85
|
+
self
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
EXPECTATION_TYPES.each_key do |type|
|
|
89
|
+
define_method type do |count|
|
|
90
|
+
@expectation_type = type
|
|
91
|
+
@expected_number = Integer(count)
|
|
92
|
+
self
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def times
|
|
97
|
+
self
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def failure_message
|
|
101
|
+
"expected to #{base_message}"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def failure_message_when_negated
|
|
105
|
+
"expected not to #{base_message}"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def supports_block_expectations?
|
|
109
|
+
true
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
attr_reader :bot, :action, :expectation_type, :expected_number,
|
|
115
|
+
:arg_list_matcher, :matching_requests_count
|
|
116
|
+
|
|
117
|
+
def base_message
|
|
118
|
+
"make #{expectation_type.to_s.tr('_', ' ')} #{expected_number} " \
|
|
119
|
+
"#{bot.inspect}.#{action} requests,".tap do |msg|
|
|
120
|
+
msg << " with #{arg_list_matcher}," if arg_list_matcher
|
|
121
|
+
msg << " but made #{matching_requests_count}"
|
|
122
|
+
if performed_requests
|
|
123
|
+
actual_args = performed_requests.map(&:inspect).join(', ')
|
|
124
|
+
msg << ", and #{performed_requests.count} with #{actual_args}"
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Check that bot performed request to telegram API:
|
|
131
|
+
#
|
|
132
|
+
# expect { dispatch_message('Hi!') }.
|
|
133
|
+
# to make_telegram_request(bot, :sendMessage).
|
|
134
|
+
# with(text: 'Hello!', chat_id: chat_id)
|
|
135
|
+
def make_telegram_request(bot, action)
|
|
136
|
+
MakeTelegramRequest.new(bot, action)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Helper for asserting message is sent. Note that options are checked
|
|
140
|
+
# with `hash_including`. For strict checks use #make_telegram_request.
|
|
141
|
+
def send_telegram_message(bot, text = nil, options = {})
|
|
142
|
+
description = "send telegram message #{text.inspect}"
|
|
143
|
+
text = a_string_matching(text) if text.is_a?(Regexp)
|
|
144
|
+
options = options.merge(text: text) if text
|
|
145
|
+
MakeTelegramRequest.new(bot, :sendMessage, description: description).
|
|
146
|
+
with(hash_including(options))
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'telegram/bot/rspec/integration/shared'
|
|
2
|
+
|
|
3
|
+
RSpec.shared_context 'telegram/bot/integration/poller' do
|
|
4
|
+
include_context 'telegram/bot/integration/shared'
|
|
5
|
+
let(:controller_class) { described_class }
|
|
6
|
+
|
|
7
|
+
def dispatch(update)
|
|
8
|
+
controller_class.dispatch(bot, update.as_json)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Telegram::Bot::RSpec.with_include_context do |config|
|
|
13
|
+
config.include_context 'telegram/bot/integration/poller', telegram_bot: :poller
|
|
14
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'telegram/bot/rspec/integration/shared'
|
|
2
|
+
require 'rack/test'
|
|
3
|
+
|
|
4
|
+
RSpec.shared_context 'telegram/bot/integration/rack' do
|
|
5
|
+
include_context 'telegram/bot/integration/shared'
|
|
6
|
+
include Rack::Test::Methods
|
|
7
|
+
|
|
8
|
+
let(:request_path) { raise '`let(:request_path) { path to bot }` is required' }
|
|
9
|
+
let(:app) { raise '`let(:app) { your rack app here }` is required' }
|
|
10
|
+
let(:request_headers) do
|
|
11
|
+
{
|
|
12
|
+
'ACCEPT' => 'application/json',
|
|
13
|
+
'CONTENT_TYPE' => 'application/json',
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def dispatch(update)
|
|
18
|
+
post request_path, update.to_json, request_headers
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Telegram::Bot::RSpec.with_include_context do |config|
|
|
23
|
+
config.include_context 'telegram/bot/integration/rack', telegram_bot: :rack
|
|
24
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'telegram/bot/rspec/integration/shared'
|
|
2
|
+
|
|
3
|
+
RSpec.shared_context 'telegram/bot/integration/rails', type: :request do
|
|
4
|
+
include_context 'telegram/bot/integration/shared'
|
|
5
|
+
|
|
6
|
+
let(:controller_path) do
|
|
7
|
+
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
|
8
|
+
Rails.application.routes.url_helpers.public_send("#{route_name}_path")
|
|
9
|
+
end
|
|
10
|
+
let(:request_headers) do
|
|
11
|
+
{
|
|
12
|
+
'Accept' => 'application/json',
|
|
13
|
+
'Content-Type' => 'application/json',
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def dispatch(update)
|
|
18
|
+
if ActionPack::VERSION::MAJOR >= 5
|
|
19
|
+
post(controller_path, params: update.to_json, headers: request_headers)
|
|
20
|
+
else
|
|
21
|
+
post(controller_path, update.to_json, request_headers)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
Telegram::Bot::RSpec.with_include_context do |config|
|
|
27
|
+
config.include_context 'telegram/bot/integration/rails', telegram_bot: :rails
|
|
28
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'active_support/json'
|
|
2
|
+
require 'telegram/bot'
|
|
3
|
+
require 'telegram/bot/rspec/message_helpers'
|
|
4
|
+
require 'telegram/bot/rspec/callback_query_helpers'
|
|
5
|
+
|
|
6
|
+
RSpec.shared_context 'telegram/bot/integration/shared' do
|
|
7
|
+
include Telegram::Bot::RSpec::ClientMatchers
|
|
8
|
+
include_context 'telegram/bot/message_helpers'
|
|
9
|
+
include_context 'telegram/bot/callback_query', :callback_query
|
|
10
|
+
|
|
11
|
+
let(:bot) { Telegram.bot }
|
|
12
|
+
let(:clear_session?) { described_class.respond_to?(:session_store) }
|
|
13
|
+
before { described_class.session_store.try!(:clear) if clear_session? }
|
|
14
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'telegram/bot'
|
|
2
|
+
Telegram::Bot.deprecation_0_15.warn(
|
|
3
|
+
"`require 'telegram/bot/rspec/integration'` is deprecated in favor of " \
|
|
4
|
+
"`require 'telegram/bot/rspec/integration/rails'`"
|
|
5
|
+
)
|
|
6
|
+
require 'telegram/bot/rspec/integration/rails'
|
|
7
|
+
|
|
8
|
+
Telegram::Bot::RSpec.with_include_context do |config|
|
|
9
|
+
config.include_context 'telegram/bot/integration/rails', telegram_bot: true
|
|
10
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Shared helpers for testing message updates.
|
|
2
|
+
RSpec.shared_context 'telegram/bot/message_helpers' do
|
|
3
|
+
let(:default_message_options) { {from: from, chat: chat} }
|
|
4
|
+
let(:from) { {id: from_id} }
|
|
5
|
+
let(:from_id) { 123 }
|
|
6
|
+
let(:chat) { {id: chat_id} }
|
|
7
|
+
let(:chat_id) { 456 }
|
|
8
|
+
|
|
9
|
+
# Shortcut for dispatching messages with default params.
|
|
10
|
+
def dispatch_message(text, options = {})
|
|
11
|
+
dispatch message: default_message_options.merge(options).merge(text: text)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Dispatch command message.
|
|
15
|
+
def dispatch_command(cmd, *args)
|
|
16
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
17
|
+
args.unshift("/#{cmd}")
|
|
18
|
+
dispatch_message(args.join(' '), options)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
|
22
|
+
def respond_with_message(expected = Regexp.new(''))
|
|
23
|
+
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
|
24
|
+
send_telegram_message(bot, expected, chat_id: chat_id)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
module RSpec
|
|
4
|
+
autoload :ClientMatchers, 'telegram/bot/rspec/client_matchers'
|
|
5
|
+
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Yelds a block if `include_context` is supported.
|
|
9
|
+
def with_include_context
|
|
10
|
+
::RSpec.configure do |config|
|
|
11
|
+
yield(config) if config.respond_to?(:include_context)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
# Use separate actions for different callback queries.
|
|
5
|
+
# It doesn't require session support. Simply add `%{context}:` prefix to data.
|
|
6
|
+
module CallbackQueryContext
|
|
7
|
+
protected
|
|
8
|
+
|
|
9
|
+
# Uses #context_from_callback_query as context name.
|
|
10
|
+
# If context is present checks if `%context%_callback_query` is valid
|
|
11
|
+
# action method and returns it if so. Context is stripped from data
|
|
12
|
+
# in this case. Otherwise returns `super`.
|
|
13
|
+
#
|
|
14
|
+
# It wont raise ActionNotFound as MessageContext does,
|
|
15
|
+
# because `data` param is controlled by client.
|
|
16
|
+
def action_for_callback_query
|
|
17
|
+
context, new_data = context_from_callback_query
|
|
18
|
+
if context
|
|
19
|
+
action_name = "#{context}_callback_query"
|
|
20
|
+
if action_method?(action_name)
|
|
21
|
+
action_options = {type: :callback_query_context, context: context}
|
|
22
|
+
return [[action_name, action_options], [new_data]]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
super
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def context_from_callback_query
|
|
29
|
+
data = payload['data']
|
|
30
|
+
return unless data
|
|
31
|
+
parts = data.split(':', 2)
|
|
32
|
+
parts if parts.size > 1
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
# Support for parsing commands
|
|
5
|
+
module Commands
|
|
6
|
+
CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
# Fetches command from text message. All subsequent words are returned
|
|
10
|
+
# as arguments.
|
|
11
|
+
# If command has mention (eg. `/test@SomeBot`), it returns commands only
|
|
12
|
+
# for specified username. Set `username` to `true` to accept
|
|
13
|
+
# any commands.
|
|
14
|
+
def command_from_text(text, username = nil)
|
|
15
|
+
return unless text
|
|
16
|
+
match = text.match(CMD_REGEX)
|
|
17
|
+
return unless match
|
|
18
|
+
mention = match[3]
|
|
19
|
+
[match[1], text.split.drop(1)] if username == true || !mention || mention == username
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Override it to filter or transform commands.
|
|
24
|
+
# Default implementation is to downcase and add `!` suffix.
|
|
25
|
+
def action_for_command(cmd)
|
|
26
|
+
"#{cmd.downcase}!"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# If payload is a message with command, then returned action is an
|
|
30
|
+
# action for this command.
|
|
31
|
+
# Separate method, so it can be easily overriden (ex. MessageContext).
|
|
32
|
+
#
|
|
33
|
+
# This is not used for edited messages/posts. It process them as basic updates.
|
|
34
|
+
def action_for_message
|
|
35
|
+
cmd, args = Commands.command_from_text(payload['text'], bot_username)
|
|
36
|
+
return unless cmd
|
|
37
|
+
[[action_for_command(cmd), type: :command, command: cmd], args]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
alias_method :action_for_channel_post, :action_for_message
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
module Telegram
|
|
2
|
-
|
|
2
|
+
module Bot
|
|
3
3
|
class UpdatesController
|
|
4
4
|
# Most methods are taken from ActionController::Instrumentation,
|
|
5
5
|
# some are slightly modified.
|
|
6
6
|
module Instrumentation
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
config_accessor :logger
|
|
11
|
+
end
|
|
12
12
|
|
|
13
|
+
class << self
|
|
13
14
|
def instrument(action, *args, &block)
|
|
14
15
|
ActiveSupport::Notifications.instrument(
|
|
15
16
|
"#{action}.updates_controller.bot.telegram",
|
|
@@ -35,8 +36,14 @@ module Telegram
|
|
|
35
36
|
end
|
|
36
37
|
end
|
|
37
38
|
|
|
38
|
-
def
|
|
39
|
-
Instrumentation.instrument(:
|
|
39
|
+
def respond_with(type, *)
|
|
40
|
+
Instrumentation.instrument(:respond_with, type: type) { super }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
%i[answer_callback_query answer_inline_query].each do |type|
|
|
44
|
+
define_method(type) do |*args|
|
|
45
|
+
Instrumentation.instrument(:respond_with, type: type) { super(*args) }
|
|
46
|
+
end
|
|
40
47
|
end
|
|
41
48
|
|
|
42
49
|
private
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'active_support/log_subscriber'
|
|
2
2
|
|
|
3
3
|
module Telegram
|
|
4
|
-
|
|
4
|
+
module Bot
|
|
5
5
|
class UpdatesController
|
|
6
6
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
7
7
|
def start_processing(event)
|
|
@@ -17,13 +17,13 @@ module Telegram
|
|
|
17
17
|
payload = event.payload
|
|
18
18
|
additions = UpdatesController.log_process_action(payload)
|
|
19
19
|
message = "Completed in #{event.duration.round}ms"
|
|
20
|
-
message << " (#{additions.join(' | ')})"
|
|
20
|
+
message << " (#{additions.join(' | ')})" if additions.present?
|
|
21
21
|
message
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def
|
|
26
|
-
info { "
|
|
25
|
+
def respond_with(event)
|
|
26
|
+
info { "Responded with #{event.payload[:type]}" }
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def halted_callback(event)
|