telegram-bot 0.12.0 → 0.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +33 -8
- data/lib/tasks/telegram-bot.rake +1 -1
- data/lib/telegram/bot/railtie.rb +1 -1
- data/lib/telegram/bot/updates_controller/message_context.rb +14 -9
- data/lib/telegram/bot/updates_controller/session.rb +12 -5
- data/lib/telegram/bot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68b890442fcebe77768635e83f83a3c17836fe1c
|
4
|
+
data.tar.gz: d63e9386c4bbed598c8708aedbea3c19e3dbc8f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0346c315cba86a652b0ab3914129ba426568790c5d18962b233cb60ef96b5c943562dec8cbaf348fe55d8b4592c7043987e30787707830d21791064be0d588a9
|
7
|
+
data.tar.gz: 6d643748c150f11087e29ed73e61cf979746d876461582055748dafa2aec2f4ffe5d9f52048158f2005e9c1e8c41abf6481feb88b09ac7c98549576ead446f50
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Unreleased
|
2
2
|
|
3
|
+
# 0.12.1
|
4
|
+
|
5
|
+
- Fix `set_webhook` rake task for async bots with self-issued certificates.
|
6
|
+
- Make `#session` raise error when store is not configured,
|
7
|
+
don't use Rails.cache as fallback for session_store.
|
8
|
+
- Allow use different sessions for MessageContext.
|
9
|
+
|
3
10
|
# 0.12.0
|
4
11
|
|
5
12
|
- New API methods and payload types (up to Bot API 3.2).
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Here is sample [telegram_bot_app](https://github.com/telegram-bot-rb/telegram_bo
|
|
24
24
|
with session, keyboards and inline queries.
|
25
25
|
Run it on your local machine in 1 minute!
|
26
26
|
|
27
|
-
And here is [app
|
27
|
+
And here is [app template](https://github.com/telegram-bot-rb/rails_template)
|
28
28
|
to generate clean app in seconds.
|
29
29
|
|
30
30
|
Examples and cookbook in [wiki](https://github.com/telegram-bot-rb/telegram-bot/wiki).
|
@@ -199,12 +199,33 @@ end
|
|
199
199
|
|
200
200
|
#### Session
|
201
201
|
|
202
|
-
|
202
|
+
This API is very close to ActiveController's session API, but works different
|
203
|
+
under the hood. Cookies can not be used to store session id or
|
204
|
+
whole session (like CookieStore does). So it uses key-value store and `session_key`
|
205
|
+
method to build identifier from update.
|
206
|
+
|
207
|
+
Store can be one of numerous `ActiveSupport::Cache` stores.
|
208
|
+
While `:file_store` is suitable for development and single-server deployments
|
209
|
+
without heavy load, it doesn't scale well. Key-value databases with persistance
|
210
|
+
like Redis are more appropriate for production use.
|
203
211
|
|
204
212
|
```ruby
|
205
|
-
#
|
213
|
+
# In rails app store can be configured in env files:
|
206
214
|
config.telegram_updates_controller.session_store = :redis_store, {expires_in: 1.month}
|
207
215
|
|
216
|
+
# In other app it can be done for all controllers with:
|
217
|
+
Telegram::Bot::UpdatesController.session_store = :redis_store, {expires_in: 1.month}
|
218
|
+
# or for specific one:
|
219
|
+
OneOfUpdatesController.session_store = :redis_store, {expires_in: 1.month}
|
220
|
+
```
|
221
|
+
|
222
|
+
Default session id is made from bot's username and `(from || chat)['id']`.
|
223
|
+
It means that session will be the same for updates from user in every chat,
|
224
|
+
and different for every user in the same group chat.
|
225
|
+
To change this behavior you can override `session_key` method, or even
|
226
|
+
define multiple sessions in single controller. For details see `Session` module.
|
227
|
+
|
228
|
+
```ruby
|
208
229
|
class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
209
230
|
include Telegram::Bot::UpdatesController::Session
|
210
231
|
# or just shortcut:
|
@@ -222,12 +243,16 @@ class Telegram::WebhookController < Telegram::Bot::UpdatesController
|
|
222
243
|
end
|
223
244
|
|
224
245
|
private
|
225
|
-
|
226
|
-
#
|
227
|
-
#
|
246
|
+
|
247
|
+
# In this case session will persist for user only in specific chat.
|
248
|
+
# Same user in other chat will have different session.
|
228
249
|
def session_key
|
229
|
-
#
|
230
|
-
|
250
|
+
"#{bot.username}:#{chat['id']}:#{from['id']}" if chat && from
|
251
|
+
end
|
252
|
+
|
253
|
+
# This session will be the same for all updates in chat.
|
254
|
+
def chat_session
|
255
|
+
@_chat_session ||= self.class.build_session(chat && "#{bot.username}:#{chat['id']}")
|
231
256
|
end
|
232
257
|
end
|
233
258
|
```
|
data/lib/tasks/telegram-bot.rake
CHANGED
@@ -21,7 +21,7 @@ namespace :telegram do
|
|
21
21
|
route_name = Telegram::Bot::RoutesHelper.route_name_for_bot(bot)
|
22
22
|
url = routes.send("#{route_name}_url")
|
23
23
|
puts "Setting webhook for #{key}..."
|
24
|
-
bot.set_webhook(url: url, certificate: cert)
|
24
|
+
bot.async(false) { bot.set_webhook(url: url, certificate: cert) }
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/telegram/bot/railtie.rb
CHANGED
@@ -18,7 +18,7 @@ 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
|
21
|
+
self.session_store = options.session_store if options.session_store
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -7,14 +7,11 @@ module Telegram
|
|
7
7
|
|
8
8
|
include Session
|
9
9
|
|
10
|
-
included do
|
11
|
-
# As we use before_action context is cleared anyway,
|
12
|
-
# no matter we used it or not.
|
13
|
-
singleton_class.send :attr_reader, :context_handlers, :context_to_action
|
14
|
-
@context_handlers = {}
|
15
|
-
end
|
16
|
-
|
17
10
|
module ClassMethods
|
11
|
+
def context_handlers
|
12
|
+
@_context_handlers ||= {}
|
13
|
+
end
|
14
|
+
|
18
15
|
# Registers handler for context.
|
19
16
|
#
|
20
17
|
# context_handler :rename do |*|
|
@@ -39,6 +36,8 @@ module Telegram
|
|
39
36
|
context_handlers[context] = action || context
|
40
37
|
end
|
41
38
|
|
39
|
+
attr_reader :context_to_action
|
40
|
+
|
42
41
|
# Use it to use context value as action name for all contexts
|
43
42
|
# which miss handlers.
|
44
43
|
# For security reasons it supports only action methods and will
|
@@ -59,10 +58,16 @@ module Telegram
|
|
59
58
|
# according to previous request.
|
60
59
|
attr_reader :context
|
61
60
|
|
61
|
+
# Controller may have multiple sessions, let it be possible
|
62
|
+
# to select session for message context.
|
63
|
+
def message_context_session
|
64
|
+
session
|
65
|
+
end
|
66
|
+
|
62
67
|
# Fetches context and finds handler for it. If message has new command,
|
63
68
|
# it has higher priority than contextual action.
|
64
69
|
def action_for_message
|
65
|
-
val =
|
70
|
+
val = message_context_session.delete(:context)
|
66
71
|
@context = val && val.to_sym
|
67
72
|
super || context && begin
|
68
73
|
handler = handler_for_context
|
@@ -72,7 +77,7 @@ module Telegram
|
|
72
77
|
|
73
78
|
# Save context for the next request.
|
74
79
|
def save_context(context)
|
75
|
-
|
80
|
+
message_context_session[:context] = context
|
76
81
|
end
|
77
82
|
|
78
83
|
def handler_for_context
|
@@ -8,19 +8,26 @@ module Telegram
|
|
8
8
|
module Session
|
9
9
|
extend ActiveSupport::Concern
|
10
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
|
+
|
11
21
|
def process_action(*)
|
12
22
|
super
|
13
23
|
ensure
|
14
|
-
session.commit
|
24
|
+
session.commit if @_session
|
15
25
|
end
|
16
26
|
|
17
27
|
protected
|
18
28
|
|
19
29
|
def session
|
20
|
-
@_session ||=
|
21
|
-
key = session_key
|
22
|
-
key ? SessionHash.new(self.class.session_store, key) : NullSessionHash.new
|
23
|
-
end
|
30
|
+
@_session ||= self.class.build_session(session_key)
|
24
31
|
end
|
25
32
|
|
26
33
|
def session_key
|
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.12.
|
4
|
+
version: 0.12.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max Melentiev
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|