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
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "actionpack", "~> 4.2.0"
|
|
6
|
+
gem "railties", "~> 4.2.0"
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "appraisal", "~> 2.2"
|
|
10
|
+
gem "pry", "~> 0.10.1"
|
|
11
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
12
|
+
gem "sdoc", "~> 0.4.1"
|
|
13
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
14
|
+
gem "rspec", "~> 3.5.0"
|
|
15
|
+
gem "rspec-its", "~> 1.1.0"
|
|
16
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
17
|
+
gem "rubocop", "~> 0.52.1"
|
|
18
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "actionpack", "~> 5.0.0"
|
|
6
|
+
gem "railties", "~> 5.0.0"
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "appraisal", "~> 2.2"
|
|
10
|
+
gem "pry", "~> 0.10.1"
|
|
11
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
12
|
+
gem "sdoc", "~> 0.4.1"
|
|
13
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
14
|
+
gem "rspec", "~> 3.5.0"
|
|
15
|
+
gem "rspec-its", "~> 1.1.0"
|
|
16
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
17
|
+
gem "rubocop", "~> 0.52.1"
|
|
18
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "actionpack", "~> 5.1.0"
|
|
6
|
+
gem "railties", "~> 5.1.0"
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "appraisal", "~> 2.2"
|
|
10
|
+
gem "pry", "~> 0.10.1"
|
|
11
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
12
|
+
gem "sdoc", "~> 0.4.1"
|
|
13
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
14
|
+
gem "rspec", "~> 3.5.0"
|
|
15
|
+
gem "rspec-its", "~> 1.1.0"
|
|
16
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
17
|
+
gem "rubocop", "~> 0.52.1"
|
|
18
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "actionpack", "~> 5.2.0"
|
|
6
|
+
gem "railties", "~> 5.2.0"
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "appraisal", "~> 2.2"
|
|
10
|
+
gem "pry", "~> 0.10.1"
|
|
11
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
12
|
+
gem "sdoc", "~> 0.4.1"
|
|
13
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
14
|
+
gem "rspec", "~> 3.5.0"
|
|
15
|
+
gem "rspec-its", "~> 1.1.0"
|
|
16
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
17
|
+
gem "rubocop", "~> 0.52.1"
|
|
18
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "actionpack", "~> 6.0.0"
|
|
6
|
+
gem "railties", "~> 6.0.0"
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "appraisal", "~> 2.2"
|
|
10
|
+
gem "pry", "~> 0.10.1"
|
|
11
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
12
|
+
gem "sdoc", "~> 0.4.1"
|
|
13
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
14
|
+
gem "rspec", "~> 3.5.0"
|
|
15
|
+
gem "rspec-its", "~> 1.1.0"
|
|
16
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
17
|
+
gem "rubocop", "~> 0.52.1"
|
|
18
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
gemspec path: "../"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
git "https://github.com/rails/rails.git" do
|
|
6
|
+
gem "actionpack", "~> 6.1.0.alpha"
|
|
7
|
+
gem "railties", "~> 6.1.0.alpha"
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
group :development do
|
|
11
|
+
gem "appraisal", "~> 2.2"
|
|
12
|
+
gem "pry", "~> 0.10.1"
|
|
13
|
+
gem "pry-byebug", "~> 3.2.0"
|
|
14
|
+
gem "sdoc", "~> 0.4.1"
|
|
15
|
+
gem "telegram-bot-types", "~> 0.6.0"
|
|
16
|
+
gem "rspec", "~> 3.5.0"
|
|
17
|
+
gem "rspec-its", "~> 1.1.0"
|
|
18
|
+
gem "rspec-rails", "~> 3.5.0"
|
|
19
|
+
gem "rubocop", "~> 0.52.1"
|
|
20
|
+
gem "coveralls", "~> 0.8.2", require: false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
gemspec path: "../"
|
data/lib/tasks/telegram-bot.rake
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
namespace :telegram do
|
|
2
2
|
namespace :bot do
|
|
3
|
-
desc 'Run poller'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
desc 'Run poller. It broadcasts Rails.logger to STDOUT in dev like `rails s` do. ' \
|
|
4
|
+
'Use LOG_TO_STDOUT to enable/disable broadcasting.'
|
|
5
|
+
task :poller do
|
|
6
|
+
ENV['BOT_POLLER_MODE'] = 'true'
|
|
7
|
+
Rake::Task['environment'].invoke
|
|
8
|
+
if ENV.fetch('LOG_TO_STDOUT') { Rails.env.development? }.present?
|
|
9
|
+
console = ActiveSupport::Logger.new(STDERR)
|
|
10
|
+
Rails.logger.extend ActiveSupport::Logger.broadcast console
|
|
11
|
+
end
|
|
7
12
|
Telegram::Bot::UpdatesPoller.start(ENV['BOT'].try!(:to_sym) || :default)
|
|
8
13
|
end
|
|
9
14
|
|
|
@@ -12,10 +17,11 @@ namespace :telegram do
|
|
|
12
17
|
routes = Rails.application.routes.url_helpers
|
|
13
18
|
cert_file = ENV['CERT']
|
|
14
19
|
cert = File.open(cert_file) if cert_file
|
|
15
|
-
Telegram.bots.
|
|
16
|
-
route_name = Telegram::RoutesHelper.route_name_for_bot(bot)
|
|
20
|
+
Telegram.bots.each do |key, bot|
|
|
21
|
+
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
|
17
22
|
url = routes.send("#{route_name}_url")
|
|
18
|
-
|
|
23
|
+
puts "Setting webhook for #{key}..."
|
|
24
|
+
bot.async(false) { bot.set_webhook(url: url, certificate: cert) }
|
|
19
25
|
end
|
|
20
26
|
end
|
|
21
27
|
end
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
# Telegram clients can perform requests in async way with
|
|
4
|
+
# any job adapter (ActiveJob by default). Using Rails you don't need any
|
|
5
|
+
# additional configuration. However you may want to enable async requests
|
|
6
|
+
# by default with `async: true` in `secrets.yml`.
|
|
7
|
+
#
|
|
8
|
+
# telegram:
|
|
9
|
+
# bots:
|
|
10
|
+
# chat_async:
|
|
11
|
+
# token: secret
|
|
12
|
+
# async: true # enable async mode for client
|
|
13
|
+
#
|
|
14
|
+
# Without Rails To start using async requests
|
|
15
|
+
# initialize client with `id` kwarg and make sure the client is
|
|
16
|
+
# accessible via `Teletgram.bots[id]` in job worker. Or just use
|
|
17
|
+
# `Telegram.bots_config=` for configuration.
|
|
18
|
+
#
|
|
19
|
+
# Being in async mode `#request` enqueues job instead to perform
|
|
20
|
+
# http request instead of performing it directly.
|
|
21
|
+
# Async behavior is controlled with `#async=` writer
|
|
22
|
+
# and can be enabled/disabled for the block with `#async`:
|
|
23
|
+
#
|
|
24
|
+
# client = Telegram::Bot::Client.new(**config, async: true)
|
|
25
|
+
# client.send_message(message)
|
|
26
|
+
# client.async(false) { client.send_message(other_one) }
|
|
27
|
+
#
|
|
28
|
+
# `#async=` sets global value for all threads,
|
|
29
|
+
# while `#async(val, &block)` is thread-safe.
|
|
30
|
+
#
|
|
31
|
+
# It can be set with custom job class or classname. By default it defines
|
|
32
|
+
# job classes for every client class, inherited from ApplicationRecord, which
|
|
33
|
+
# can be accessed via `.default_async_job`. You can integrate it with any
|
|
34
|
+
# other job provider by defining a class with `.perform_later(bot_id, *args)`
|
|
35
|
+
# method. See Async::Job for implemetation.
|
|
36
|
+
module Async
|
|
37
|
+
# Used to track missing key in a hash in local variable.
|
|
38
|
+
MISSING_VALUE = Object.new.freeze
|
|
39
|
+
|
|
40
|
+
module Job
|
|
41
|
+
class << self
|
|
42
|
+
def included(base)
|
|
43
|
+
base.singleton_class.send :attr_accessor, :client_class
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def perform(client_id, *args)
|
|
48
|
+
client = self.class.client_class.wrap(client_id.to_sym)
|
|
49
|
+
client.async(false) { client.request(*args) }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
module ClassMethods
|
|
54
|
+
def default_async_job
|
|
55
|
+
@default_async_job ||= begin
|
|
56
|
+
begin
|
|
57
|
+
ApplicationJob
|
|
58
|
+
rescue NameError
|
|
59
|
+
raise 'Define ApplicationJob class or setup #async= with custom job class'
|
|
60
|
+
end
|
|
61
|
+
klass = Class.new(ApplicationJob) { include Job }
|
|
62
|
+
klass.client_class = self
|
|
63
|
+
const_set(:AsyncJob, klass)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# This is used in specs.
|
|
68
|
+
def default_async_job=(val)
|
|
69
|
+
@default_async_job = val
|
|
70
|
+
remove_const(:AsyncJob) if const_defined?(:AsyncJob, false)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Prepares argments for async job. ActiveJob doesn't support
|
|
74
|
+
# Symbol in argumens. Also we can encode json bodies only once here,
|
|
75
|
+
# so it would not be unnecessarily serialized-deserialized.
|
|
76
|
+
#
|
|
77
|
+
# This is stub method, which returns input. Every client class
|
|
78
|
+
# must prepare args itself.
|
|
79
|
+
def prepare_async_args(*args)
|
|
80
|
+
args
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Returns default_async_job if `true` is given,
|
|
84
|
+
# treats String as a constant name, or bypasses any other values.
|
|
85
|
+
def prepare_async_val(val)
|
|
86
|
+
case val
|
|
87
|
+
when true then default_async_job
|
|
88
|
+
when String then Object.const_get(val)
|
|
89
|
+
else val
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
class << self
|
|
95
|
+
def prepended(base)
|
|
96
|
+
base.extend(ClassMethods)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Transforms symbols to strings in hash values.
|
|
100
|
+
def prepare_hash(hash)
|
|
101
|
+
return hash unless hash.is_a?(Hash)
|
|
102
|
+
hash = hash.dup
|
|
103
|
+
hash.each { |key, val| hash[key] = val.to_s if val.is_a?(Symbol) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Thread-local hash to store async config for every client.
|
|
107
|
+
def thread_store
|
|
108
|
+
Thread.current[:telegram_bot_async] ||= {}
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
attr_reader :id
|
|
113
|
+
|
|
114
|
+
def initialize(*, id: nil, async: nil, **options)
|
|
115
|
+
@id = id
|
|
116
|
+
self.async = async
|
|
117
|
+
super
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Sets default async value for all threads.
|
|
121
|
+
# Uses `self.class.prepare_async_val` to prepare value.
|
|
122
|
+
def async=(val)
|
|
123
|
+
@async = self.class.prepare_async_val(val)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Sets async value in a thread-safe way for the block.
|
|
127
|
+
# Uses `self.class.prepare_async_val` to prepare value.
|
|
128
|
+
#
|
|
129
|
+
# If no block is given returns previously set value or the global one,
|
|
130
|
+
# set by #async=.
|
|
131
|
+
def async(val = true)
|
|
132
|
+
thread_key = object_id
|
|
133
|
+
thread_store = Async.thread_store
|
|
134
|
+
return thread_store.fetch(thread_key) { @async } unless block_given?
|
|
135
|
+
begin
|
|
136
|
+
old_val = thread_store.fetch(thread_key) { MISSING_VALUE }
|
|
137
|
+
thread_store[thread_key] = self.class.prepare_async_val(val)
|
|
138
|
+
yield
|
|
139
|
+
ensure
|
|
140
|
+
if MISSING_VALUE == old_val
|
|
141
|
+
thread_store.delete(thread_key)
|
|
142
|
+
else
|
|
143
|
+
thread_store[thread_key] = old_val
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# Uses job if #async is set.
|
|
149
|
+
def request(*args)
|
|
150
|
+
job_class = async
|
|
151
|
+
return super unless job_class
|
|
152
|
+
raise 'Can not enqueue job without client id' unless id
|
|
153
|
+
job_class.perform_later(id.to_s, *self.class.prepare_async_args(*args))
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
|
2
|
+
|
|
3
|
+
module Telegram
|
|
4
|
+
module Bot
|
|
5
|
+
class Client
|
|
6
|
+
module ApiHelper
|
|
7
|
+
METHODS_LIST_FILE = File.expand_path('../api_methods.txt', __FILE__)
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
def methods_list(file = METHODS_LIST_FILE)
|
|
11
|
+
File.read(file).lines.
|
|
12
|
+
map(&:strip).
|
|
13
|
+
reject { |x| x.empty? || x.start_with?('#') }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Defines method with underscored name to post to specific endpoint:
|
|
17
|
+
#
|
|
18
|
+
# define_method :getMe
|
|
19
|
+
# # defines #get_me
|
|
20
|
+
def define_helpers(*list)
|
|
21
|
+
list.map(&:to_s).each do |method|
|
|
22
|
+
define_method(method.underscore) { |*args| request(method, *args) }
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
define_helpers(*methods_list)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Generated with bin/fetch-telegram-methods
|
|
2
|
+
# Bot API 4.4
|
|
3
|
+
|
|
4
|
+
getUpdates
|
|
5
|
+
setWebhook
|
|
6
|
+
deleteWebhook
|
|
7
|
+
getWebhookInfo
|
|
8
|
+
|
|
9
|
+
getMe
|
|
10
|
+
sendMessage
|
|
11
|
+
forwardMessage
|
|
12
|
+
sendPhoto
|
|
13
|
+
sendAudio
|
|
14
|
+
sendDocument
|
|
15
|
+
sendVideo
|
|
16
|
+
sendAnimation
|
|
17
|
+
sendVoice
|
|
18
|
+
sendVideoNote
|
|
19
|
+
sendMediaGroup
|
|
20
|
+
sendLocation
|
|
21
|
+
editMessageLiveLocation
|
|
22
|
+
stopMessageLiveLocation
|
|
23
|
+
sendVenue
|
|
24
|
+
sendContact
|
|
25
|
+
sendPoll
|
|
26
|
+
sendChatAction
|
|
27
|
+
getUserProfilePhotos
|
|
28
|
+
getFile
|
|
29
|
+
kickChatMember
|
|
30
|
+
unbanChatMember
|
|
31
|
+
restrictChatMember
|
|
32
|
+
promoteChatMember
|
|
33
|
+
setChatPermissions
|
|
34
|
+
exportChatInviteLink
|
|
35
|
+
setChatPhoto
|
|
36
|
+
deleteChatPhoto
|
|
37
|
+
setChatTitle
|
|
38
|
+
setChatDescription
|
|
39
|
+
pinChatMessage
|
|
40
|
+
unpinChatMessage
|
|
41
|
+
leaveChat
|
|
42
|
+
getChat
|
|
43
|
+
getChatAdministrators
|
|
44
|
+
getChatMembersCount
|
|
45
|
+
getChatMember
|
|
46
|
+
setChatStickerSet
|
|
47
|
+
deleteChatStickerSet
|
|
48
|
+
answerCallbackQuery
|
|
49
|
+
|
|
50
|
+
editMessageText
|
|
51
|
+
editMessageCaption
|
|
52
|
+
editMessageMedia
|
|
53
|
+
editMessageReplyMarkup
|
|
54
|
+
stopPoll
|
|
55
|
+
deleteMessage
|
|
56
|
+
|
|
57
|
+
sendSticker
|
|
58
|
+
getStickerSet
|
|
59
|
+
uploadStickerFile
|
|
60
|
+
createNewStickerSet
|
|
61
|
+
addStickerToSet
|
|
62
|
+
setStickerPositionInSet
|
|
63
|
+
deleteStickerFromSet
|
|
64
|
+
|
|
65
|
+
answerInlineQuery
|
|
66
|
+
|
|
67
|
+
sendInvoice
|
|
68
|
+
answerShippingQuery
|
|
69
|
+
answerPreCheckoutQuery
|
|
70
|
+
|
|
71
|
+
setPassportDataErrors
|
|
72
|
+
|
|
73
|
+
sendGame
|
|
74
|
+
setGameScore
|
|
75
|
+
getGameHighScores
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'telegram/bot/client/api_helper'
|
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
|
3
|
+
|
|
4
|
+
module Telegram
|
|
5
|
+
module Bot
|
|
6
|
+
class Client
|
|
7
|
+
# Actions with type-casted results. Install `telegram-bot-types` gem first.
|
|
8
|
+
module TypedResponse
|
|
9
|
+
# First we define methods for every available api method to return `result`
|
|
10
|
+
# field instead of object.
|
|
11
|
+
ApiHelper.methods_list.each do |method|
|
|
12
|
+
define_method(method.to_s.underscore) do |*args|
|
|
13
|
+
super(*args)['result']
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# And then override some of them which has castable results.
|
|
18
|
+
Types::Response::WRAPPED_METHODS.each do |method, type|
|
|
19
|
+
define_method(method.to_s.underscore) do |*args|
|
|
20
|
+
Types::Response.wrap(super(*args)['result'], type)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'httpclient'
|
|
3
|
+
|
|
4
|
+
module Telegram
|
|
5
|
+
module Bot
|
|
6
|
+
class Client
|
|
7
|
+
URL_TEMPLATE = 'https://api.telegram.org/bot%<token>s/'.freeze
|
|
8
|
+
|
|
9
|
+
autoload :TypedResponse, 'telegram/bot/client/typed_response'
|
|
10
|
+
extend Initializers
|
|
11
|
+
prepend Async
|
|
12
|
+
include DebugClient
|
|
13
|
+
|
|
14
|
+
require 'telegram/bot/client/api_helper'
|
|
15
|
+
include ApiHelper
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
def by_id(id)
|
|
19
|
+
Telegram.bots[id]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Prepend TypedResponse module.
|
|
23
|
+
def typed_response!
|
|
24
|
+
prepend TypedResponse
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Encodes nested hashes as json.
|
|
28
|
+
def prepare_body(body)
|
|
29
|
+
body = body.dup
|
|
30
|
+
body.each do |k, val|
|
|
31
|
+
body[k] = val.to_json if val.is_a?(Hash) || val.is_a?(Array)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def prepare_async_args(action, body = {})
|
|
36
|
+
[action.to_s, Async.prepare_hash(prepare_body(body))]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def error_for_response(response)
|
|
40
|
+
result = JSON.parse(response.body) rescue nil # rubocop:disable RescueModifier
|
|
41
|
+
return Error.new(response.reason) unless result
|
|
42
|
+
message = result['description'] || '-'
|
|
43
|
+
# This errors are raised only for valid responses from Telegram
|
|
44
|
+
case response.status
|
|
45
|
+
when 403 then Forbidden.new(message)
|
|
46
|
+
when 404 then NotFound.new(message)
|
|
47
|
+
else Error.new("#{response.reason}: #{message}")
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
attr_reader :client, :token, :username, :base_uri
|
|
53
|
+
|
|
54
|
+
def initialize(token = nil, username = nil, **options)
|
|
55
|
+
@client = HTTPClient.new
|
|
56
|
+
@token = token || options[:token]
|
|
57
|
+
@username = username || options[:username]
|
|
58
|
+
@base_uri = format(URL_TEMPLATE, token: self.token)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def request(action, body = {})
|
|
62
|
+
response = http_request("#{base_uri}#{action}", self.class.prepare_body(body))
|
|
63
|
+
raise self.class.error_for_response(response) if response.status >= 300
|
|
64
|
+
JSON.parse(response.body)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Endpoint for low-level request. For easy host highjacking & instrumentation.
|
|
68
|
+
# Params are not used directly but kept for instrumentation purpose.
|
|
69
|
+
# You probably don't want to use this method directly.
|
|
70
|
+
def http_request(uri, body)
|
|
71
|
+
client.post(uri, body)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def inspect
|
|
75
|
+
"#<#{self.class.name}##{object_id}(#{@username})>"
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
# Stubbed client for tests. Saves all requests into #requests hash.
|
|
4
|
+
class ClientStub < Client
|
|
5
|
+
attr_reader :requests
|
|
6
|
+
|
|
7
|
+
module StubbedConstructor
|
|
8
|
+
def new(*args)
|
|
9
|
+
if self == ClientStub || !ClientStub.stub_all?
|
|
10
|
+
super
|
|
11
|
+
else
|
|
12
|
+
ClientStub.new(*args)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# Any call to Client.new will return ClientStub instance when `enabled` is true.
|
|
19
|
+
# Can be used with a block.
|
|
20
|
+
def stub_all!(enabled = true)
|
|
21
|
+
Client.extend(StubbedConstructor) unless Client < StubbedConstructor
|
|
22
|
+
return @_stub_all = enabled unless block_given?
|
|
23
|
+
begin
|
|
24
|
+
old = @_stub_all
|
|
25
|
+
stub_all!(enabled)
|
|
26
|
+
yield
|
|
27
|
+
ensure
|
|
28
|
+
stub_all!(old)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def stub_all?
|
|
33
|
+
@_stub_all
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def initialize(token = nil, username = nil, **options)
|
|
38
|
+
@token = token || options[:token]
|
|
39
|
+
@username = username || options[:username] || token
|
|
40
|
+
reset
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def reset
|
|
44
|
+
@requests = Hash.new { |h, k| h[k] = [] }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def request(action, body = {})
|
|
48
|
+
requests[action.to_sym] << body
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'active_support/core_ext/hash/keys'
|
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
|
3
|
+
|
|
4
|
+
module Telegram
|
|
5
|
+
module Bot
|
|
6
|
+
module ConfigMethods
|
|
7
|
+
# Overwrite config.
|
|
8
|
+
attr_writer :bots_config
|
|
9
|
+
|
|
10
|
+
# Keep this setting here, so we can avoid loading Bot::UpdatesPoller
|
|
11
|
+
# when polling is disabled.
|
|
12
|
+
attr_writer :bot_poller_mode
|
|
13
|
+
|
|
14
|
+
# It just tells routes helpers whether to add routed bots to
|
|
15
|
+
# Bot::UpdatesPoller, so their config will be available by bot key in
|
|
16
|
+
# Bot::UpdatesPoller.start.
|
|
17
|
+
#
|
|
18
|
+
# It's enabled by default in Rails dev environment and `rake telegram:bot:poller`
|
|
19
|
+
# task. Use `BOT_POLLER_MODE=true` envvar to set it manually.
|
|
20
|
+
def bot_poller_mode?
|
|
21
|
+
return @bot_poller_mode if defined?(@bot_poller_mode)
|
|
22
|
+
@bot_poller_mode = ENV.fetch('BOT_POLLER_MODE') do
|
|
23
|
+
Rails.env.development? if defined?(Rails.env)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Hash of bots made with bots_config.
|
|
28
|
+
def bots
|
|
29
|
+
@bots ||= bots_config.each_with_object({}) do |(id, config), h|
|
|
30
|
+
h[id] = Client.wrap(config, id: id)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Default bot.
|
|
35
|
+
def bot
|
|
36
|
+
@bot ||= bots.fetch(:default) do
|
|
37
|
+
raise 'Default bot is not configured.' \
|
|
38
|
+
' Add :default to bots_config' \
|
|
39
|
+
' or use telegram.bot/telegram.bots.default section in secrets.yml.'
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Returns config for .bots method. By default uses `telegram['bots']` section
|
|
44
|
+
# from `secrets.yml` merging `telegram['bot']` at `:default` key.
|
|
45
|
+
#
|
|
46
|
+
# Can be overwritten with .bots_config=
|
|
47
|
+
def bots_config
|
|
48
|
+
@bots_config ||=
|
|
49
|
+
if defined?(Rails.application)
|
|
50
|
+
app = Rails.application
|
|
51
|
+
store = app.credentials[:telegram] if app.respond_to?(:credentials)
|
|
52
|
+
store ||= app.secrets[:telegram] if app.respond_to?(:secrets)
|
|
53
|
+
store ||= {}
|
|
54
|
+
store = store.with_indifferent_access
|
|
55
|
+
store.fetch(:bots, {}).symbolize_keys.tap do |config|
|
|
56
|
+
default = store[:bot]
|
|
57
|
+
config[:default] = default if default
|
|
58
|
+
end
|
|
59
|
+
else
|
|
60
|
+
{}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Resets all cached bots and their configs.
|
|
65
|
+
def reset_bots
|
|
66
|
+
@bots = nil
|
|
67
|
+
@bot = nil
|
|
68
|
+
@bots_config = nil
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Telegram
|
|
2
|
+
module Bot
|
|
3
|
+
module DebugClient
|
|
4
|
+
def debug!(dev = STDOUT)
|
|
5
|
+
if block_given?
|
|
6
|
+
begin
|
|
7
|
+
old_dev = client.debug_dev
|
|
8
|
+
client.debug_dev = dev
|
|
9
|
+
yield
|
|
10
|
+
ensure
|
|
11
|
+
client.debug_dev = old_dev
|
|
12
|
+
end
|
|
13
|
+
else
|
|
14
|
+
client.debug_dev = dev
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def debug_off!
|
|
19
|
+
client.debug_dev = nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|