rbender 0.3.0 → 0.4.92
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/bin/rbender +117 -8
- data/lib/rbender.rb +2 -2
- data/lib/rbender/base.rb +178 -154
- data/lib/rbender/config_handler.rb +35 -0
- data/lib/rbender/keyboard.rb +112 -140
- data/lib/rbender/keyboard_inline.rb +3 -3
- data/lib/rbender/methods.rb +129 -0
- data/lib/rbender/mongo_client.rb +4 -6
- data/lib/rbender/state.rb +202 -285
- data/templates/Gemfile +3 -0
- data/templates/config.yaml +9 -0
- data/templates/locales/en.yaml +3 -0
- data/templates/sample.rb +61 -0
- metadata +8 -2
data/lib/rbender/state.rb
CHANGED
|
@@ -1,288 +1,205 @@
|
|
|
1
|
-
require_relative '../r_bender'
|
|
2
|
-
|
|
3
1
|
class RBender::State
|
|
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
|
-
# API METHODS
|
|
209
|
-
#--------------
|
|
210
|
-
# Hides inline keyboard
|
|
211
|
-
# Must be called from any inline keyboard state
|
|
212
|
-
def hide_inline
|
|
213
|
-
edit_message_reply_markup
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Hides keyboard's markup.
|
|
217
|
-
def hide_keyboard
|
|
218
|
-
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
#
|
|
222
|
-
# @param text [String] string
|
|
223
|
-
#
|
|
224
|
-
def answer_callback_query(text: nil,
|
|
225
|
-
show_alert: nil)
|
|
226
|
-
begin
|
|
227
|
-
@api.answer_callback_query callback_query_id: @message.id,
|
|
228
|
-
text: text,
|
|
229
|
-
show_alert: show_alert
|
|
230
|
-
rescue
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def send_message(text:,
|
|
235
|
-
chat_id: @message.from.id,
|
|
236
|
-
parse_mode: nil,
|
|
237
|
-
disable_web_page_preview: nil,
|
|
238
|
-
disable_notification: nil,
|
|
239
|
-
reply_to_message_id: nil,
|
|
240
|
-
reply_markup: nil)
|
|
241
|
-
|
|
242
|
-
if text.strip.empty?
|
|
243
|
-
raise "A text can't be empty or consists of space symbols only"
|
|
244
|
-
end
|
|
245
|
-
@api.send_message chat_id: chat_id,
|
|
246
|
-
text: text,
|
|
247
|
-
disable_web_page_preview: disable_web_page_preview,
|
|
248
|
-
disable_notification: disable_notification,
|
|
249
|
-
reply_to_message_id: reply_to_message_id,
|
|
250
|
-
parse_mode: parse_mode,
|
|
251
|
-
reply_markup: reply_markup
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
def edit_message_text(inline_message_id: nil,
|
|
256
|
-
text:,
|
|
257
|
-
message_id: @message.message.message_id,
|
|
258
|
-
parse_mode: nil,
|
|
259
|
-
disable_web_page_preview: nil,
|
|
260
|
-
reply_markup: nil)
|
|
261
|
-
begin
|
|
262
|
-
@api.edit_message_text chat_id: @message.from.id,
|
|
263
|
-
message_id: message_id,
|
|
264
|
-
text: text,
|
|
265
|
-
inline_message_id: inline_message_id,
|
|
266
|
-
parse_mode: parse_mode,
|
|
267
|
-
disable_web_page_preview: disable_web_page_preview,
|
|
268
|
-
reply_markup: reply_markup
|
|
269
|
-
rescue
|
|
270
|
-
end
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
def edit_message_reply_markup(chat_id: @message.from.id,
|
|
274
|
-
message_id: @message.message.message_id,
|
|
275
|
-
inline_message_id: nil,
|
|
276
|
-
reply_markup: nil)
|
|
277
|
-
begin
|
|
278
|
-
@api.edit_message_reply_markup chat_id: chat_id,
|
|
279
|
-
message_id: message_id,
|
|
280
|
-
inline_message_id: inline_message_id,
|
|
281
|
-
reply_markup: reply_markup
|
|
282
|
-
rescue
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
end
|
|
2
|
+
|
|
3
|
+
def initialize(message, api, session, &state_block)
|
|
4
|
+
@message = message
|
|
5
|
+
@api = api
|
|
6
|
+
@session = session
|
|
7
|
+
@state_block = state_block
|
|
8
|
+
@keyboard = nil
|
|
9
|
+
@inline_keyboards = {}
|
|
10
|
+
@action_after = nil
|
|
11
|
+
@action_before = nil
|
|
12
|
+
@text_action = nil
|
|
13
|
+
@helpers_block = nil
|
|
14
|
+
@methods = RBender::Methods.new(message, api, session)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def get_keyboard
|
|
18
|
+
@keyboard
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def message
|
|
22
|
+
@message
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Invokes states and processes user's input
|
|
26
|
+
def invoke
|
|
27
|
+
case message
|
|
28
|
+
when Telegram::Bot::Types::CallbackQuery
|
|
29
|
+
process_callback
|
|
30
|
+
when Telegram::Bot::Types::Message
|
|
31
|
+
if @message.text
|
|
32
|
+
process_text_message
|
|
33
|
+
elsif @message.photo
|
|
34
|
+
process_photo
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
when Telegram::Bot::Types::Document
|
|
38
|
+
if @message.photo
|
|
39
|
+
process_photo
|
|
40
|
+
end
|
|
41
|
+
else
|
|
42
|
+
raise "This type isn't available: #{message.class}"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def process_photo
|
|
47
|
+
instance_exec(message.photo, &@photo_action) unless @photo_action.nil?
|
|
48
|
+
end
|
|
49
|
+
# Process if message is just text
|
|
50
|
+
def process_text_message
|
|
51
|
+
|
|
52
|
+
unless @keyboard.nil? # if state has keyboard
|
|
53
|
+
@keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
|
|
54
|
+
build_keyboard
|
|
55
|
+
|
|
56
|
+
@keyboard.markup_final.each do |btn, final_btn|
|
|
57
|
+
if message.text == final_btn
|
|
58
|
+
instance_exec(&@keyboard.actions[btn])
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
unless @text_action.nil?
|
|
64
|
+
instance_exec(@message.text, &@text_action)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Process if message is inline keyboard callback
|
|
70
|
+
def process_callback
|
|
71
|
+
keyboard_name, action = @message.data.split(RBender::CALLBACK_SPLITTER)
|
|
72
|
+
keyboard = @inline_keyboards[keyboard_name.to_sym]
|
|
73
|
+
keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
|
|
74
|
+
keyboard.invoke unless keyboard.nil?
|
|
75
|
+
|
|
76
|
+
unless keyboard.nil?
|
|
77
|
+
unless keyboard.buttons_actions[action].nil?
|
|
78
|
+
instance_eval(&keyboard.buttons_actions[action])
|
|
79
|
+
else
|
|
80
|
+
raise "There is no action called '#{action}'"
|
|
81
|
+
end
|
|
82
|
+
else
|
|
83
|
+
edit_message_text text: "deleted"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def build
|
|
88
|
+
instance_exec(&@state_block)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def build_keyboard
|
|
92
|
+
@keyboard.build(@session)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def invoke_keyboard
|
|
96
|
+
|
|
97
|
+
@api.send_message(chat_id: message.from.id,
|
|
98
|
+
text: @keyboard.response,
|
|
99
|
+
reply_markup: @keyboard.markup_tg)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def invoke_before
|
|
103
|
+
instance_eval(&@action_before)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def has_after?
|
|
107
|
+
@action_after.nil? ? false : true
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def has_before?
|
|
111
|
+
@action_before.nil? ? false : true
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def invoke_after
|
|
115
|
+
instance_eval(&@action_after)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def has_keyboard?
|
|
119
|
+
@keyboard.nil? ? false : true
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
public
|
|
123
|
+
|
|
124
|
+
# adds inline keyboard
|
|
125
|
+
def keyboard_inline(inline_keyboard_name, &inline_keyboard_block)
|
|
126
|
+
@inline_keyboards[inline_keyboard_name] = RBender::KeyboardInline.new(inline_keyboard_name,
|
|
127
|
+
@session,
|
|
128
|
+
inline_keyboard_block)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
#before hook
|
|
132
|
+
def before(&action)
|
|
133
|
+
if @action_before.nil?
|
|
134
|
+
@action_before = action
|
|
135
|
+
else
|
|
136
|
+
raise 'Too many before hooks!'
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
#after hook
|
|
141
|
+
def after(&action)
|
|
142
|
+
if @action_after.nil?
|
|
143
|
+
@action_after = action
|
|
144
|
+
else
|
|
145
|
+
raise 'Too many after hooks!'
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Text callbacks
|
|
150
|
+
def text(&action)
|
|
151
|
+
if @text_action.nil?
|
|
152
|
+
@text_action = action
|
|
153
|
+
else
|
|
154
|
+
raise 'Too many text processors!'
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def keyboard(response_message, &keyboard_block)
|
|
159
|
+
@keyboard = RBender::Keyboard.new response_message
|
|
160
|
+
@keyboard.session = @session
|
|
161
|
+
@keyboard.instance_eval(&keyboard_block)
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
#initialize helper methods
|
|
165
|
+
def helpers(&helpers_block)
|
|
166
|
+
@helpers_block = helpers_block
|
|
167
|
+
instance_eval(&helpers_block)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def photo(&action)
|
|
171
|
+
if @photo_action.nil?
|
|
172
|
+
@photo_action = action
|
|
173
|
+
else
|
|
174
|
+
raise 'Too many image processors!'
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
alias image photo
|
|
179
|
+
alias picture photo
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def method_missing(m, *args, &block)
|
|
183
|
+
if RBender::Methods.method_defined? m
|
|
184
|
+
if block_given?
|
|
185
|
+
if args.empty?
|
|
186
|
+
return @methods.send(m, &block)
|
|
187
|
+
else
|
|
188
|
+
args = args[0] if args.count == 1
|
|
189
|
+
return @methods.send(m, args, &block)
|
|
190
|
+
end
|
|
191
|
+
else
|
|
192
|
+
if args.empty?
|
|
193
|
+
return @methods.send(m)
|
|
194
|
+
else
|
|
195
|
+
args = args[0] if args.count == 1
|
|
196
|
+
return @methods.send(m, args)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
else
|
|
200
|
+
raise NoMethodError, "Method #{m} is missing"
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
287
204
|
end
|
|
288
205
|
|