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
|
@@ -1,102 +1,221 @@
|
|
|
1
1
|
require 'abstract_controller'
|
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
|
2
3
|
require 'active_support/callbacks'
|
|
4
|
+
require 'active_support/version'
|
|
3
5
|
|
|
4
6
|
module Telegram
|
|
5
|
-
|
|
6
|
-
class
|
|
7
|
+
module Bot
|
|
8
|
+
# Base class to create update processors. With callbacks, session and helpers.
|
|
9
|
+
#
|
|
10
|
+
# Public methods ending with `!` handle messages with commands.
|
|
11
|
+
# Message text is automatically parsed into method arguments.
|
|
12
|
+
# Be sure to use default values and
|
|
13
|
+
# splat arguments in every action method to not get errors, when user
|
|
14
|
+
# sends command without necessary args / with extra args.
|
|
15
|
+
#
|
|
16
|
+
# def start!(token = nil, *)
|
|
17
|
+
# if token
|
|
18
|
+
# # ...
|
|
19
|
+
# else
|
|
20
|
+
# # ...
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# def help!(*)
|
|
25
|
+
# respond_with :message, text:
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# To process plain text messages (without commands) or other updates just
|
|
29
|
+
# define public method with name of payload type.
|
|
30
|
+
# By default they receive payload as an argument, but some of them are called
|
|
31
|
+
# with more usefuk args:
|
|
32
|
+
#
|
|
33
|
+
# def message(message)
|
|
34
|
+
# respond_with :message, text: "Echo: #{message['text']}"
|
|
35
|
+
# end
|
|
36
|
+
#
|
|
37
|
+
# def inline_query(query, offset)
|
|
38
|
+
# answer_inline_query results_for_query(query, offset), is_personal: true
|
|
39
|
+
# end
|
|
40
|
+
#
|
|
41
|
+
# To process update run:
|
|
42
|
+
#
|
|
43
|
+
# ControllerClass.dispatch(bot, update)
|
|
44
|
+
#
|
|
45
|
+
# There is also ability to run action without update:
|
|
46
|
+
#
|
|
47
|
+
# ControllerClass.new(bot, from: telegram_user, chat: telegram_chat).
|
|
48
|
+
# process(:help, *args)
|
|
49
|
+
#
|
|
50
|
+
class UpdatesController < AbstractController::Base # rubocop:disable ClassLength
|
|
51
|
+
abstract!
|
|
52
|
+
|
|
53
|
+
%w[
|
|
54
|
+
Commands
|
|
55
|
+
Instrumentation
|
|
56
|
+
LogSubscriber
|
|
57
|
+
ReplyHelpers
|
|
58
|
+
Rescue
|
|
59
|
+
Session
|
|
60
|
+
Translation
|
|
61
|
+
].each { |name| require "telegram/bot/updates_controller/#{name.underscore}" }
|
|
62
|
+
|
|
63
|
+
%w[
|
|
64
|
+
CallbackQueryContext
|
|
65
|
+
MessageContext
|
|
66
|
+
TypedUpdate
|
|
67
|
+
].each { |mod| autoload mod, "telegram/bot/updates_controller/#{mod.underscore}" }
|
|
68
|
+
|
|
7
69
|
include AbstractController::Callbacks
|
|
8
|
-
|
|
70
|
+
# Redefine callbacks with default terminator.
|
|
71
|
+
if ActiveSupport::VERSION::MAJOR >= 5
|
|
72
|
+
define_callbacks :process_action,
|
|
73
|
+
skip_after_callbacks_if_terminated: true
|
|
74
|
+
else
|
|
75
|
+
define_callbacks :process_action,
|
|
76
|
+
terminator: ->(_, result) { result == false },
|
|
77
|
+
skip_after_callbacks_if_terminated: true
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
include Commands
|
|
81
|
+
include Rescue
|
|
82
|
+
include ReplyHelpers
|
|
83
|
+
include Translation
|
|
84
|
+
# Add instrumentations hooks at the bottom, to ensure they instrument
|
|
85
|
+
# all the methods properly.
|
|
86
|
+
include Instrumentation
|
|
9
87
|
|
|
10
|
-
|
|
11
|
-
require 'telegram/bot/updates_controller/instrumentation'
|
|
12
|
-
prepend Instrumentation
|
|
88
|
+
extend Session::ConfigMethods
|
|
13
89
|
|
|
14
|
-
PAYLOAD_TYPES = %w
|
|
90
|
+
PAYLOAD_TYPES = %w[
|
|
15
91
|
message
|
|
92
|
+
edited_message
|
|
93
|
+
channel_post
|
|
94
|
+
edited_channel_post
|
|
16
95
|
inline_query
|
|
17
96
|
chosen_inline_result
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
97
|
+
callback_query
|
|
98
|
+
shipping_query
|
|
99
|
+
pre_checkout_query
|
|
100
|
+
].freeze
|
|
22
101
|
|
|
23
102
|
class << self
|
|
103
|
+
# Initialize controller and process update.
|
|
24
104
|
def dispatch(*args)
|
|
25
105
|
new(*args).dispatch
|
|
26
106
|
end
|
|
27
107
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
cmd.match(CONFLICT_CMD_REGEX) ? "on_#{cmd}" : cmd
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
# Fetches command from text message. All subsequent words are returned
|
|
37
|
-
# as arguments.
|
|
38
|
-
# If command has mention (eg. `/test@SomeBot`), it returns commands only
|
|
39
|
-
# for specified username. Set `username` to `true` to accept
|
|
40
|
-
# any commands.
|
|
41
|
-
def command_from_text(text, username = nil)
|
|
42
|
-
return unless text
|
|
43
|
-
match = text.match CMD_REGEX
|
|
44
|
-
return unless match
|
|
45
|
-
return if match[3] && username != true && match[3] != username
|
|
46
|
-
[match[1], text.split(' ').drop(1)]
|
|
108
|
+
def payload_from_update(update)
|
|
109
|
+
update && PAYLOAD_TYPES.find do |type|
|
|
110
|
+
item = update[type]
|
|
111
|
+
return [item, type] if item
|
|
112
|
+
end
|
|
47
113
|
end
|
|
48
114
|
end
|
|
49
115
|
|
|
50
|
-
attr_internal_reader :update, :bot, :payload, :payload_type
|
|
51
|
-
alias_method :command?, :is_command
|
|
116
|
+
attr_internal_reader :update, :bot, :payload, :payload_type
|
|
52
117
|
delegate :username, to: :bot, prefix: true, allow_nil: true
|
|
53
118
|
|
|
119
|
+
# Second argument can be either update object with hash access & string
|
|
120
|
+
# keys or Hash with `:from` or `:chat` to override this values and assume
|
|
121
|
+
# that update is nil.
|
|
54
122
|
def initialize(bot = nil, update = nil)
|
|
123
|
+
if update.is_a?(Hash) && (update.key?(:from) || update.key?(:chat))
|
|
124
|
+
options = update
|
|
125
|
+
update = nil
|
|
126
|
+
end
|
|
55
127
|
@_update = update
|
|
56
128
|
@_bot = bot
|
|
129
|
+
@_chat, @_from = options && options.values_at(:chat, :from)
|
|
130
|
+
@_payload, @_payload_type = self.class.payload_from_update(update)
|
|
131
|
+
end
|
|
57
132
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
133
|
+
# Accessor to `'chat'` field of payload. Also tries `'chat'` in `'message'`
|
|
134
|
+
# when there is no such field in payload.
|
|
135
|
+
#
|
|
136
|
+
# Can be overriden with `chat` option for #initialize.
|
|
137
|
+
def chat
|
|
138
|
+
@_chat ||=
|
|
139
|
+
if payload
|
|
140
|
+
if payload.is_a?(Hash)
|
|
141
|
+
payload['chat'] || payload['message'] && payload['message']['chat']
|
|
142
|
+
else
|
|
143
|
+
payload.try(:chat) || payload.try(:message).try!(:chat)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Accessor to `'from'` field of payload. Can be overriden with `from` option
|
|
149
|
+
# for #initialize.
|
|
150
|
+
def from
|
|
151
|
+
@_from ||= payload && payload['from']
|
|
64
152
|
end
|
|
65
153
|
|
|
154
|
+
# Processes current update.
|
|
66
155
|
def dispatch
|
|
67
|
-
|
|
156
|
+
action, args = action_for_payload
|
|
68
157
|
process(action, *args)
|
|
69
158
|
end
|
|
70
159
|
|
|
160
|
+
attr_internal_reader :action_options
|
|
161
|
+
|
|
162
|
+
# It provides support for passing array as action, where first vaule
|
|
163
|
+
# is action name and second is action metadata.
|
|
164
|
+
# This metadata is stored inside action_options
|
|
165
|
+
def process(action, *args)
|
|
166
|
+
action, options = action if action.is_a?(Array)
|
|
167
|
+
@_action_options = options || {}
|
|
168
|
+
super
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# There are multiple ways how action name is calculated for update
|
|
172
|
+
# (see Commands, MessageContext, etc.). This method represents the
|
|
173
|
+
# way how action was calculated for current udpate.
|
|
174
|
+
#
|
|
175
|
+
# Some of possible values are `:payload, :command, :message_context`.
|
|
176
|
+
def action_type
|
|
177
|
+
action_options[:type] || :payload
|
|
178
|
+
end
|
|
179
|
+
|
|
71
180
|
# Calculates action name and args for payload.
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
181
|
+
# Uses `action_for_#{payload_type}` methods.
|
|
182
|
+
# If this method doesn't return anything
|
|
183
|
+
# it uses fallback with action same as payload type.
|
|
184
|
+
# Returns array `[action, args]`.
|
|
75
185
|
def action_for_payload
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
186
|
+
if payload_type
|
|
187
|
+
send("action_for_#{payload_type}") || action_for_default_payload
|
|
188
|
+
else
|
|
189
|
+
[:unsupported_payload_type, []]
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def action_for_default_payload
|
|
194
|
+
[payload_type, [payload]]
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def action_for_inline_query
|
|
198
|
+
[payload_type, [payload['query'], payload['offset']]]
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def action_for_chosen_inline_result
|
|
202
|
+
[payload_type, [payload['result_id'], payload['query']]]
|
|
82
203
|
end
|
|
83
204
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def action_missing(*)
|
|
205
|
+
def action_for_callback_query
|
|
206
|
+
[payload_type, [payload['data']]]
|
|
87
207
|
end
|
|
88
208
|
|
|
89
|
-
|
|
90
|
-
|
|
209
|
+
# Silently ignore unsupported messages to not fail when user crafts
|
|
210
|
+
# an update with usupported command, callback query context, etc.
|
|
211
|
+
def action_missing(action, *_args)
|
|
212
|
+
logger.debug { "The action '#{action}' is not defined in #{self.class.name}" } if logger
|
|
213
|
+
nil
|
|
91
214
|
end
|
|
92
215
|
|
|
93
|
-
|
|
94
|
-
method = "
|
|
95
|
-
|
|
96
|
-
chat_id: chat['id'],
|
|
97
|
-
reply_to_message: payload['message_id'],
|
|
98
|
-
)
|
|
99
|
-
bot.public_send(method, params)
|
|
216
|
+
PAYLOAD_TYPES.each do |type|
|
|
217
|
+
method = :"action_for_#{type}"
|
|
218
|
+
alias_method method, :action_for_default_payload unless instance_methods.include?(method)
|
|
100
219
|
end
|
|
101
220
|
|
|
102
221
|
ActiveSupport.run_load_hooks('telegram.bot.updates_controller', self)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module Telegram
|
|
2
|
-
|
|
2
|
+
module Bot
|
|
3
3
|
# Supposed to be used in development environments only.
|
|
4
4
|
class UpdatesPoller
|
|
5
5
|
class << self
|
|
@@ -15,7 +15,7 @@ module Telegram
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def start(bot_id, controller = nil)
|
|
18
|
-
bot = bot_id.is_a?(Symbol) ? Telegram.bots[bot_id] :
|
|
18
|
+
bot = bot_id.is_a?(Symbol) ? Telegram.bots[bot_id] : Client.wrap(bot_id)
|
|
19
19
|
instance = controller ? new(bot, controller) : instances[bot]
|
|
20
20
|
raise "Poller not found for #{bot_id.inspect}" unless instance
|
|
21
21
|
instance.start
|
|
@@ -27,12 +27,12 @@ module Telegram
|
|
|
27
27
|
attr_reader :bot, :controller, :timeout, :offset, :logger, :running, :reload
|
|
28
28
|
|
|
29
29
|
def initialize(bot, controller, **options)
|
|
30
|
-
@logger = options.fetch(:logger) { defined?(Rails) && Rails.logger }
|
|
30
|
+
@logger = options.fetch(:logger) { defined?(Rails.logger) && Rails.logger }
|
|
31
31
|
@bot = bot
|
|
32
32
|
@controller = controller
|
|
33
33
|
@timeout = options.fetch(:timeout) { DEFAULT_TIMEOUT }
|
|
34
34
|
@offset = options[:offset]
|
|
35
|
-
@reload = options.fetch(:reload) { defined?(Rails) && Rails.env.development? }
|
|
35
|
+
@reload = options.fetch(:reload) { defined?(Rails.env) && Rails.env.development? }
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def log(&block)
|
|
@@ -41,47 +41,77 @@ module Telegram
|
|
|
41
41
|
|
|
42
42
|
def start
|
|
43
43
|
return if running
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
begin
|
|
45
|
+
@running = true
|
|
46
|
+
log { 'Started bot poller.' }
|
|
47
|
+
run
|
|
48
|
+
rescue Interrupt
|
|
49
|
+
nil # noop
|
|
50
|
+
ensure
|
|
51
|
+
@running = false
|
|
52
|
+
end
|
|
53
|
+
log { 'Stoped polling bot updates.' }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def run
|
|
46
57
|
while running
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
controller.dispatch(bot, update)
|
|
50
|
-
end
|
|
51
|
-
rescue Interrupt
|
|
52
|
-
@running = false
|
|
53
|
-
rescue => e
|
|
54
|
-
logger.error { ([e.message] + e.backtrace).join("\n") } if logger
|
|
55
|
-
end
|
|
58
|
+
updates = fetch_updates
|
|
59
|
+
process_updates(updates) if updates && updates.any?
|
|
56
60
|
end
|
|
57
|
-
log { 'Stop polling bot updates.' }
|
|
58
61
|
end
|
|
59
62
|
|
|
63
|
+
# Method to stop poller from other thread.
|
|
60
64
|
def stop
|
|
61
65
|
return unless running
|
|
62
|
-
log { '
|
|
66
|
+
log { 'Stopping polling bot updates.' }
|
|
63
67
|
@running = false
|
|
64
68
|
end
|
|
65
69
|
|
|
66
|
-
def fetch_updates
|
|
67
|
-
response = bot.get_updates(offset: offset, timeout: timeout)
|
|
68
|
-
|
|
70
|
+
def fetch_updates(offset = self.offset)
|
|
71
|
+
response = bot.async(false) { bot.get_updates(offset: offset, timeout: timeout) }
|
|
72
|
+
response.is_a?(Array) ? response : response['result']
|
|
73
|
+
rescue Timeout::Error
|
|
74
|
+
log { 'Fetch timeout' }
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def process_updates(updates)
|
|
69
79
|
reload! do
|
|
70
|
-
|
|
80
|
+
updates.each do |update|
|
|
71
81
|
@offset = update['update_id'] + 1
|
|
72
|
-
|
|
82
|
+
process_update(update)
|
|
73
83
|
end
|
|
74
84
|
end
|
|
75
|
-
rescue
|
|
85
|
+
rescue StandardError => e
|
|
86
|
+
logger.error { ([e.message] + e.backtrace).join("\n") } if logger
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Override this method to setup custom error collector.
|
|
90
|
+
def process_update(update)
|
|
91
|
+
controller.dispatch(bot, update)
|
|
76
92
|
end
|
|
77
93
|
|
|
78
94
|
def reload!
|
|
79
95
|
return yield unless reload
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
96
|
+
reloading_code do
|
|
97
|
+
if controller.is_a?(Class) && controller.name
|
|
98
|
+
@controller = Object.const_get(controller.name)
|
|
99
|
+
end
|
|
100
|
+
yield
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
if defined?(Rails.application) && Rails.application.respond_to?(:reloader)
|
|
105
|
+
def reloading_code
|
|
106
|
+
Rails.application.reloader.wrap do
|
|
107
|
+
yield
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
else
|
|
111
|
+
def reloading_code
|
|
112
|
+
ActionDispatch::Reloader.prepare!
|
|
113
|
+
yield.tap { ActionDispatch::Reloader.cleanup! }
|
|
83
114
|
end
|
|
84
|
-
yield.tap { ActionDispatch::Reloader.cleanup! }
|
|
85
115
|
end
|
|
86
116
|
end
|
|
87
117
|
end
|
data/lib/telegram/bot/version.rb
CHANGED
data/lib/telegram/bot.rb
CHANGED
|
@@ -1,97 +1,35 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require 'json'
|
|
3
|
-
require 'active_support/core_ext/string/inflections'
|
|
4
|
-
require 'active_support/core_ext/hash/keys'
|
|
5
|
-
require 'active_support/core_ext/array/wrap'
|
|
6
|
-
require 'telegram/bottable'
|
|
1
|
+
require 'telegram/bot/config_methods'
|
|
7
2
|
|
|
8
3
|
module Telegram
|
|
9
|
-
extend
|
|
4
|
+
extend Bot::ConfigMethods
|
|
10
5
|
|
|
11
|
-
|
|
6
|
+
module Bot
|
|
12
7
|
class Error < StandardError; end
|
|
13
|
-
class NotFound < Error; end
|
|
14
|
-
|
|
15
|
-
autoload :Middleware, 'telegram/bot/middleware'
|
|
16
|
-
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
|
17
|
-
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
|
18
|
-
|
|
19
|
-
URL_TEMPLATE = 'https://api.telegram.org/bot%s/'.freeze
|
|
20
8
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def wrap(input)
|
|
24
|
-
case input
|
|
25
|
-
when self then input
|
|
26
|
-
when Array then input.map(&method(__callee__))
|
|
27
|
-
when Hash then
|
|
28
|
-
input = input.stringify_keys
|
|
29
|
-
new input['token'], input['username']
|
|
30
|
-
else
|
|
31
|
-
new(input)
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
attr_reader :client, :token, :username, :base_uri
|
|
9
|
+
# Raised for valid telegram response with 403 status code.
|
|
10
|
+
class Forbidden < Error; end
|
|
37
11
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
@token = token
|
|
41
|
-
@username = username
|
|
42
|
-
@base_uri = format URL_TEMPLATE, token
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def debug!(dev = STDOUT)
|
|
46
|
-
client.debug_dev = dev
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def debug_off!
|
|
50
|
-
client.debug_dev = nil
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def request(action, data = {})
|
|
54
|
-
res = http_request("#{base_uri}#{action}", data)
|
|
55
|
-
status = res.status
|
|
56
|
-
return JSON.parse(res.body) if 300 > status
|
|
57
|
-
result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
|
|
58
|
-
err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
|
|
59
|
-
# NotFound is raised only for valid responses from Telegram
|
|
60
|
-
raise NotFound, err_msg if 404 == status && result
|
|
61
|
-
raise Error, err_msg
|
|
62
|
-
end
|
|
12
|
+
# Raised for valid telegram response with 404 status code.
|
|
13
|
+
class NotFound < Error; end
|
|
63
14
|
|
|
64
|
-
|
|
65
|
-
answerInlineQuery
|
|
66
|
-
forwardMessage
|
|
67
|
-
getFile
|
|
68
|
-
getMe
|
|
69
|
-
getUpdates
|
|
70
|
-
getUserProfilePhotos
|
|
71
|
-
sendAudio
|
|
72
|
-
sendChatAction
|
|
73
|
-
sendDocument
|
|
74
|
-
sendLocation
|
|
75
|
-
sendMessage
|
|
76
|
-
sendPhoto
|
|
77
|
-
sendSticker
|
|
78
|
-
sendVideo
|
|
79
|
-
sendVoice
|
|
80
|
-
setWebhook
|
|
81
|
-
).each do |method|
|
|
82
|
-
define_method(method.underscore) { |*args| request(method, *args) }
|
|
83
|
-
end
|
|
15
|
+
module_function
|
|
84
16
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
17
|
+
def deprecation_0_15
|
|
18
|
+
@deprecation ||= begin
|
|
19
|
+
require 'active_support/deprecation'
|
|
20
|
+
ActiveSupport::Deprecation.new('0.15', 'Telegram::Bot')
|
|
21
|
+
end
|
|
90
22
|
end
|
|
91
23
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
24
|
+
autoload :Async, 'telegram/bot/async'
|
|
25
|
+
autoload :Client, 'telegram/bot/client'
|
|
26
|
+
autoload :ClientStub, 'telegram/bot/client_stub'
|
|
27
|
+
autoload :DebugClient, 'telegram/bot/debug_client'
|
|
28
|
+
autoload :Initializers, 'telegram/bot/initializers'
|
|
29
|
+
autoload :Middleware, 'telegram/bot/middleware'
|
|
30
|
+
autoload :RSpec, 'telegram/bot/rspec'
|
|
31
|
+
autoload :UpdatesController, 'telegram/bot/updates_controller'
|
|
32
|
+
autoload :UpdatesPoller, 'telegram/bot/updates_poller'
|
|
95
33
|
end
|
|
96
34
|
end
|
|
97
35
|
|
data/telegram-bot.gemspec
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
3
|
require 'telegram/bot/version'
|
|
@@ -10,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
|
10
9
|
spec.email = ['melentievm@gmail.com']
|
|
11
10
|
|
|
12
11
|
spec.summary = 'Library for building Telegram Bots with Rails integration'
|
|
13
|
-
spec.homepage = 'https://github.com/
|
|
12
|
+
spec.homepage = 'https://github.com/telegram-bot-rb/telegram-bot'
|
|
14
13
|
spec.license = 'MIT'
|
|
15
14
|
|
|
16
15
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
|
|
@@ -18,11 +17,15 @@ Gem::Specification.new do |spec|
|
|
|
18
17
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
19
18
|
spec.require_paths = ['lib']
|
|
20
19
|
|
|
20
|
+
spec.post_install_message = 'Breaking changes in v0.14! ' \
|
|
21
|
+
'See upgrade guide at https://github.com/telegram-bot-rb/telegram-bot/wiki/Upgrading-to-0.14'
|
|
22
|
+
|
|
21
23
|
spec.required_ruby_version = '~> 2.0'
|
|
22
24
|
|
|
23
|
-
spec.add_dependency '
|
|
24
|
-
spec.add_dependency '
|
|
25
|
+
spec.add_dependency 'actionpack', '>= 4.0', '< 6.1'
|
|
26
|
+
spec.add_dependency 'activesupport', '>= 4.0', '< 6.1'
|
|
25
27
|
spec.add_dependency 'httpclient', '~> 2.7'
|
|
26
|
-
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency 'bundler', '> 1.16'
|
|
27
30
|
spec.add_development_dependency 'rake', '~> 10.0'
|
|
28
31
|
end
|