telegram-bot 0.9.0 → 0.10.0
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +1 -1
- data/README.md +31 -15
- data/lib/telegram/bot/async.rb +1 -1
- data/lib/telegram/bot/botan/client_helpers.rb +1 -1
- data/lib/telegram/bot/config_methods.rb +2 -2
- data/lib/telegram/bot/rspec/integration.rb +49 -0
- data/lib/telegram/bot/updates_controller/rspec_helpers.rb +11 -3
- data/lib/telegram/bot/updates_poller.rb +3 -3
- data/lib/telegram/bot/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d17b0edbf259ea5ac3a2b5b156cb90e711c79766
|
4
|
+
data.tar.gz: 9d7258cc01ebc1265d8b00bc6d29780cb09163d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ed9bdce4d16621485a94f83be3dba04237119529bcbb62e2df9c5c796e5d1e91e290ec1f9460a0e5516af6195b2b8d3d824c080502dd4d9ec445341b7fbdc9f
|
7
|
+
data.tar.gz: e6e59d6585eabda015011994c794489638daddfa6b174633a1cdb48a5c585053447b699a72f9e3e579b3544bc9a55499a3aecdbc766f6350eb86c8d57a5ee11b
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -323,36 +323,52 @@ To stub all possible clients use `Telegram::Bot::ClientStub.stub_all!` before
|
|
323
323
|
initializing clients. Most likely you'll want something like this:
|
324
324
|
|
325
325
|
```ruby
|
326
|
+
# environments/test.rb
|
327
|
+
# Make sure to run it before defining routes or storing bot to some place in app!
|
328
|
+
Telegram.reset_bots
|
329
|
+
Telegram::Bot::ClientStub.stub_all!
|
330
|
+
|
331
|
+
# rails_helper.rb
|
326
332
|
RSpec.configure do |config|
|
327
333
|
# ...
|
328
|
-
Telegram.reset_bots
|
329
|
-
Telegram::Bot::ClientStub.stub_all!
|
330
334
|
config.after { Telegram.bot.reset }
|
331
335
|
# ...
|
332
336
|
end
|
333
337
|
```
|
334
338
|
|
335
|
-
There are
|
336
|
-
Check out `telegram/bot/updates_controller/rspec_helpers` and
|
337
|
-
`telegram/bot/updates_controller/testing`.
|
338
|
-
|
339
|
-
Built-in RSpec matchers will help you to write tests fast:
|
339
|
+
There are integration and controller contexts for RSpec and some built-in matchers:
|
340
340
|
|
341
341
|
```ruby
|
342
|
-
|
342
|
+
# spec/requests/telegram_webhooks_spec.rb
|
343
|
+
require 'telegram/bot/rspec/integration'
|
344
|
+
|
345
|
+
RSpec.describe TelegramWebhooksController, :telegram_bot do
|
346
|
+
# for old rspec add:
|
347
|
+
# include_context 'telegram/bot/integration'
|
348
|
+
|
349
|
+
describe '#start' do
|
350
|
+
subject { -> { dispatch_command :start } }
|
351
|
+
it { should respond_with_message 'Hi there!' }
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
# For controller specs use
|
356
|
+
require 'telegram/bot/updates_controller/rspec_helpers'
|
357
|
+
RSpec.describe TelegramWebhooksController, type: :telegram_bot_controller do
|
358
|
+
# for old rspec add:
|
359
|
+
# include_context 'telegram/bot/updates_controller'
|
360
|
+
end
|
361
|
+
|
362
|
+
# Matchers are available for custom specs:
|
363
|
+
include Telegram::Bot::RSpec::ClientMatchers
|
343
364
|
|
344
365
|
expect(&process_update).to send_telegram_message(bot, /msg regexp/, some: :option)
|
345
366
|
expect(&process_update).
|
346
367
|
to make_telegram_request(bot, :sendMessage, hash_including(text: 'msg text'))
|
347
|
-
|
348
|
-
# controller specs are even simplier:
|
349
|
-
describe '#start' do
|
350
|
-
subject { -> { dispatch_message '/start' } }
|
351
|
-
it { should respond_with_message(/Hello/) }
|
352
|
-
end
|
353
|
-
# See sample app for more examples.
|
354
368
|
```
|
355
369
|
|
370
|
+
See sample app for more examples.
|
371
|
+
|
356
372
|
### Deploying
|
357
373
|
|
358
374
|
Use `rake telegram:bot:set_webhook` to update webhook url for all configured bots.
|
data/lib/telegram/bot/async.rb
CHANGED
@@ -20,7 +20,7 @@ module Telegram
|
|
20
20
|
def bot_poller_mode?
|
21
21
|
return @bot_poller_mode if defined?(@bot_poller_mode)
|
22
22
|
@bot_poller_mode = ENV.fetch('BOT_POLLER_MODE') do
|
23
|
-
Rails.env.development? if defined?(Rails)
|
23
|
+
Rails.env.development? if defined?(Rails.env)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -47,7 +47,7 @@ module Telegram
|
|
47
47
|
# Can be overwritten with .bots_config=
|
48
48
|
def bots_config
|
49
49
|
@bots_config ||=
|
50
|
-
if defined?(Rails)
|
50
|
+
if defined?(Rails.application)
|
51
51
|
telegram_config = Rails.application.secrets[:telegram] || {}
|
52
52
|
(telegram_config['bots'] || {}).symbolize_keys.tap do |config|
|
53
53
|
default = telegram_config['bot']
|
@@ -0,0 +1,49 @@
|
|
1
|
+
RSpec.shared_context 'telegram/bot/integration' do
|
2
|
+
let(:bot) { Telegram.bot }
|
3
|
+
let(:from_id) { 123 }
|
4
|
+
let(:chat_id) { 456 }
|
5
|
+
let(:default_message_options) { {from: {id: from_id}, chat: {id: chat_id}} }
|
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
|
+
let(:clear_session?) { described_class.respond_to?(:session_store) }
|
17
|
+
before { described_class.session_store.clear if clear_session? }
|
18
|
+
|
19
|
+
include Telegram::Bot::RSpec::ClientMatchers
|
20
|
+
|
21
|
+
def dispatch(update)
|
22
|
+
if ActionPack::VERSION::MAJOR >= 5
|
23
|
+
post(controller_path, params: update.to_json, headers: request_headers)
|
24
|
+
else
|
25
|
+
post(controller_path, update.to_json, request_headers)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def dispatch_message(text, options = {})
|
30
|
+
dispatch message: default_message_options.merge(options).merge(text: text)
|
31
|
+
end
|
32
|
+
|
33
|
+
def dispatch_command(*args)
|
34
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
35
|
+
dispatch_message("/#{args.join ' '}", options)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Matcher to check response. Make sure to define `let(:chat_id)`.
|
39
|
+
def respond_with_message(expected)
|
40
|
+
raise 'Define chat_id to use respond_with_message' unless defined?(chat_id)
|
41
|
+
send_telegram_message(bot, expected, chat_id: chat_id)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec.configure do |config|
|
46
|
+
if config.respond_to?(:include_context)
|
47
|
+
config.include_context 'telegram/bot/integration', :telegram_bot
|
48
|
+
end
|
49
|
+
end
|
@@ -13,6 +13,9 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
|
13
13
|
let(:bot) { Telegram::Bot::ClientStub.new(bot_name) }
|
14
14
|
let(:bot_name) { 'bot' }
|
15
15
|
let(:session) { controller.send(:session) }
|
16
|
+
let(:from_id) { 123 }
|
17
|
+
let(:chat_id) { 456 }
|
18
|
+
let(:default_message_options) { {from: {id: from_id}, chat: {id: chat_id}} }
|
16
19
|
|
17
20
|
include Telegram::Bot::RSpec::ClientMatchers
|
18
21
|
|
@@ -20,9 +23,8 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
|
20
23
|
controller.dispatch_again(bot, update)
|
21
24
|
end
|
22
25
|
|
23
|
-
def dispatch_message(text, options =
|
24
|
-
|
25
|
-
update = build_update :message, options.merge(text: text)
|
26
|
+
def dispatch_message(text, options = {})
|
27
|
+
update = build_update :message, default_message_options.merge(options).merge(text: text)
|
26
28
|
dispatch bot, update
|
27
29
|
end
|
28
30
|
|
@@ -43,3 +45,9 @@ RSpec.shared_context 'telegram/bot/updates_controller' do
|
|
43
45
|
send_telegram_message(bot, expected, chat_id: chat_id)
|
44
46
|
end
|
45
47
|
end
|
48
|
+
|
49
|
+
RSpec.configure do |config|
|
50
|
+
if config.respond_to?(:include_context)
|
51
|
+
config.include_context 'telegram/bot/updates_controller', type: :telegram_bot_controller
|
52
|
+
end
|
53
|
+
end
|
@@ -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)
|
@@ -85,7 +85,7 @@ module Telegram
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
if defined?(Rails) && Rails.application.respond_to?(:reloader)
|
88
|
+
if defined?(Rails.application) && Rails.application.respond_to?(:reloader)
|
89
89
|
def reloading_code
|
90
90
|
Rails.application.reloader.wrap do
|
91
91
|
yield
|
data/lib/telegram/bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Melentiev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- lib/telegram/bot/routes_helper.rb
|
130
130
|
- lib/telegram/bot/rspec.rb
|
131
131
|
- lib/telegram/bot/rspec/client_matchers.rb
|
132
|
+
- lib/telegram/bot/rspec/integration.rb
|
132
133
|
- lib/telegram/bot/updates_controller.rb
|
133
134
|
- lib/telegram/bot/updates_controller/callback_query_context.rb
|
134
135
|
- lib/telegram/bot/updates_controller/instrumentation.rb
|