telegram_bot_middleware 0.3.0 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19dee6688c9ccd4a5f152303e79a438a3ada2355
4
- data.tar.gz: ccd54a63a55513074a270f07fbcd0018a6a2deca
3
+ metadata.gz: 870191d85926103616be33ea31cb39dc8bb1d272
4
+ data.tar.gz: bc550f86c1c106ae526d18d4e5afaa31b91fdcf0
5
5
  SHA512:
6
- metadata.gz: 6ff282369b82fd2ca2f8ee923530f0b7a6d1c1b7ab0ce5dc3a675efef887bf8e1b59d008368aadce034593d69376b89bb3f8b0d6cfcc9779d6c40f025019935e
7
- data.tar.gz: 8fcdba4d61f4d1c499aa250f10ce4ef4e3af4ad250f3ea3409353bd0dd0d561a75917d98d39ea87d3ef4afbccef395d59b75ea1f060cce575dcbd93964816460
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
- * To obtain a token follow the instructions in [telegram bot api](https://core.telegram.org/bots#botfather).
34
- * The host is the address where the script is running, for example during development could be http://127.0.0.1:9292.
35
- * The get_updates 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.
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:
@@ -1,9 +1,13 @@
1
1
  require_relative 'example_init'
2
2
 
3
- calc_string = ''
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
- calc_string = ''
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
- calc_string += cmd
18
+ session[:result] += cmd
15
19
  end
16
20
 
17
21
  get '/=' do
18
22
  {
19
- text: eval(calc_string).to_s,
23
+ text: eval(session[:result]).to_s,
20
24
  reply_markup: {hide_keyboard: true}
21
25
  }
22
26
  end
@@ -1,3 +1,3 @@
1
1
  class TelegramBotMiddleware
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  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
- # TODO use update(hash) { |name, old_value, new_value| }
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.0
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-16 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake