rbender 0.5.35 → 0.7.3
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 +5 -5
- data/bin/rbender +47 -3
- data/lib/rbender/base.rb +184 -182
- data/lib/rbender/config_handler.rb +39 -39
- data/lib/rbender/fly_settings.rb +30 -0
- data/lib/rbender/keyboard.rb +79 -80
- data/lib/rbender/keyboard_inline.rb +29 -15
- data/lib/rbender/methods.rb +453 -376
- data/lib/rbender/mongo_client.rb +2 -1
- data/lib/rbender/{sessionmanager.rb → session_manager.rb} +11 -14
- data/lib/rbender/state.rb +305 -241
- data/templates/sample.rb +2 -1
- metadata +7 -5
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
require 'yaml'
|
|
2
2
|
|
|
3
3
|
module RBender
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
4
|
+
module ConfigHandler
|
|
5
|
+
@@config_path = 'config.yaml'
|
|
6
|
+
CONFIG_NAME = 'config.yaml'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def self.underscore(str)
|
|
10
|
+
str.gsub(/::/, '/').
|
|
11
|
+
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
|
|
12
|
+
gsub(/([a-z\d])([A-Z])/, '\1_\2').
|
|
13
|
+
tr("-", "_").
|
|
14
|
+
downcase
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.settings
|
|
18
|
+
begin
|
|
19
|
+
@@settings = YAML.load(File.read(@@config_path))
|
|
20
|
+
rescue
|
|
21
|
+
raise "Config file doesn't exists!"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def @@settings.save
|
|
25
|
+
File.write(@@config_path, @@settings.to_yaml)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
@@settings
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.config_path=(path)
|
|
32
|
+
@@config_path = "#{path}/#{CONFIG_NAME}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.token=(token)
|
|
36
|
+
@@token = token
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.token
|
|
40
|
+
@@token
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
43
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
class RBender::FlySettings
|
|
2
|
+
include Singleton
|
|
3
|
+
|
|
4
|
+
attr_reader :settings
|
|
5
|
+
FLY_SETTINGS = :fly_settings.freeze
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@settings = RBender::MongoClient.client[FLY_SETTINGS].find({ fly_settings: 1 })
|
|
9
|
+
|
|
10
|
+
if @settings.count == 0
|
|
11
|
+
@settings.insert_one({ "fly_settings" => 1 })
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def set(key, value)
|
|
16
|
+
@settings.update_one({ "$set" => { key => value } })
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def push(key, value)
|
|
20
|
+
@settings.update_one({ "$push" => { key => value } }) unless list_includes?(key, value)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def get(key)
|
|
24
|
+
@settings.first[key]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def list_includes?(key, value)
|
|
28
|
+
@settings.first[key].include?(value)
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/rbender/keyboard.rb
CHANGED
|
@@ -1,97 +1,96 @@
|
|
|
1
1
|
class RBender::Keyboard
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
attr_accessor :markup,
|
|
4
|
+
:localizations,
|
|
5
|
+
:actions,
|
|
6
|
+
:response,
|
|
7
|
+
:switchers,
|
|
8
|
+
:markup_tg,
|
|
9
|
+
:markup_final,
|
|
10
|
+
:switch_groups,
|
|
11
|
+
:session
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
def initialize
|
|
14
|
+
@actions = {}
|
|
15
|
+
@markup = []
|
|
16
|
+
@localizations = {}
|
|
17
|
+
@markup_tg = nil
|
|
18
|
+
@markup_final = {}
|
|
19
|
+
@button_switch_group = {}
|
|
20
|
+
@one_time = false
|
|
21
|
+
@session = nil
|
|
22
|
+
@hide_on_action = false
|
|
23
|
+
@resize = false
|
|
24
|
+
@force_invoke = false
|
|
25
|
+
end
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
def hide_on_action
|
|
28
|
+
@hide_on_action = true
|
|
29
|
+
end
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
def force_invoked?
|
|
32
|
+
@force_invoke
|
|
33
|
+
end
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
# Adds button to the keyboard
|
|
36
|
+
#
|
|
37
|
+
# Example:
|
|
38
|
+
# button :btn_test, "Test button" do
|
|
39
|
+
# some_actions
|
|
40
|
+
# end
|
|
41
|
+
#
|
|
42
|
+
# button :btn_with_loc, {ru: "Кнопка", en: "Button"} do
|
|
43
|
+
# some_actions
|
|
44
|
+
# end
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
def button(id, description, &action)
|
|
47
|
+
@actions[id] = action
|
|
48
|
+
@localizations[id] = description
|
|
49
|
+
end
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
# Checks keyboard one time or not
|
|
52
|
+
def one_time?
|
|
53
|
+
@one_time
|
|
54
|
+
end
|
|
55
55
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
# makes a keyboard one time
|
|
57
|
+
def one_time
|
|
58
|
+
@one_time = true
|
|
59
|
+
end
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
# Add a line to markup
|
|
62
|
+
def line (*buttons)
|
|
63
|
+
@markup += [buttons]
|
|
64
|
+
end
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
# Set response when keyboard is invoked
|
|
67
|
+
def set_response(new_response)
|
|
68
|
+
@response = new_response
|
|
69
|
+
end
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
# Resize telegram buttons
|
|
73
|
+
def resize
|
|
74
|
+
@resize = true
|
|
75
|
+
end
|
|
76
76
|
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
78
|
+
# Method to initialize a keyboard
|
|
79
|
+
#
|
|
80
|
+
def build(session)
|
|
81
|
+
markup = []
|
|
82
|
+
@markup.each do |line|
|
|
83
|
+
buf_line = []
|
|
84
|
+
line.each do |button_id|
|
|
85
|
+
button = @localizations[button_id].dup
|
|
86
86
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
end
|
|
87
|
+
buf_line << button
|
|
88
|
+
@markup_final[button_id] = button
|
|
89
|
+
end
|
|
90
|
+
markup << buf_line
|
|
91
|
+
end
|
|
92
|
+
@markup_tg = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: markup,
|
|
93
|
+
one_time_keyboard: one_time?,
|
|
94
|
+
resize_keyboard: @resize)
|
|
95
|
+
end
|
|
97
96
|
end
|
|
@@ -3,15 +3,16 @@ class RBender::KeyboardInline
|
|
|
3
3
|
:buttons_actions
|
|
4
4
|
|
|
5
5
|
def initialize(name, session, block)
|
|
6
|
-
@name
|
|
6
|
+
@name = name
|
|
7
7
|
@buttons_callback = {}
|
|
8
|
-
@buttons_inline
|
|
9
|
-
@buttons_links
|
|
10
|
-
@buttons_actions
|
|
11
|
-
@
|
|
12
|
-
@
|
|
13
|
-
@
|
|
14
|
-
@
|
|
8
|
+
@buttons_inline = {}
|
|
9
|
+
@buttons_links = {}
|
|
10
|
+
@buttons_actions = {}
|
|
11
|
+
@buttons_pay = {}
|
|
12
|
+
@markup = []
|
|
13
|
+
@markup_tg = []
|
|
14
|
+
@session = session
|
|
15
|
+
@keyboard = block
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
def session
|
|
@@ -20,31 +21,43 @@ class RBender::KeyboardInline
|
|
|
20
21
|
|
|
21
22
|
# Adds button with callback
|
|
22
23
|
def button(name, description, &action)
|
|
23
|
-
callback
|
|
24
|
-
@buttons_callback[name]
|
|
25
|
-
|
|
24
|
+
callback = @name.to_s + RBender::CALLBACK_SPLITTER + name.to_s
|
|
25
|
+
@buttons_callback[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text: description,
|
|
26
|
+
callback_data: callback)
|
|
26
27
|
@buttons_actions[name.to_s] = action
|
|
27
28
|
end
|
|
28
29
|
|
|
29
30
|
# Adds link button
|
|
30
31
|
def button_link(name, description, link)
|
|
31
32
|
@buttons_links[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text: description,
|
|
32
|
-
url:
|
|
33
|
+
url: link)
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
# Adds inline switch mode button
|
|
36
37
|
def button_inline(name, description, inline_query)
|
|
37
|
-
@buttons_inline[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text:
|
|
38
|
+
@buttons_inline[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text: description,
|
|
38
39
|
switch_inline_query: inline_query)
|
|
39
40
|
end
|
|
40
41
|
|
|
42
|
+
def button_game(name, description, callback_game)
|
|
43
|
+
@buttons_inline[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text: description,
|
|
44
|
+
callback_game: callback_game)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# def button_callback
|
|
48
|
+
|
|
49
|
+
def button_pay(name, description)
|
|
50
|
+
@buttons_pay[name] = Telegram::Bot::Types::InlineKeyboardButton.new(text: description,
|
|
51
|
+
pay: true)
|
|
52
|
+
end
|
|
53
|
+
|
|
41
54
|
# Adds a line to markup
|
|
42
|
-
def
|
|
55
|
+
def line(*buttons)
|
|
43
56
|
@markup += [buttons]
|
|
44
57
|
end
|
|
45
58
|
|
|
46
59
|
def invoke
|
|
47
|
-
instance_eval(&@
|
|
60
|
+
instance_eval(&@keyboard)
|
|
48
61
|
end
|
|
49
62
|
|
|
50
63
|
def build
|
|
@@ -55,6 +68,7 @@ class RBender::KeyboardInline
|
|
|
55
68
|
buttons.merge! @buttons_callback
|
|
56
69
|
buttons.merge! @buttons_links
|
|
57
70
|
buttons.merge! @buttons_inline
|
|
71
|
+
buttons.merge! @buttons_pay
|
|
58
72
|
|
|
59
73
|
markup = []
|
|
60
74
|
@markup.each do |btn_row|
|
data/lib/rbender/methods.rb
CHANGED
|
@@ -2,381 +2,458 @@ require 'faraday'
|
|
|
2
2
|
|
|
3
3
|
class RBender::Methods
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
5
|
+
def initialize(message, api, session)
|
|
6
|
+
@message = message
|
|
7
|
+
@api = api
|
|
8
|
+
@session = session
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
#--------------
|
|
12
|
+
# User methods
|
|
13
|
+
#--------------
|
|
14
|
+
|
|
15
|
+
# Set message user gets while keyboard has invoked
|
|
16
|
+
def set_response(new_response)
|
|
17
|
+
@keyboard.set_response(new_response)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Returns session hash
|
|
21
|
+
def session
|
|
22
|
+
@session
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Returns message object
|
|
26
|
+
def message
|
|
27
|
+
case @message
|
|
28
|
+
when Telegram::Bot::Types::CallbackQuery
|
|
29
|
+
@message.message
|
|
30
|
+
when Telegram::Bot::Types::Message
|
|
31
|
+
@message
|
|
32
|
+
else
|
|
33
|
+
@message
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def chat_id
|
|
38
|
+
message().chat.id
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def switch(state_to)
|
|
42
|
+
@session[:state] = state_to
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
#--------------
|
|
46
|
+
# API METHODS
|
|
47
|
+
#--------------
|
|
48
|
+
# Hides inline keyboard
|
|
49
|
+
# Must be called from any inline keyboard state
|
|
50
|
+
|
|
51
|
+
def send_message(text:,
|
|
52
|
+
chat_id: chat_id(),
|
|
53
|
+
parse_mode: nil,
|
|
54
|
+
disable_web_page_preview: nil,
|
|
55
|
+
disable_notification: nil,
|
|
56
|
+
reply_to_message_id: nil,
|
|
57
|
+
reply_markup: nil)
|
|
58
|
+
|
|
59
|
+
if text.strip.empty?
|
|
60
|
+
raise "A text can't be empty or consists of space symbols only"
|
|
61
|
+
end
|
|
62
|
+
@api.send_message chat_id: chat_id,
|
|
63
|
+
text: text,
|
|
64
|
+
disable_web_page_preview: disable_web_page_preview,
|
|
65
|
+
disable_notification: disable_notification,
|
|
66
|
+
reply_to_message_id: reply_to_message_id,
|
|
67
|
+
parse_mode: parse_mode,
|
|
68
|
+
reply_markup: reply_markup
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def edit_message_text(inline_message_id: nil,
|
|
72
|
+
text:,
|
|
73
|
+
chat_id: chat_id(),
|
|
74
|
+
message_id: message().message_id,
|
|
75
|
+
parse_mode: nil,
|
|
76
|
+
disable_web_page_preview: nil,
|
|
77
|
+
reply_markup: nil)
|
|
78
|
+
if text.strip.empty?
|
|
79
|
+
raise "A text can't be empty or consists of space symbols only"
|
|
80
|
+
end
|
|
81
|
+
@api.edit_message_text chat_id: chat_id,
|
|
82
|
+
message_id: message_id,
|
|
83
|
+
text: text,
|
|
84
|
+
inline_message_id: inline_message_id,
|
|
85
|
+
parse_mode: parse_mode,
|
|
86
|
+
disable_web_page_preview: disable_web_page_preview,
|
|
87
|
+
reply_markup: reply_markup
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def edit_message_caption(inline_message_id: nil,
|
|
91
|
+
caption: nil,
|
|
92
|
+
chat_id: chat_id(),
|
|
93
|
+
message_id: message().message_id,
|
|
94
|
+
reply_markup: nil)
|
|
95
|
+
if text.strip.empty?
|
|
96
|
+
raise "A text can't be empty or consists of space symbols only"
|
|
97
|
+
end
|
|
98
|
+
@api.edit_message_text chat_id: chat_id,
|
|
99
|
+
message_id: message_id,
|
|
100
|
+
caption: caption,
|
|
101
|
+
inline_message_id: inline_message_id,
|
|
102
|
+
reply_markup: reply_markup
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def edit_message_reply_markup(chat_id: chat_id(),
|
|
106
|
+
message_id: message().message_id,
|
|
107
|
+
inline_message_id: nil,
|
|
108
|
+
reply_markup: nil)
|
|
109
|
+
@api.edit_message_reply_markup chat_id: chat_id,
|
|
110
|
+
message_id: message_id,
|
|
111
|
+
inline_message_id: inline_message_id,
|
|
112
|
+
reply_markup: reply_markup
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def answer_callback_query(callback_query_id: @message.id,
|
|
116
|
+
text: nil,
|
|
117
|
+
show_alert: nil,
|
|
118
|
+
url: nil,
|
|
119
|
+
cache_time: nil)
|
|
120
|
+
@api.answer_callback_query(callback_query_id: callback_query_id,
|
|
121
|
+
text: text,
|
|
122
|
+
show_alert: show_alert,
|
|
123
|
+
url: url,
|
|
124
|
+
cache_time: cache_time)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def delete_message(chat_id: chat_id(),
|
|
128
|
+
message_id: message().message_id)
|
|
129
|
+
@api.delete_message(chat_id: chat_id,
|
|
130
|
+
message_id: message_id)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def get_file(file_id:)
|
|
135
|
+
result = @api.get_file(file_id: file_id)
|
|
136
|
+
result['ok'] ? result['result'] : nil
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def get_file_path(file_id:)
|
|
140
|
+
result = @api.get_file(file_id: file_id)
|
|
141
|
+
result['ok'] ? result['result']['file_path'] : nil
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def get_me()
|
|
145
|
+
@api.get_me
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def forward_message(chat_id:,
|
|
149
|
+
from_chat_id: chat_id(),
|
|
150
|
+
disable_notification: false,
|
|
151
|
+
message_id:)
|
|
152
|
+
@api.forward_message(chat_id: chat_id,
|
|
153
|
+
from_chat_id: from_chat_id,
|
|
154
|
+
disable_notification: disable_notification,
|
|
155
|
+
message_id: message_id)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def send_photo(chat_id: chat_id(),
|
|
159
|
+
photo:,
|
|
160
|
+
caption: nil,
|
|
161
|
+
disable_notification: false,
|
|
162
|
+
reply_to_message_id: nil,
|
|
163
|
+
reply_markup: nil)
|
|
164
|
+
@api.send_photo(chat_id: chat_id,
|
|
165
|
+
photo: photo,
|
|
166
|
+
caption: caption,
|
|
167
|
+
disable_notification: disable_notification,
|
|
168
|
+
reply_to_message_id: reply_to_message_id,
|
|
169
|
+
reply_markup: reply_markup)
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def send_audio(chat_id: chat_id(),
|
|
173
|
+
audio:,
|
|
174
|
+
caption: nil,
|
|
175
|
+
duration: nil,
|
|
176
|
+
performer: nil,
|
|
177
|
+
title: nil,
|
|
178
|
+
disable_notification: false,
|
|
179
|
+
reply_to_message_id: nil,
|
|
180
|
+
reply_markup: nil)
|
|
181
|
+
|
|
182
|
+
@api.send_audio(chat_id: chat_id,
|
|
183
|
+
audio: audio,
|
|
184
|
+
caption: caption,
|
|
185
|
+
duration: duration,
|
|
186
|
+
performer: performer,
|
|
187
|
+
title: title,
|
|
188
|
+
disable_notification: disable_notification,
|
|
189
|
+
reply_to_message_id: reply_to_message_id,
|
|
190
|
+
reply_markup: reply_markup)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def send_media_group(chat_id: chat_id(),
|
|
194
|
+
media:,
|
|
195
|
+
disable_notification: nil,
|
|
196
|
+
reply_to_message_id: nil)
|
|
197
|
+
@api.send_media_group(chat_id: chat_id,
|
|
198
|
+
media: media,
|
|
199
|
+
disable_notification: disable_notification,
|
|
200
|
+
reply_to_message_id: reply_to_message_id)
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def send_document(chat_id: chat_id(),
|
|
204
|
+
document:,
|
|
205
|
+
caption: nil,
|
|
206
|
+
disable_notification: false,
|
|
207
|
+
reply_to_message_id: nil,
|
|
208
|
+
reply_markup: nil)
|
|
209
|
+
@api.send_document(chat_id: chat_id,
|
|
210
|
+
document: document,
|
|
211
|
+
caption: caption,
|
|
212
|
+
disable_notification: disable_notification,
|
|
213
|
+
reply_to_message_id: reply_to_message_id,
|
|
214
|
+
reply_markup: reply_markup)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def send_sticker(chat_id: chat_id(),
|
|
218
|
+
sticker:,
|
|
219
|
+
caption: nil,
|
|
220
|
+
disable_notification: false,
|
|
221
|
+
reply_to_message_id: nil,
|
|
222
|
+
reply_markup: nil)
|
|
223
|
+
@api.send_sticker(chat_id: chat_id,
|
|
224
|
+
sticker: sticker,
|
|
225
|
+
caption: caption,
|
|
226
|
+
disable_notification: disable_notification,
|
|
227
|
+
reply_to_message_id: reply_to_message_id,
|
|
228
|
+
reply_markup: reply_markup)
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
def send_video(chat_id: chat_id(),
|
|
232
|
+
video:,
|
|
233
|
+
width: nil,
|
|
234
|
+
height: nil,
|
|
235
|
+
caption: nil,
|
|
236
|
+
duration: nil,
|
|
237
|
+
disable_notification: false,
|
|
238
|
+
reply_to_message_id: nil,
|
|
239
|
+
reply_markup: nil)
|
|
240
|
+
@api.send_video(chat_id: chat_id,
|
|
241
|
+
video: video,
|
|
242
|
+
width: width,
|
|
243
|
+
height: height,
|
|
244
|
+
caption: caption,
|
|
245
|
+
duration: duration,
|
|
246
|
+
disable_notification: disable_notification,
|
|
247
|
+
reply_to_message_id: reply_to_message_id,
|
|
248
|
+
reply_markup: reply_markup)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def send_voice(chat_id: chat_id(),
|
|
252
|
+
voice:,
|
|
253
|
+
caption: nil,
|
|
254
|
+
duration: nil,
|
|
255
|
+
disable_notification: false,
|
|
256
|
+
reply_to_message_id: nil,
|
|
257
|
+
reply_markup: nil)
|
|
258
|
+
|
|
259
|
+
@api.send_voice(chat_id: chat_id,
|
|
260
|
+
voice: voice,
|
|
261
|
+
caption: caption,
|
|
262
|
+
duration: duration,
|
|
263
|
+
disable_notification: disable_notification,
|
|
264
|
+
reply_to_message_id: reply_to_message_id,
|
|
265
|
+
reply_markup: reply_markup)
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def send_video_note(chat_id: chat_id(),
|
|
269
|
+
video_note:,
|
|
270
|
+
length: nil,
|
|
271
|
+
duration: nil,
|
|
272
|
+
disable_notification: false,
|
|
273
|
+
reply_to_message_id: nil,
|
|
274
|
+
reply_markup: nil)
|
|
275
|
+
@api.send_video_note(chat_id: chat_id,
|
|
276
|
+
video_note: video_note,
|
|
277
|
+
length: length,
|
|
278
|
+
duration: duration,
|
|
279
|
+
disable_notification: disable_notification,
|
|
280
|
+
reply_to_message_id: reply_to_message_id,
|
|
281
|
+
reply_markup: reply_markup)
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
def send_location(chat_id: chat_id(),
|
|
285
|
+
latitude:,
|
|
286
|
+
longitude:,
|
|
287
|
+
disable_notification: false,
|
|
288
|
+
reply_to_message_id: nil,
|
|
289
|
+
reply_markup: nil)
|
|
290
|
+
|
|
291
|
+
@api.send_location(chat_id: chat_id,
|
|
292
|
+
latitude: latitude,
|
|
293
|
+
longitude: longitude,
|
|
294
|
+
disable_notification: disable_notification,
|
|
295
|
+
reply_to_message_id: reply_to_message_id,
|
|
296
|
+
reply_markup: reply_markup)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def send_venue(chat_id: chat_id(),
|
|
300
|
+
latitude:,
|
|
301
|
+
longitude:,
|
|
302
|
+
title:,
|
|
303
|
+
address:,
|
|
304
|
+
foursquare_id: nil,
|
|
305
|
+
disable_notification: false,
|
|
306
|
+
reply_to_message_id: nil,
|
|
307
|
+
reply_markup: nil)
|
|
308
|
+
|
|
309
|
+
@api.send_venue(chat_id: chat_id,
|
|
310
|
+
latitude: latitude,
|
|
311
|
+
longitude: longitude,
|
|
312
|
+
title: title,
|
|
313
|
+
address: address,
|
|
314
|
+
foursquare_id: foursquare_id,
|
|
315
|
+
disable_notification: disable_notification,
|
|
316
|
+
reply_to_message_id: reply_to_message_id,
|
|
317
|
+
reply_markup: reply_markup)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def send_contact(chat_id: chat_id(),
|
|
321
|
+
phone_number:,
|
|
322
|
+
first_name:,
|
|
323
|
+
last_name: nil,
|
|
324
|
+
disable_notification: false,
|
|
325
|
+
reply_to_message_id: nil,
|
|
326
|
+
reply_markup: nil)
|
|
327
|
+
|
|
328
|
+
@api.send_contact(chat_id: chat_id,
|
|
329
|
+
phone_number: phone_number,
|
|
330
|
+
first_name: first_name,
|
|
331
|
+
last_name: last_name,
|
|
332
|
+
disable_notification: disable_notification,
|
|
333
|
+
reply_to_message_id: reply_to_message_id,
|
|
334
|
+
reply_markup: reply_markup)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def send_chat_action(chat_id: chat_id(),
|
|
338
|
+
action:)
|
|
339
|
+
@api.send_chat_action(chat_id: chat_id,
|
|
340
|
+
action: action)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def get_user_profile_photos(chat_id: chat_id(),
|
|
344
|
+
offset: nil,
|
|
345
|
+
limit: nil)
|
|
346
|
+
@api.get_user_profile_photos(chat_id: chat_id,
|
|
347
|
+
offset: offset,
|
|
348
|
+
limit: limit)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def kick_chat_member(chat_id:,
|
|
352
|
+
user_id:)
|
|
353
|
+
@api.kick_chat_member(chat_id: chat_id,
|
|
354
|
+
user_id: user_id)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
def unban_chat_member(chat_id:,
|
|
358
|
+
user_id:)
|
|
359
|
+
@api.unban_chat_member(chat_id: chat_id,
|
|
360
|
+
user_id: user_id)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def leave_chat(chat_id:)
|
|
364
|
+
@api.leave_chat(chat_id: chat_id)
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def get_chat(chat_id:)
|
|
368
|
+
@api.get_chat(chat_id: chat_id)
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
def get_chat_administrators(chat_id:)
|
|
372
|
+
@api.get_chat_administrators(chat_id: chat_id)
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
def get_chat_members_count(chat_id:)
|
|
376
|
+
@api.get_chat_members_count(chat_id: chat_id)
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
def get_chat_member(chat_id:, user_id:)
|
|
380
|
+
@api.get_chat_member(chat_id: chat_id,
|
|
381
|
+
user_id: user_id)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def upload_file(file_path)
|
|
385
|
+
full_path = "#{Dir.pwd}/public/#{file_path}"
|
|
386
|
+
Faraday::UploadIO.new(full_path, "multipart/form-data")
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
alias file upload_file
|
|
390
|
+
alias upload upload_file
|
|
391
|
+
|
|
392
|
+
def download_file(file_path:, to:)
|
|
393
|
+
url = 'https://api.telegram.org/file/bot'
|
|
394
|
+
token = RBender::ConfigHandler.token
|
|
395
|
+
path = Dir.pwd + "/public/#{to}"
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
http_conn = Faraday.new do |builder|
|
|
399
|
+
builder.adapter(Faraday.default_adapter)
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
response = http_conn.get "#{url}#{token}/#{file_path}"
|
|
403
|
+
|
|
404
|
+
dir = File.dirname(path)
|
|
405
|
+
|
|
406
|
+
unless File.directory?(dir)
|
|
407
|
+
FileUtils.mkdir_p(dir)
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
File.open(path, 'wb') { |fp| fp.write(response.body) }
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
alias download download_file
|
|
414
|
+
|
|
415
|
+
def send_invoice(chat_id: chat_id(), title:, description:,
|
|
416
|
+
payload:, provider_token:, start_parameter:, currency:,
|
|
417
|
+
prices:, provider_data: nil, photo_url: nil, photo_size: nil,
|
|
418
|
+
photo_width: nil, need_name: nil, need_phone_number: nil, need_email: nil,
|
|
419
|
+
need_shipping_address:, send_phone_number_to_provider: nil, send_email_to_provider: nil,
|
|
420
|
+
is_flexible: nil, disable_notification: nil, reply_to_message_id: nil, reply_markup: nil)
|
|
421
|
+
|
|
422
|
+
@api.send_invoice(chat_id: chat_id,
|
|
423
|
+
title: title,
|
|
424
|
+
description: description,
|
|
425
|
+
payload: payload,
|
|
426
|
+
provider_token: provider_token,
|
|
427
|
+
start_parameter: start_parameter,
|
|
428
|
+
currency: currency,
|
|
429
|
+
prices: prices, provider_data: provider_data,
|
|
430
|
+
photo_url: photo_url,
|
|
431
|
+
photo_size: photo_size,
|
|
432
|
+
photo_width: photo_width,
|
|
433
|
+
need_name: need_name,
|
|
434
|
+
need_phone_number: need_phone_number,
|
|
435
|
+
need_email: need_email,
|
|
436
|
+
need_shipping_address: need_shipping_address,
|
|
437
|
+
send_phone_number_to_provider: send_phone_number_to_provider,
|
|
438
|
+
send_email_to_provider: send_email_to_provider,
|
|
439
|
+
is_flexible: is_flexible,
|
|
440
|
+
disable_notification: disable_notification,
|
|
441
|
+
reply_to_message_id: reply_to_message_id,
|
|
442
|
+
reply_markup: reply_markup)
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def answer_shipping_query(shipping_query_id:, ok:,
|
|
446
|
+
shipping_options: nil, error_message: nil)
|
|
447
|
+
@api.answer_shipping_query(shipping_query_id: shipping_query_id,
|
|
448
|
+
shipping_options: shipping_options,
|
|
449
|
+
error_message: error_message,
|
|
450
|
+
ok: ok)
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def answer_pre_checkout_query(pre_checkout_query_id: message().id, ok:, error_message: nil)
|
|
454
|
+
@api.answer_pre_checkout_query(pre_checkout_query_id: pre_checkout_query_id,
|
|
455
|
+
error_message: error_message,
|
|
456
|
+
ok: ok)
|
|
457
|
+
end
|
|
381
458
|
end
|
|
382
459
|
|