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,78 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
# Allows to store context in session and treat next message according to this context.
|
|
5
|
+
#
|
|
6
|
+
# It provides `save_context` method to store method name
|
|
7
|
+
# to be used as action for next update:
|
|
8
|
+
#
|
|
9
|
+
# def set_location!(*)
|
|
10
|
+
# save_context(:set_location_from_message)
|
|
11
|
+
# respond_with :message, text: 'Where are you?'
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# def set_location_from_messge(city = nil, *)
|
|
15
|
+
# # update
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# # OR
|
|
19
|
+
# # This will support both `/set_location city_name`, and `/set_location`
|
|
20
|
+
# # with subsequent refinement.
|
|
21
|
+
# def set_location!(city = nil, *)
|
|
22
|
+
# if city
|
|
23
|
+
# # update
|
|
24
|
+
# else
|
|
25
|
+
# save_context(:set_location!)
|
|
26
|
+
# respond_with :message, text: 'Where are you?'
|
|
27
|
+
# end
|
|
28
|
+
# end
|
|
29
|
+
module MessageContext
|
|
30
|
+
extend ActiveSupport::Concern
|
|
31
|
+
|
|
32
|
+
include Session
|
|
33
|
+
|
|
34
|
+
# Action to clear context.
|
|
35
|
+
def cancel!
|
|
36
|
+
# Context is already cleared in action_for_message
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# Controller may have multiple sessions, let it be possible
|
|
42
|
+
# to select session for message context.
|
|
43
|
+
def message_context_session
|
|
44
|
+
session
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Fetches context and finds handler for it. If message has new command,
|
|
48
|
+
# it has higher priority than contextual action.
|
|
49
|
+
def action_for_message
|
|
50
|
+
val = message_context_session.delete(:context)
|
|
51
|
+
context = val && val.to_s
|
|
52
|
+
super || context && begin
|
|
53
|
+
args = payload['text'].try!(:split) || []
|
|
54
|
+
action = action_for_message_context(context)
|
|
55
|
+
[[action, type: :message_context, context: context], args]
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Save context for the next request.
|
|
60
|
+
def save_context(context)
|
|
61
|
+
message_context_session[:context] = context
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Returns action name for message context. By default it's the same as context name.
|
|
65
|
+
# Raises AbstractController::ActionNotFound if action is not available.
|
|
66
|
+
# This differs from other cases where invalid actions are silently ignored,
|
|
67
|
+
# because message context is controlled by developer, and users are not able
|
|
68
|
+
# to construct update to run any specific context.
|
|
69
|
+
def action_for_message_context(context)
|
|
70
|
+
action = context.to_s
|
|
71
|
+
return action if action_method?(action)
|
|
72
|
+
raise AbstractController::ActionNotFound,
|
|
73
|
+
"The context action '#{action}' is not found in #{self.class.name}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
module ReplyHelpers
|
|
5
|
+
# Helper to call bot's `send_#{type}` method with already set `chat_id`:
|
|
6
|
+
#
|
|
7
|
+
# respond_with :message, text: 'Hello!'
|
|
8
|
+
# respond_with :message, text: '__Hello!__', parse_mode: :Markdown
|
|
9
|
+
# respond_with :photo, photo: File.open(photo_to_send), caption: "It's incredible!"
|
|
10
|
+
def respond_with(type, params)
|
|
11
|
+
chat = self.chat
|
|
12
|
+
chat_id = chat && chat['id'] or raise 'Can not respond_with when chat is not present'
|
|
13
|
+
bot.public_send("send_#{type}", params.merge(chat_id: chat_id))
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Same as respond_with but also sets `reply_to_message_id`.
|
|
17
|
+
def reply_with(type, params)
|
|
18
|
+
payload = self.payload
|
|
19
|
+
message_id = payload && payload['message_id']
|
|
20
|
+
params = params.merge(reply_to_message_id: message_id) if message_id
|
|
21
|
+
respond_with(type, params)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Same as respond_with, but for inline queries.
|
|
25
|
+
def answer_inline_query(results, params = {})
|
|
26
|
+
params = params.merge(
|
|
27
|
+
inline_query_id: payload['id'],
|
|
28
|
+
results: results,
|
|
29
|
+
)
|
|
30
|
+
bot.answer_inline_query(params)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Same as respond_with, but for callback queries.
|
|
34
|
+
def answer_callback_query(text, params = {})
|
|
35
|
+
params = params.merge(
|
|
36
|
+
callback_query_id: payload['id'],
|
|
37
|
+
text: text,
|
|
38
|
+
)
|
|
39
|
+
bot.answer_callback_query(params)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Same as respond_with, but for pre checkout queries.
|
|
43
|
+
def answer_pre_checkout_query(ok, params = {})
|
|
44
|
+
params = params.merge(
|
|
45
|
+
pre_checkout_query_id: payload['id'],
|
|
46
|
+
ok: ok,
|
|
47
|
+
)
|
|
48
|
+
bot.answer_pre_checkout_query(params)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def answer_shipping_query(ok, params = {})
|
|
52
|
+
params = params.merge(
|
|
53
|
+
shipping_query_id: payload['id'],
|
|
54
|
+
ok: ok,
|
|
55
|
+
)
|
|
56
|
+
bot.answer_shipping_query(params)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Edit message from callback query.
|
|
60
|
+
def edit_message(type, params = {})
|
|
61
|
+
params =
|
|
62
|
+
if message_id = payload['inline_message_id'] # rubocop:disable AssignmentInCondition
|
|
63
|
+
params.merge(inline_message_id: message_id)
|
|
64
|
+
elsif message = payload['message'] # rubocop:disable AssignmentInCondition
|
|
65
|
+
params.merge(chat_id: message['chat']['id'], message_id: message['message_id'])
|
|
66
|
+
else
|
|
67
|
+
raise 'Can not edit message without `inline_message_id` or `message`'
|
|
68
|
+
end
|
|
69
|
+
bot.public_send("edit_message_#{type}", params)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'active_support/rescuable'
|
|
2
|
+
|
|
3
|
+
module Telegram
|
|
4
|
+
module Bot
|
|
5
|
+
class UpdatesController
|
|
6
|
+
module Rescue
|
|
7
|
+
extend ActiveSupport::Concern
|
|
8
|
+
include ActiveSupport::Rescuable
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
def process_action(*)
|
|
13
|
+
super
|
|
14
|
+
rescue Exception => exception # rubocop:disable RescueException
|
|
15
|
+
rescue_with_handler(exception) || raise
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'telegram/bot/updates_controller/testing'
|
|
2
|
+
require 'telegram/bot/rspec/message_helpers'
|
|
3
|
+
require 'telegram/bot/rspec/callback_query_helpers'
|
|
4
|
+
|
|
5
|
+
RSpec.shared_context 'telegram/bot/updates_controller' do
|
|
6
|
+
include Telegram::Bot::RSpec::ClientMatchers
|
|
7
|
+
include_context 'telegram/bot/message_helpers'
|
|
8
|
+
include_context 'telegram/bot/callback_query', :callback_query
|
|
9
|
+
|
|
10
|
+
let(:controller_class) { described_class }
|
|
11
|
+
let(:controller) do
|
|
12
|
+
controller_class.new(*controller_args).tap do |x|
|
|
13
|
+
x.extend Telegram::Bot::UpdatesController::Testing
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
let(:controller_args) { [bot, deep_stringify(update)] }
|
|
17
|
+
let(:update) { {payload_type => payload} }
|
|
18
|
+
let(:payload_type) { :some_type }
|
|
19
|
+
let(:payload) { double(:payload) }
|
|
20
|
+
let(:bot) { Telegram::Bot::ClientStub.new(bot_name) }
|
|
21
|
+
let(:bot_name) { 'bot' }
|
|
22
|
+
let(:session) { controller.send(:session) }
|
|
23
|
+
|
|
24
|
+
# Process update.
|
|
25
|
+
def dispatch(update = self.update, bot = self.bot)
|
|
26
|
+
controller.dispatch_again(bot, deep_stringify(update))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Same as `.as_json` but mocks-friendly.
|
|
30
|
+
def deep_stringify(input)
|
|
31
|
+
case input
|
|
32
|
+
when Array then input.map(&method(__callee__))
|
|
33
|
+
when Hash then input.map { |k, v| [k.to_s, deep_stringify(v)] }.to_h
|
|
34
|
+
else input
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
Telegram::Bot::RSpec.with_include_context do |config|
|
|
40
|
+
config.include_context 'telegram/bot/updates_controller', type: :telegram_bot_controller
|
|
41
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'rack/session/abstract/id'
|
|
2
|
+
require 'active_support/cache'
|
|
3
|
+
|
|
4
|
+
module Telegram
|
|
5
|
+
module Bot
|
|
6
|
+
class UpdatesController
|
|
7
|
+
# Add functionality to store data between requests.
|
|
8
|
+
module Session
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
# Builds session with given key and optional store (default to session_store).
|
|
13
|
+
# This way it's easier to define multiple custom sessions,
|
|
14
|
+
# ex. one for group chat and one for user.
|
|
15
|
+
def build_session(key, store = session_store)
|
|
16
|
+
raise 'session_store is not configured' unless store
|
|
17
|
+
key ? SessionHash.new(store, key) : NullSessionHash.new
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def process_action(*)
|
|
22
|
+
super
|
|
23
|
+
ensure
|
|
24
|
+
session.commit if @_session
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
|
|
29
|
+
def session
|
|
30
|
+
@_session ||= self.class.build_session(session_key)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def session_key
|
|
34
|
+
subject = from || chat
|
|
35
|
+
"#{bot.username}:#{subject['id']}" if subject
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Rack::Session::Abstract::SessionHash is taken to provide lazy loading.
|
|
39
|
+
# All methods that access store are overriden to support
|
|
40
|
+
# ActiveSupport::Cache::Store stores.
|
|
41
|
+
class SessionHash < Rack::Session::Abstract::SessionHash
|
|
42
|
+
attr_reader :id
|
|
43
|
+
|
|
44
|
+
def initialize(store, id)
|
|
45
|
+
@store = store
|
|
46
|
+
@id = id
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def destroy
|
|
50
|
+
clear
|
|
51
|
+
@store.delete(id)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def exists?
|
|
55
|
+
return @exists if defined?(@exists)
|
|
56
|
+
@data = {}
|
|
57
|
+
@exists = @store.exist? id
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def load!
|
|
61
|
+
session = @store.read(id)
|
|
62
|
+
@data = session ? stringify_keys(session) : {}
|
|
63
|
+
@loaded = true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def commit
|
|
67
|
+
return unless loaded?
|
|
68
|
+
data = to_hash.delete_if { |_, v| v.nil? }
|
|
69
|
+
@store.write(id, data)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
class NullSessionHash < Session::SessionHash
|
|
74
|
+
def initialize
|
|
75
|
+
@data = {}
|
|
76
|
+
@loaded = true
|
|
77
|
+
@exists = true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
alias_method :destroy, :clear
|
|
81
|
+
alias_method :load!, :id
|
|
82
|
+
alias_method :commit, :id
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
module ConfigMethods
|
|
86
|
+
delegate :session_store, to: :config
|
|
87
|
+
|
|
88
|
+
def session_store=(store)
|
|
89
|
+
config.session_store = ActiveSupport::Cache.lookup_store(store)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def use_session!
|
|
93
|
+
include Session
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
module Testing
|
|
5
|
+
IVARS_TO_KEEP = %i[@_session].freeze
|
|
6
|
+
|
|
7
|
+
# Perform multiple dispatches on same instance.
|
|
8
|
+
def dispatch_again(bot = nil, update = nil)
|
|
9
|
+
recycle!
|
|
10
|
+
initialize(bot, update)
|
|
11
|
+
dispatch
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Cleans controller between dispatches.
|
|
15
|
+
# Seems like there is nothing to clean between requests for now:
|
|
16
|
+
# everything will be rewriten with #initialize.
|
|
17
|
+
#
|
|
18
|
+
# With `full` set to `true` it'll clear all cached instance variables.
|
|
19
|
+
def recycle!(full = false)
|
|
20
|
+
return unless full
|
|
21
|
+
(instance_variables - IVARS_TO_KEEP).each do |ivar|
|
|
22
|
+
remove_instance_variable(ivar)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
protected
|
|
27
|
+
|
|
28
|
+
# Stubs session.
|
|
29
|
+
def session
|
|
30
|
+
@_session ||= Session::NullSessionHash.new
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
# Provides helpers similar to AbstractController::Translation
|
|
5
|
+
# but by default uses `action_name_i18n_key` in lazy translation keys
|
|
6
|
+
# which strips `!` from action names by default. This makes translating
|
|
7
|
+
# strings for commands more convenient.
|
|
8
|
+
#
|
|
9
|
+
# To disable this behaviour use `alias_method :action_name_i18n_key, :action_name`.
|
|
10
|
+
module Translation
|
|
11
|
+
extend ActiveSupport::Concern
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
# Class-level helper for lazy translations.
|
|
15
|
+
def translate(key, options = {})
|
|
16
|
+
key = "#{controller_path.tr('/', '.')}#{key}" if key.to_s.start_with?('.')
|
|
17
|
+
I18n.translate(key, options)
|
|
18
|
+
end
|
|
19
|
+
alias :t :translate
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# See toplevel description.
|
|
23
|
+
def translate(key, options = {})
|
|
24
|
+
if key.to_s.start_with?('.')
|
|
25
|
+
path = controller_path.tr('/', '.')
|
|
26
|
+
defaults = [:"#{path}#{key}"]
|
|
27
|
+
defaults << options[:default] if options[:default]
|
|
28
|
+
options[:default] = defaults.flatten
|
|
29
|
+
key = "#{path}.#{action_name_i18n_key}#{key}"
|
|
30
|
+
end
|
|
31
|
+
I18n.translate(key, options)
|
|
32
|
+
end
|
|
33
|
+
alias :t :translate
|
|
34
|
+
|
|
35
|
+
# Strips trailing `!` from action_name.
|
|
36
|
+
def action_name_i18n_key
|
|
37
|
+
action_name.chomp('!')
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def localize(*args)
|
|
41
|
+
I18n.localize(*args)
|
|
42
|
+
end
|
|
43
|
+
alias :l :localize
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
class UpdatesController
|
|
4
|
+
# Include this module to type cast update to Virtus model
|
|
5
|
+
# using `telegram-bot-types` gem (install this gem first).
|
|
6
|
+
module TypedUpdate
|
|
7
|
+
def initialize(bot = nil, update = nil)
|
|
8
|
+
update = Types::Update.new(update) if update && !update.is_a?(Types::Update)
|
|
9
|
+
super
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|