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.
@@ -1,288 +1,205 @@
1
- require_relative '../r_bender'
2
-
3
1
  class RBender::State
4
- def initialize(message, api, session, &state_block)
5
- @message = message
6
- @api = api
7
- @session = session
8
- @state_block = state_block
9
- @keyboard = nil
10
- @inline_keyboards = {}
11
- @action_after = nil
12
- @action_before = nil
13
- @text_action = nil
14
- @helpers_block = nil
15
- end
16
-
17
- def _keyboard
18
- @keyboard
19
- end
20
-
21
- # Invokes states and processes user's input
22
- def _invoke
23
- case message
24
- when Telegram::Bot::Types::CallbackQuery
25
- _process_callback
26
- when Telegram::Bot::Types::Message
27
- _process_text_message
28
- else
29
- puts "This type isn't available: #{message.class}"
30
- end
31
- end
32
-
33
- # Process if message is just text
34
- def _process_text_message
35
-
36
- unless @keyboard.nil? # if state has keyboard
37
- @keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
38
- _build_keyboard
39
-
40
- @keyboard.markup_final.each do |btn, final_btn|
41
- if message.text == final_btn
42
- instance_exec(&@keyboard.actions[btn])
43
-
44
- unless (group_id = @keyboard.button_switch_group(btn)).nil? # Switch group's logic
45
- group_size = @keyboard.switch_groups[group_id].size
46
- @session[:_keyboard_switch_groups][group_id] += 1
47
- @session[:_keyboard_switch_groups][group_id] %= group_size
48
- end
49
- if @keyboard.switchers.member? btn # Switcher's Logic
50
- switcher_size = @keyboard.switchers[btn].size
51
- @session[:_keyboard_switchers][btn] += 1
52
- @session[:_keyboard_switchers][btn] %= switcher_size
53
- end
54
- return
55
- end
56
- end
57
- end
58
-
59
- unless @text_action.nil?
60
-
61
- instance_exec(@message.text, &@text_action)
62
- end
63
-
64
- end
65
-
66
- # Process if message is inline keyboard callback
67
- def _process_callback
68
- keyboard_name, action = @message.data.split(RBender::CALLBACK_SPLITTER)
69
- keyboard = @inline_keyboards[keyboard_name.to_sym]
70
- keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
71
- keyboard._invoke unless keyboard.nil?
72
-
73
- unless keyboard.nil?
74
- unless keyboard.buttons_actions[action].nil?
75
- instance_eval(&keyboard.buttons_actions[action])
76
- else
77
- raise "There is no action called '#{action}'"
78
- end
79
- else
80
- edit_message_text text: "deleted"
81
- end
82
- end
83
-
84
- def _build
85
- instance_exec(&@state_block)
86
- end
87
-
88
- def _build_keyboard
89
- @keyboard._build(@session)
90
- end
91
-
92
- def _invoke_keyboard
93
-
94
- @api.send_message(chat_id: message.from.id,
95
- text: @keyboard.response,
96
- reply_markup: @keyboard.markup_tg)
97
- end
98
-
99
- def _invoke_before
100
- instance_eval(&@action_before)
101
- end
102
-
103
- def has_after?
104
- @action_after.nil? ? false : true
105
- end
106
-
107
- def has_before?
108
- @action_before.nil? ? false : true
109
- end
110
-
111
- def _invoke_after
112
- instance_eval(&@action_after)
113
- end
114
-
115
- def has_keyboard?
116
- @keyboard.nil? ? false : true
117
- end
118
-
119
- #--------------
120
- # User methods
121
- #--------------
122
-
123
- def keyboard(response_message, &keyboard_block)
124
- @keyboard = RBender::Keyboard.new response_message
125
- @keyboard.session = @session
126
- @keyboard.instance_eval(&keyboard_block)
127
- end
128
-
129
- #initialize helper methods
130
- def helpers(&helpers_block)
131
- @helpers_block = helpers_block
132
- instance_eval(&helpers_block)
133
- end
134
-
135
- # Set message user gets while keyboard has invoked
136
- def set_response(new_response)
137
- @keyboard.set_response(new_response)
138
- end
139
-
140
- # Returns session hash
141
- def session
142
- @session
143
- end
144
-
145
- # Returns message object
146
- def message
147
- @message
148
- end
149
-
150
- # adds inline keyboard
151
- def keyboard_inline(inline_keyboard_name, &inline_keyboard_block)
152
- keyboard = @inline_keyboards[inline_keyboard_name] = RBender::KeyboardInline.new(inline_keyboard_name,
153
- @session,
154
- inline_keyboard_block)
155
- end
156
-
157
- def inline_markup(name)
158
-
159
- raise "Keyboard #{name} doesn't exists!" unless @inline_keyboards.member? name
160
- keyboard = @inline_keyboards[name]
161
- keyboard.instance_eval(&@helpers_block) unless @helpers_block.nil?
162
- keyboard._build
163
- keyboard.markup_tg
164
- end
165
-
166
- def switch(state_to)
167
- @session[:_state_stack].push(@session[:_state])
168
- @session[:_state] = state_to
169
- end
170
-
171
- def switch_prev
172
- @session[:_state] = @session[:_state_stack].pop
173
- end
174
-
175
- def switcher_state(id)
176
- session[:_keyboard_switchers][id]
177
- end
178
-
179
- #before hook
180
- def before(&action)
181
- if @action_before.nil?
182
- @action_before = action
183
- else
184
- raise 'Too many before hooks!'
185
- end
186
- end
187
-
188
- #after hook
189
- def after(&action)
190
- if @action_after.nil?
191
- @action_after = action
192
- else
193
- raise 'Too many after hooks!'
194
- end
195
- end
196
-
197
- # Text callbacks
198
- def text(&action)
199
- if @text_action.nil?
200
- @text_action = action
201
- else
202
- raise 'Too many text processors!'
203
- end
204
- end
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