telegram_bot_middleware 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/examples/sinatra/calc.rb +8 -4
- data/lib/telegram_bot_middleware/version.rb +1 -1
- data/lib/telegram_bot_middleware.rb +13 -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: 870191d85926103616be33ea31cb39dc8bb1d272
|
4
|
+
data.tar.gz: bc550f86c1c106ae526d18d4e5afaa31b91fdcf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42cf0b10bfb844b86c821140e64f35271e0f7ee37fe4f4a818cf14e5040db05fb4f418f70a371d2cae5f9e4f1c529ce4524f85e085a8be701b8a81bd748ce4f3
|
7
|
+
data.tar.gz: 2de4247868fb47150d373d91254ad41fa964e519303e5287243b278744f03cf07fcd77794eba22a8f2d41665ca2189e957ec4590cb0f7afc431275dc61bd1c86
|
data/README.md
CHANGED
@@ -26,13 +26,16 @@ require 'telegram_bot_middleware'
|
|
26
26
|
use TelegramBotMiddleware do |config|
|
27
27
|
config.token = '<TELEGRAM_TOKEN>'
|
28
28
|
config.host = '<HOST>'
|
29
|
-
config.get_updates = :polling or :webhook
|
30
29
|
end
|
31
30
|
```
|
32
31
|
|
33
|
-
|
34
|
-
*
|
35
|
-
*
|
32
|
+
### Config options:
|
33
|
+
* token (required): to obtain the token follow the instructions in [telegram bot api](https://core.telegram.org/bots#botfather).
|
34
|
+
* host (required): is the address where the script is running, for example during development could be http://127.0.0.1:9292.
|
35
|
+
* get_updates (optional, default is :polling) params specify how to get incoming messages from telegram, can be :polling or :webhook, look at the [telegram bot api](https://core.telegram.org/bots/api#getupdates) for details.
|
36
|
+
* connection_pool_size (optional, default is 2):
|
37
|
+
* connection_keep_alive (optional, default is 30):
|
38
|
+
* connection_force_retry (optional, default is true):
|
36
39
|
|
37
40
|
## Usage
|
38
41
|
|
@@ -139,6 +142,9 @@ Sometimes can be useful to return more than one message, do this is very simple,
|
|
139
142
|
```
|
140
143
|
In this case the bot will send an image, a message and a location.
|
141
144
|
|
145
|
+
### Session and cookies
|
146
|
+
The middleware supports the standard sessions variables, that are stored as a cookie and the values are valid for the given chat, see the [calculator sample](https://github.com/MirkoMignini/telegram_bot_middleware/blob/master/examples/sinatra/calc.rb).
|
147
|
+
|
142
148
|
## Examples
|
143
149
|
|
144
150
|
To run an example call:
|
data/examples/sinatra/calc.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
require_relative 'example_init'
|
2
2
|
|
3
|
-
|
3
|
+
use Rack::Session::Cookie, :key => 'rack.session',
|
4
|
+
:domain => 'foo.com',
|
5
|
+
:path => '/',
|
6
|
+
:expire_after => 2592000, # In seconds
|
7
|
+
:secret => 'some_secret'
|
4
8
|
|
5
9
|
get %r{calc/?$}i do
|
6
|
-
|
10
|
+
session[:result] = ''
|
7
11
|
{
|
8
12
|
text: "Let's calc!",
|
9
13
|
reply_markup: {keyboard: [%w(7 8 9 *), %w(4 5 6 /), %w(1 2 3 -), %w(0 . = +)], resize_keyboard: true, one_time_keyboard: false, selective: false}
|
@@ -11,12 +15,12 @@ get %r{calc/?$}i do
|
|
11
15
|
end
|
12
16
|
|
13
17
|
get %r{(0|1|2|3|4|5|6|7|8|9|\*|\/|-|\.|\+)$} do |cmd|
|
14
|
-
|
18
|
+
session[:result] += cmd
|
15
19
|
end
|
16
20
|
|
17
21
|
get '/=' do
|
18
22
|
{
|
19
|
-
text: eval(
|
23
|
+
text: eval(session[:result]).to_s,
|
20
24
|
reply_markup: {hide_keyboard: true}
|
21
25
|
}
|
22
26
|
end
|
@@ -13,6 +13,10 @@ class TelegramBotMiddleware
|
|
13
13
|
def initialize(app, &block)
|
14
14
|
# save the app var
|
15
15
|
@app = app
|
16
|
+
|
17
|
+
# local cookies hash
|
18
|
+
@cookies = Hash.new
|
19
|
+
|
16
20
|
@env = nil
|
17
21
|
|
18
22
|
# create the config and populate passing do the block function
|
@@ -22,6 +26,8 @@ class TelegramBotMiddleware
|
|
22
26
|
self.class.persistent_connection_adapter pool_size: (@config.connection_pool_size || 2),
|
23
27
|
keep_alive: (@config.connection_keep_alive || 30),
|
24
28
|
force_retry: (@config.connection_force_retry || true)
|
29
|
+
|
30
|
+
@config.get_updates ||= :polling
|
25
31
|
|
26
32
|
# setup webhook
|
27
33
|
if @config.webhook.nil?
|
@@ -122,13 +128,19 @@ class TelegramBotMiddleware
|
|
122
128
|
@env['QUERY_STRING'] = query_string
|
123
129
|
@env['REQUEST_METHOD'] = 'GET'
|
124
130
|
@env['REQUEST_URI'] = "https://#{req.host}#{path}"
|
125
|
-
|
131
|
+
|
132
|
+
# if in cache a cookie for this chat was present add to the header
|
133
|
+
@env['HTTP_COOKIE'] = @cookies[params.message.chat.id] if @cookies.include?(params.message.chat.id)
|
126
134
|
|
127
135
|
# call the rack stack
|
128
136
|
status, headers, body = @app.call(@env)
|
129
137
|
|
138
|
+
# try to send to telegram only if no errors
|
130
139
|
if status == 200 or status == '200'
|
131
140
|
|
141
|
+
# if the call setted a cookie save to local cache
|
142
|
+
@cookies[params.message.chat.id] = headers['Set-Cookie'] if headers.include?('Set-Cookie')
|
143
|
+
|
132
144
|
case headers['Content-Type'].split(';').first
|
133
145
|
when 'text/html', 'application/json'
|
134
146
|
if body.is_a? Hash
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram_bot_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mirko Mignini
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|