telegram_workflow 1.2.0 → 1.6.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/.github/workflows/ruby.yml +41 -0
- data/.gitignore +1 -0
- data/Gemfile +3 -1
- data/README.md +5 -268
- data/lib/telegram_workflow.rb +2 -1
- data/lib/telegram_workflow/client.rb +54 -20
- data/lib/telegram_workflow/config.rb +3 -1
- data/lib/telegram_workflow/errors.rb +12 -0
- data/lib/telegram_workflow/input_file.rb +2 -0
- data/lib/telegram_workflow/version.rb +1 -1
- data/lib/telegram_workflow/workflow.rb +3 -0
- data/telegram_workflow.gemspec +1 -1
- metadata +9 -8
- data/.travis.yml +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fd629dee17a8e0caa932b91ad3cdccda3eba9e2cbd33dd15c57f38fcbb9aca7
|
4
|
+
data.tar.gz: e6ae112b2d49d6e9539549254605cc1ee8a5c57ac1421335195b531dd5ebde12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '058ad9c7866ccd36ae2f5adc9a8c52c51364ecb84e1d0c3f7d39fd514f3868ba50745ff384485ab541988bbb87a1a725691c06d28b28f121cb7dfd36bfb8f3bb'
|
7
|
+
data.tar.gz: a5f3dc285f8fa706be2f9fad08d3cc192e6e90c256204e01befdce1ed547d9400de1a5e132cc750adbf369f18b064e019368df0b352836cc7bb3ecc23fd57b82
|
@@ -0,0 +1,41 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
strategy:
|
10
|
+
matrix:
|
11
|
+
ruby-version: ['2.4', '2.5', '2.6', '2.7']
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v2
|
15
|
+
- name: Install packages
|
16
|
+
run: |
|
17
|
+
sudo apt update -qy
|
18
|
+
sudo apt install libdb-dev
|
19
|
+
- name: Set up Ruby
|
20
|
+
uses: ruby/setup-ruby@v1
|
21
|
+
with:
|
22
|
+
ruby-version: ${{ matrix.ruby-version }}
|
23
|
+
bundler-cache: true
|
24
|
+
- name: Run tests
|
25
|
+
run: bundle exec rake
|
26
|
+
- name: Coveralls Parallel
|
27
|
+
uses: coverallsapp/github-action@master
|
28
|
+
with:
|
29
|
+
github-token: ${{ secrets.github_token }}
|
30
|
+
flag-name: run-${{ matrix.ruby-version }}
|
31
|
+
parallel: true
|
32
|
+
|
33
|
+
finish:
|
34
|
+
needs: test
|
35
|
+
runs-on: ubuntu-latest
|
36
|
+
steps:
|
37
|
+
- name: Coveralls Finished
|
38
|
+
uses: coverallsapp/github-action@master
|
39
|
+
with:
|
40
|
+
github-token: ${{ secrets.github_token }}
|
41
|
+
parallel-finished: true
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# TelegramWorkflow
|
2
2
|
|
3
|
-
|
3
|
+

|
4
4
|
[](https://coveralls.io/github/rsamoilov/telegram_workflow?branch=master)
|
5
5
|
[](https://codeclimate.com/github/rsamoilov/telegram_workflow/maintainability)
|
6
6
|
|
@@ -110,276 +110,13 @@ end
|
|
110
110
|
Here you can see an example of redirection to another action.
|
111
111
|
Having a bot logic split over such small actions improves code maintanability and allows to follow SRP.
|
112
112
|
|
113
|
-
##
|
113
|
+
## Documentation
|
114
114
|
|
115
|
-
|
115
|
+
Please see the [TelegramWorkflow wiki](https://github.com/rsamoilov/telegram_workflow/wiki) for more detailed documentation.
|
116
116
|
|
117
|
-
|
118
|
-
-------|------------
|
119
|
-
[params](README.md#params) | Instance of `TelegramWorkflow::Params`.
|
120
|
-
[client](README.md#client) | Instance of `TelegramWorkflow::Client`. Can be [customized](README.md#client-customization).
|
121
|
-
[session](README.md#session) | Persistent store to keep session data. Instance of `Hash`.
|
122
|
-
[flash](README.md#flash) | Temporary store to keep some data between different steps. Instance of `Hash`.
|
117
|
+
## Example
|
123
118
|
|
124
|
-
|
125
|
-
|
126
|
-
### params
|
127
|
-
|
128
|
-
The `params` object encapsulates the logic to parse Telegram params.
|
129
|
-
It implements useful methods, like `message_text`, `callback_data` or `deep_link_payload` to fetch user submitted data from the params.
|
130
|
-
|
131
|
-
### client
|
132
|
-
|
133
|
-
This is an instance of `TelegramWorkflow::Client` class, which implements a complete Telegram Bot API.
|
134
|
-
The methods to access the API are called after raw Telegram API methods.
|
135
|
-
For example, if you needed to call a [sendLocation](https://core.telegram.org/bots/api#sendlocation) method, you would use the following code:
|
136
|
-
|
137
|
-
```ruby
|
138
|
-
client.send_location latitude: 40.748, longitude: -73.985, live_period: 120
|
139
|
-
```
|
140
|
-
|
141
|
-
`chat_id` parameter should be omitted.
|
142
|
-
|
143
|
-
### session
|
144
|
-
|
145
|
-
This is a persistent store to save the data associated with a user, e.g. current user's id, some settings or anything you would store in a session in a regular web application.
|
146
|
-
|
147
|
-
### flash
|
148
|
-
|
149
|
-
This is a temporary store to save the data between the steps. The data persists while redirecting between the steps, but **gets deleted automatically when redirecting to another action**.
|
150
|
-
|
151
|
-
### redirect_to(action_or_class, flash_params = {})
|
152
|
-
|
153
|
-
As you already know, this function allows to build complex workflows by redirecting between actions and steps.
|
154
|
-
The function expects either a symbol or instance of `Class` as a first argument. Passing a symbol will redirect to another step inside the current action. Passing instance of `Class` will redirect to another action.
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
# redirect to a step
|
158
|
-
redirect_to :suggest
|
159
|
-
|
160
|
-
# redirect to an action
|
161
|
-
redirect_to RateMovie
|
162
|
-
```
|
163
|
-
|
164
|
-
Sometimes you will need to share some data between the actions. You could use `session` for this, but a more appropriate solution would be to have `redirect_to` function to preserve the flash between actions.
|
165
|
-
Check out this example:
|
166
|
-
|
167
|
-
```ruby
|
168
|
-
class AskForBirthday < TelegramWorkflow::Action
|
169
|
-
def initial
|
170
|
-
on_redirect do
|
171
|
-
client.send_message text: "What year is your birthday?"
|
172
|
-
end
|
173
|
-
|
174
|
-
on_message do
|
175
|
-
birthday = params.message_text.to_i
|
176
|
-
redirect_to DisplayAge, birthday: birthday
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
class DisplayAge < TelegramWorkflow::Action
|
182
|
-
def initial
|
183
|
-
on_redirect do
|
184
|
-
age = Date.today.year - flash[:birthday]
|
185
|
-
client.send_message text: "You are #{age}!"
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
```
|
190
|
-
|
191
|
-
You can see that despite the fact that flash is being cleared when redirecting to another action, passing `birthday` value to the `redirect_to` call made it accessible via flash in the action we redirected to.
|
192
|
-
|
193
|
-
## Configuration
|
194
|
-
|
195
|
-
Configure the gem using the `TelegramWorkflow.configure` call.
|
196
|
-
The two required parameters are `start_action` and `api_token`.
|
197
|
-
|
198
|
-
```ruby
|
199
|
-
TelegramWorkflow.configure do |config|
|
200
|
-
config.start_action = <Start Action Class>
|
201
|
-
config.api_token = <Your Token>
|
202
|
-
end
|
203
|
-
```
|
204
|
-
|
205
|
-
Method | Default | Description
|
206
|
-
-------|---------|------------
|
207
|
-
api_token | | This is the token you get from `@botfather` to access the Telegram API.
|
208
|
-
start_action | | This is an entry-point action, which is called every time `/start` or `/startgroup` command is sent to the chat. You cannnot redirect to this action or call it manually. Use it to set the things up, e.g. create a user record or store current user's id in the session.
|
209
|
-
session_store | `Rails.cache` or `InMemoryStore.new` | This is the session store. Default implementation stores session in memory, which means it will be reset after server shutdown. Can be [customized](README.md#customization). Use `TelegramWorkflow::Stores::File` for persistent file store.
|
210
|
-
logger | `Rails.logger` or `STDOUT` | Logger object. Can be [customized](README.md#customization).
|
211
|
-
client | `TelegramWorkflow::Client` | The object which implements Telegram API. Can be [customized](README.md#client-customization).
|
212
|
-
webhook_url | nil | The webhook url. Set it only if you are using webhooks for getting updates. TelegramWorkflow will create a webhook subscription automatically.
|
213
|
-
|
214
|
-
## Updates
|
215
|
-
|
216
|
-
The gem implements both methods of getting updates from the Telegram API.
|
217
|
-
|
218
|
-
### Webhooks
|
219
|
-
|
220
|
-
* Configure the gem with `webhook_url` value.
|
221
|
-
* Process the updates with the following code in your controller:
|
222
|
-
|
223
|
-
```ruby
|
224
|
-
class TelegramWebhooksController < ApplicationController
|
225
|
-
def create
|
226
|
-
TelegramWorkflow.process(params)
|
227
|
-
end
|
228
|
-
end
|
229
|
-
```
|
230
|
-
|
231
|
-
### Long polling
|
232
|
-
|
233
|
-
* Make sure you don't configure the gem with `webhook_url` value.
|
234
|
-
* Run the following code:
|
235
|
-
|
236
|
-
```ruby
|
237
|
-
TelegramWorkflow.updates.each do |params|
|
238
|
-
TelegramWorkflow.process(params)
|
239
|
-
end
|
240
|
-
```
|
241
|
-
|
242
|
-
Be aware that `TelegramWorkflow.updates.each` call is blocking.
|
243
|
-
|
244
|
-
`TelegramWorkflow` accepts all the parameters [getUpdates](https://core.telegram.org/bots/api#getupdates) does.
|
245
|
-
|
246
|
-
```ruby
|
247
|
-
TelegramWorkflow.updates(timeout: 60, allowed_updates: %w(channel_post edited_channel_post)).each do |params|
|
248
|
-
...
|
249
|
-
end
|
250
|
-
```
|
251
|
-
|
252
|
-
Since most of the time will be spent on waiting for the Telegram API to respond, you might also want to process the updates in parallel:
|
253
|
-
|
254
|
-
```ruby
|
255
|
-
require "concurrent-ruby"
|
256
|
-
|
257
|
-
pool = Concurrent::CachedThreadPool.new
|
258
|
-
|
259
|
-
TelegramWorkflow.updates.each do |params|
|
260
|
-
pool.post { TelegramWorkflow.process(params) }
|
261
|
-
end
|
262
|
-
```
|
263
|
-
|
264
|
-
Use `stop_updates` call to exit the updates loop:
|
265
|
-
|
266
|
-
```ruby
|
267
|
-
trap "SIGINT" do
|
268
|
-
TelegramWorkflow.stop_updates
|
269
|
-
end
|
270
|
-
|
271
|
-
# decrease the timeout to wait no more than 10 seconds when exiting
|
272
|
-
TelegramWorkflow.updates(timeout: 10).each do |params|
|
273
|
-
TelegramWorkflow.process(params)
|
274
|
-
end
|
275
|
-
|
276
|
-
```
|
277
|
-
|
278
|
-
## Customization
|
279
|
-
|
280
|
-
Object | Customization
|
281
|
-
-------|--------------
|
282
|
-
logger | An object that responds to `info` and `error` methods.
|
283
|
-
session_store | An object that responds to `read` and `write` methods. Refer to [InMemoryStore](lib/telegram_workflow/stores/in_memory.rb) class definition.
|
284
|
-
client | An object that responds to `new(chat_id)` method.
|
285
|
-
|
286
|
-
### Client Customization
|
287
|
-
|
288
|
-
Use this customization to abstract your action's code from the Telegram API implementation details.
|
289
|
-
|
290
|
-
Create a customized client:
|
291
|
-
|
292
|
-
```ruby
|
293
|
-
class MyClient < TelegramWorkflow::Client
|
294
|
-
def send_prize_location(user)
|
295
|
-
# this is an example call
|
296
|
-
prize = user.find_last_prize
|
297
|
-
|
298
|
-
send_venue latitude: prize.latitude,
|
299
|
-
longitude: prize.longitude,
|
300
|
-
address: prize.address
|
301
|
-
title: "Collect the last prize here!",
|
302
|
-
reply_markup: { keyboard: [[{ text: "Give me a hint" }], [{ text: "Give me anohter hint" }]] }
|
303
|
-
end
|
304
|
-
end
|
305
|
-
```
|
306
|
-
|
307
|
-
Now, configure the gem to use the customized client:
|
308
|
-
|
309
|
-
```ruby
|
310
|
-
TelegramWorkflow.configure do |config|
|
311
|
-
config.client = MyClient
|
312
|
-
end
|
313
|
-
```
|
314
|
-
|
315
|
-
Then, in your action:
|
316
|
-
|
317
|
-
```ruby
|
318
|
-
class FindPrize < TelegramWorkflow::Action
|
319
|
-
def initial
|
320
|
-
on_redirect do
|
321
|
-
client.send_prize_location(current_user)
|
322
|
-
end
|
323
|
-
end
|
324
|
-
end
|
325
|
-
```
|
326
|
-
|
327
|
-
## Testing
|
328
|
-
|
329
|
-
Testing utility provides `send_message` helper to emulate messages sent into the chat.
|
330
|
-
|
331
|
-
```ruby
|
332
|
-
# send a message
|
333
|
-
send_message message_text: "text"
|
334
|
-
|
335
|
-
# send CallbackQuery data
|
336
|
-
send_message callback_data: "data"
|
337
|
-
|
338
|
-
# send InlineQuery data
|
339
|
-
send_message inline_data: "data"
|
340
|
-
|
341
|
-
# customize the params
|
342
|
-
send_message { |params| params[:edited_channel_post] = { text: "message" } }
|
343
|
-
```
|
344
|
-
|
345
|
-
Also, `subject.client` and `subject.flow` spies are available to track redirects and calls to the API client inside your actions.
|
346
|
-
Store your tests under `spec/telegram_actions` or tag them with `type: :telegram_action`.
|
347
|
-
|
348
|
-
Suppose we have the following action:
|
349
|
-
|
350
|
-
```ruby
|
351
|
-
class AskForBirthday < TelegramWorkflow::Action
|
352
|
-
def initial
|
353
|
-
on_redirect do
|
354
|
-
client.send_message text: "What year is your birthday?"
|
355
|
-
end
|
356
|
-
|
357
|
-
on_message do
|
358
|
-
Birthday.create! date: params.message_text
|
359
|
-
redirect_to DisplayAge
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
```
|
364
|
-
|
365
|
-
Now, let's add some tests for this action:
|
366
|
-
|
367
|
-
```ruby
|
368
|
-
require "telegram_workflow/rspec"
|
369
|
-
|
370
|
-
RSpec.describe AskForBirthday, type: :telegram_action do
|
371
|
-
it "asks for user's birthday" do
|
372
|
-
expect(subject.client).to have_received(:send_message).with(text: "What year is your birthday?")
|
373
|
-
expect {
|
374
|
-
send_message message_text: "10/10/2000"
|
375
|
-
}.to change { Birthday.count }.by(1)
|
376
|
-
|
377
|
-
expect(subject.flow).to have_received(:redirect_to).with(DisplayAge)
|
378
|
-
end
|
379
|
-
end
|
380
|
-
```
|
381
|
-
|
382
|
-
As you can see, testing utility starts the flow automatically, calling `initial` step on `described_class`.
|
119
|
+
Check out an example of a bot under [example](example) folder.
|
383
120
|
|
384
121
|
## Development
|
385
122
|
|
data/lib/telegram_workflow.rb
CHANGED
@@ -33,11 +33,12 @@ require "telegram_workflow/session"
|
|
33
33
|
require "telegram_workflow/version"
|
34
34
|
require "telegram_workflow/updates"
|
35
35
|
require "telegram_workflow/workflow"
|
36
|
+
require "telegram_workflow/input_file"
|
36
37
|
require "telegram_workflow/stores/in_memory"
|
37
38
|
require "telegram_workflow/stores/file"
|
38
39
|
|
39
40
|
TelegramWorkflow.__after_configuration do |config|
|
40
41
|
if config.webhook_url
|
41
|
-
TelegramWorkflow::Client.new.__setup_webhook
|
42
|
+
TelegramWorkflow::Client.new.__setup_webhook(config.webhook_url, config.webhook_params)
|
42
43
|
end
|
43
44
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class TelegramWorkflow::Client
|
2
|
-
API_VERSION = "
|
3
|
-
|
2
|
+
API_VERSION = "5.3"
|
3
|
+
WebhookConfigPath = Pathname.new("tmp/telegram_workflow/webhook_config.txt")
|
4
4
|
|
5
5
|
AVAILABLE_ACTIONS = %i(
|
6
6
|
getUpdates
|
@@ -9,6 +9,7 @@ class TelegramWorkflow::Client
|
|
9
9
|
getMe
|
10
10
|
sendMessage
|
11
11
|
forwardMessage
|
12
|
+
copyMessage
|
12
13
|
sendPhoto
|
13
14
|
sendAudio
|
14
15
|
sendDocument
|
@@ -28,27 +29,34 @@ class TelegramWorkflow::Client
|
|
28
29
|
getUserProfilePhotos
|
29
30
|
getFile
|
30
31
|
kickChatMember
|
32
|
+
banChatMember
|
31
33
|
unbanChatMember
|
32
34
|
restrictChatMember
|
33
35
|
promoteChatMember
|
34
36
|
setChatAdministratorCustomTitle
|
35
37
|
setChatPermissions
|
36
38
|
exportChatInviteLink
|
39
|
+
createChatInviteLink
|
40
|
+
editChatInviteLink
|
41
|
+
revokeChatInviteLink
|
37
42
|
setChatPhoto
|
38
43
|
deleteChatPhoto
|
39
44
|
setChatTitle
|
40
45
|
setChatDescription
|
41
46
|
pinChatMessage
|
42
47
|
unpinChatMessage
|
48
|
+
unpinAllChatMessages
|
43
49
|
leaveChat
|
44
50
|
getChat
|
45
51
|
getChatAdministrators
|
46
52
|
getChatMembersCount
|
53
|
+
getChatMemberCount
|
47
54
|
getChatMember
|
48
55
|
setChatStickerSet
|
49
56
|
deleteChatStickerSet
|
50
57
|
answerCallbackQuery
|
51
58
|
setMyCommands
|
59
|
+
deleteMyCommands
|
52
60
|
getMyCommands
|
53
61
|
|
54
62
|
editMessageText
|
@@ -78,59 +86,85 @@ class TelegramWorkflow::Client
|
|
78
86
|
sendGame
|
79
87
|
setGameScore
|
80
88
|
getGameHighScores
|
89
|
+
|
90
|
+
logOut
|
91
|
+
close
|
81
92
|
)
|
82
93
|
|
94
|
+
DEPRECATED_ACTIONS = {
|
95
|
+
kickChatMember: :banChatMember,
|
96
|
+
getChatMembersCount: :getChatMemberCount
|
97
|
+
}
|
98
|
+
|
83
99
|
AVAILABLE_ACTIONS.each do |action|
|
84
100
|
method_name = action.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
|
85
101
|
|
86
102
|
define_method(method_name) do |params = {}|
|
87
|
-
|
103
|
+
if deprecated_in_favor_of = DEPRECATED_ACTIONS[action]
|
104
|
+
TelegramWorkflow.config.logger.warn "[TelegramWorkflow] #{action} action is deprecated. Use #{deprecated_in_favor_of} action instead."
|
105
|
+
end
|
106
|
+
|
107
|
+
@inline ?
|
108
|
+
save_request(action, params) :
|
109
|
+
make_request(action, params)
|
88
110
|
end
|
89
111
|
end
|
90
112
|
|
113
|
+
attr_accessor :inline, :inline_request
|
114
|
+
attr_reader :api_url
|
115
|
+
|
91
116
|
def initialize(chat_id = nil)
|
92
117
|
@chat_id = chat_id
|
93
|
-
@webhook_url = TelegramWorkflow.config.webhook_url
|
94
118
|
@api_url = "https://api.telegram.org/bot#{TelegramWorkflow.config.api_token}"
|
95
119
|
end
|
96
120
|
|
97
121
|
def set_webhook(params = {})
|
98
122
|
make_request("setWebhook", params)
|
99
|
-
|
123
|
+
cached_webhook_config(params)
|
100
124
|
end
|
101
125
|
|
102
|
-
def delete_webhook
|
103
|
-
make_request("deleteWebhook",
|
104
|
-
|
126
|
+
def delete_webhook(params = {})
|
127
|
+
make_request("deleteWebhook", params)
|
128
|
+
cached_webhook_config(params)
|
105
129
|
end
|
106
130
|
|
107
|
-
def __setup_webhook
|
131
|
+
def __setup_webhook(webhook_url = TelegramWorkflow.config.webhook_url, params = {})
|
108
132
|
TelegramWorkflow.config.logger.info "[TelegramWorkflow] Checking webhook setup..."
|
109
133
|
|
110
|
-
|
134
|
+
webhook_params = { url: webhook_url, allowed_updates: [], **params }
|
135
|
+
|
136
|
+
if cached_webhook_config != webhook_params
|
111
137
|
TelegramWorkflow.config.logger.info "[TelegramWorkflow] Setting up a new webhook..."
|
112
|
-
set_webhook(
|
138
|
+
set_webhook(webhook_params)
|
113
139
|
end
|
114
140
|
end
|
115
141
|
|
116
142
|
private
|
117
143
|
|
118
|
-
def
|
119
|
-
unless
|
120
|
-
|
121
|
-
|
144
|
+
def cached_webhook_config(new_config = nil)
|
145
|
+
unless WebhookConfigPath.exist?
|
146
|
+
WebhookConfigPath.dirname.mkpath
|
147
|
+
WebhookConfigPath.write(Marshal.dump({}))
|
122
148
|
end
|
123
149
|
|
124
|
-
if
|
125
|
-
|
150
|
+
if new_config.nil?
|
151
|
+
Marshal.load(WebhookConfigPath.read)
|
126
152
|
else
|
127
|
-
|
153
|
+
WebhookConfigPath.write(Marshal.dump(new_config))
|
128
154
|
end
|
129
155
|
end
|
130
156
|
|
131
|
-
def
|
157
|
+
def save_request(action, params = {})
|
158
|
+
raise TelegramWorkflow::Errors::DoubleInlineRequest if @inline_request
|
159
|
+
@inline_request = { method: action, chat_id: @chat_id, **params }
|
160
|
+
end
|
161
|
+
|
162
|
+
def make_request(action, params = {})
|
163
|
+
has_file_params = params.any? { |_, param| param.is_a?(TelegramWorkflow::InputFile) }
|
164
|
+
request_type = has_file_params ? :form : :json
|
165
|
+
|
132
166
|
response = ::Retryable.retryable(tries: 3, on: HTTP::ConnectionError) do
|
133
|
-
::HTTP.post("#{@api_url}/#{action}",
|
167
|
+
::HTTP.post("#{@api_url}/#{action}", request_type => { chat_id: @chat_id, **params })
|
134
168
|
end
|
135
169
|
|
136
170
|
if response.code != 200
|
@@ -21,12 +21,14 @@ module TelegramWorkflow
|
|
21
21
|
end
|
22
22
|
|
23
23
|
class Configuration
|
24
|
-
attr_accessor :session_store, :logger, :client, :start_action, :webhook_url, :api_token
|
24
|
+
attr_accessor :session_store, :logger, :client, :start_action, :webhook_url, :api_token,
|
25
|
+
:webhook_params
|
25
26
|
|
26
27
|
REQUIRED_PARAMS = %i(session_store start_action api_token)
|
27
28
|
|
28
29
|
def initialize
|
29
30
|
@client = TelegramWorkflow::Client
|
31
|
+
@webhook_params = {}
|
30
32
|
|
31
33
|
if defined?(Rails)
|
32
34
|
@session_store = Rails.cache
|
@@ -11,12 +11,24 @@ module TelegramWorkflow::Errors
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
class StartRedirect < StandardError
|
15
|
+
def initialize(msg = "You cannot redirect to a start action.")
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
14
20
|
class NoSession < StandardError
|
15
21
|
def initialize(msg = "Session could not be fetched for this update.")
|
16
22
|
super
|
17
23
|
end
|
18
24
|
end
|
19
25
|
|
26
|
+
class DoubleInlineRequest < StandardError
|
27
|
+
def initialize(msg = "Cannot send more than one request in a row in inline mode.")
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
20
32
|
class ApiError < StandardError
|
21
33
|
end
|
22
34
|
|
@@ -31,11 +31,14 @@ class TelegramWorkflow::Workflow
|
|
31
31
|
end
|
32
32
|
|
33
33
|
@session.dump
|
34
|
+
|
35
|
+
@client.inline_request
|
34
36
|
end
|
35
37
|
|
36
38
|
def redirect_to(action_or_step, session_params = nil)
|
37
39
|
raise TelegramWorkflow::Errors::DoubleRedirect if @redirect_to
|
38
40
|
raise TelegramWorkflow::Errors::SharedRedirect if action_or_step == :shared
|
41
|
+
raise TelegramWorkflow::Errors::StartRedirect if action_or_step == TelegramWorkflow.config.start_action
|
39
42
|
|
40
43
|
@redirect_to = action_or_step
|
41
44
|
@session_params = session_params
|
data/telegram_workflow.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
23
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|example)/}) }
|
25
25
|
end
|
26
26
|
spec.bindir = "exe"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram_workflow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Samoilov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -66,15 +66,15 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 5.2.0
|
69
|
-
description:
|
70
|
-
email:
|
69
|
+
description:
|
70
|
+
email:
|
71
71
|
executables: []
|
72
72
|
extensions: []
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
|
+
- ".github/workflows/ruby.yml"
|
75
76
|
- ".gitignore"
|
76
77
|
- ".rspec"
|
77
|
-
- ".travis.yml"
|
78
78
|
- CODE_OF_CONDUCT.md
|
79
79
|
- Gemfile
|
80
80
|
- LICENSE.txt
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/telegram_workflow/client.rb
|
88
88
|
- lib/telegram_workflow/config.rb
|
89
89
|
- lib/telegram_workflow/errors.rb
|
90
|
+
- lib/telegram_workflow/input_file.rb
|
90
91
|
- lib/telegram_workflow/params.rb
|
91
92
|
- lib/telegram_workflow/rspec.rb
|
92
93
|
- lib/telegram_workflow/session.rb
|
@@ -102,7 +103,7 @@ licenses:
|
|
102
103
|
metadata:
|
103
104
|
homepage_uri: https://github.com/rsamoilov/telegram_workflow
|
104
105
|
source_code_uri: https://github.com/rsamoilov/telegram_workflow
|
105
|
-
post_install_message:
|
106
|
+
post_install_message:
|
106
107
|
rdoc_options: []
|
107
108
|
require_paths:
|
108
109
|
- lib
|
@@ -118,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
119
|
version: '0'
|
119
120
|
requirements: []
|
120
121
|
rubygems_version: 3.1.2
|
121
|
-
signing_key:
|
122
|
+
signing_key:
|
122
123
|
specification_version: 4
|
123
124
|
summary: A simple library to create Telegram Bots in Ruby.
|
124
125
|
test_files: []
|