telegem 1.0.6 → 2.0.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.
data/lib/core/context.rb CHANGED
@@ -1,9 +1,10 @@
1
- # lib/core/context.rb - HTTPX VERSION (NO ASYNC GEM)
1
+ require 'json'
2
+
2
3
  module Telegem
3
4
  module Core
4
5
  class Context
5
6
  attr_accessor :update, :bot, :state, :match, :session, :scene
6
-
7
+
7
8
  def initialize(update, bot)
8
9
  @update = update
9
10
  @bot = bot
@@ -12,283 +13,275 @@ module Telegem
12
13
  @match = nil
13
14
  @scene = nil
14
15
  end
15
-
16
- # Message shortcuts
16
+
17
17
  def message
18
18
  @update.message
19
19
  end
20
-
20
+
21
21
  def callback_query
22
22
  @update.callback_query
23
23
  end
24
-
24
+
25
25
  def inline_query
26
26
  @update.inline_query
27
27
  end
28
-
29
- # Entity shortcuts
28
+
30
29
  def from
31
30
  message&.from || callback_query&.from || inline_query&.from
32
31
  end
33
-
32
+
34
33
  def chat
35
34
  message&.chat || callback_query&.message&.chat
36
35
  end
37
-
36
+
38
37
  def data
39
38
  callback_query&.data
40
39
  end
41
-
40
+
42
41
  def query
43
42
  inline_query&.query
44
43
  end
45
-
46
- # Action methods - ALL return HTTPX request objects
44
+
47
45
  def reply(text, **options)
48
46
  return nil unless chat
49
47
 
50
48
  params = { chat_id: chat.id, text: text }.merge(options)
51
49
  @bot.api.call('sendMessage', params)
52
50
  end
53
-
51
+
54
52
  def edit_message_text(text, **options)
55
53
  return nil unless message && chat
56
-
54
+
57
55
  params = {
58
56
  chat_id: chat.id,
59
57
  message_id: message.message_id,
60
58
  text: text
61
59
  }.merge(options)
62
-
60
+
63
61
  @bot.api.call('editMessageText', params)
64
62
  end
65
-
63
+
66
64
  def delete_message(message_id = nil)
67
65
  mid = message_id || message&.message_id
68
66
  return nil unless mid && chat
69
-
67
+
70
68
  @bot.api.call('deleteMessage', chat_id: chat.id, message_id: mid)
71
69
  end
72
-
70
+
73
71
  def answer_callback_query(text: nil, show_alert: false, **options)
74
72
  return nil unless callback_query
75
-
73
+
76
74
  params = {
77
75
  callback_query_id: callback_query.id,
78
76
  show_alert: show_alert
79
77
  }.merge(options)
80
-
78
+
81
79
  params[:text] = text if text
82
80
  @bot.api.call('answerCallbackQuery', params)
83
81
  end
84
-
82
+
85
83
  def answer_inline_query(results, **options)
86
84
  return nil unless inline_query
87
-
85
+
88
86
  params = {
89
87
  inline_query_id: inline_query.id,
90
88
  results: results.to_json
91
89
  }.merge(options)
92
-
90
+
93
91
  @bot.api.call('answerInlineQuery', params)
94
92
  end
95
-
96
- # Media methods
93
+
97
94
  def photo(photo, caption: nil, **options)
98
95
  return nil unless chat
99
-
96
+
100
97
  params = { chat_id: chat.id, caption: caption }.merge(options)
101
-
98
+
102
99
  if file_object?(photo)
103
100
  @bot.api.upload('sendPhoto', params.merge(photo: photo))
104
101
  else
105
102
  @bot.api.call('sendPhoto', params.merge(photo: photo))
106
103
  end
107
104
  end
108
-
105
+
109
106
  def document(document, caption: nil, **options)
110
107
  return nil unless chat
111
-
108
+
112
109
  params = { chat_id: chat.id, caption: caption }.merge(options)
113
-
110
+
114
111
  if file_object?(document)
115
112
  @bot.api.upload('sendDocument', params.merge(document: document))
116
113
  else
117
114
  @bot.api.call('sendDocument', params.merge(document: document))
118
115
  end
119
116
  end
120
-
117
+
121
118
  def audio(audio, caption: nil, **options)
122
119
  return nil unless chat
123
-
120
+
124
121
  params = { chat_id: chat.id, caption: caption }.merge(options)
125
-
122
+
126
123
  if file_object?(audio)
127
124
  @bot.api.upload('sendAudio', params.merge(audio: audio))
128
125
  else
129
126
  @bot.api.call('sendAudio', params.merge(audio: audio))
130
127
  end
131
128
  end
132
-
129
+
133
130
  def video(video, caption: nil, **options)
134
131
  return nil unless chat
135
-
132
+
136
133
  params = { chat_id: chat.id, caption: caption }.merge(options)
137
-
134
+
138
135
  if file_object?(video)
139
136
  @bot.api.upload('sendVideo', params.merge(video: video))
140
137
  else
141
138
  @bot.api.call('sendVideo', params.merge(video: video))
142
139
  end
143
140
  end
144
-
141
+
145
142
  def voice(voice, caption: nil, **options)
146
143
  return nil unless chat
147
-
144
+
148
145
  params = { chat_id: chat.id, caption: caption }.merge(options)
149
-
146
+
150
147
  if file_object?(voice)
151
148
  @bot.api.upload('sendVoice', params.merge(voice: voice))
152
149
  else
153
150
  @bot.api.call('sendVoice', params.merge(voice: voice))
154
151
  end
155
152
  end
156
-
153
+
157
154
  def sticker(sticker, **options)
158
155
  return nil unless chat
159
-
156
+
160
157
  params = { chat_id: chat.id, sticker: sticker }.merge(options)
161
158
  @bot.api.call('sendSticker', params)
162
159
  end
163
-
160
+
164
161
  def location(latitude, longitude, **options)
165
162
  return nil unless chat
166
-
163
+
167
164
  params = {
168
165
  chat_id: chat.id,
169
166
  latitude: latitude,
170
167
  longitude: longitude
171
168
  }.merge(options)
172
-
169
+
173
170
  @bot.api.call('sendLocation', params)
174
171
  end
175
-
172
+
176
173
  def send_chat_action(action, **options)
177
174
  return nil unless chat
178
-
175
+
179
176
  params = { chat_id: chat.id, action: action }.merge(options)
180
177
  @bot.api.call('sendChatAction', params)
181
178
  end
182
-
179
+
183
180
  def forward_message(from_chat_id, message_id, **options)
184
181
  return nil unless chat
185
-
182
+
186
183
  params = {
187
184
  chat_id: chat.id,
188
185
  from_chat_id: from_chat_id,
189
186
  message_id: message_id
190
187
  }.merge(options)
191
-
188
+
192
189
  @bot.api.call('forwardMessage', params)
193
190
  end
194
-
191
+
195
192
  def copy_message(from_chat_id, message_id, **options)
196
193
  return nil unless chat
197
-
194
+
198
195
  params = {
199
196
  chat_id: chat.id,
200
197
  from_chat_id: from_chat_id,
201
198
  message_id: message_id
202
199
  }.merge(options)
203
-
200
+
204
201
  @bot.api.call('copyMessage', params)
205
202
  end
206
-
207
- # Chat management
203
+
208
204
  def pin_message(message_id, **options)
209
205
  return nil unless chat
210
-
206
+
211
207
  params = { chat_id: chat.id, message_id: message_id }.merge(options)
212
208
  @bot.api.call('pinChatMessage', params)
213
209
  end
214
-
210
+
215
211
  def unpin_message(**options)
216
212
  return nil unless chat
217
-
213
+
218
214
  params = { chat_id: chat.id }.merge(options)
219
215
  @bot.api.call('unpinChatMessage', params)
220
216
  end
221
-
217
+
222
218
  def kick_chat_member(user_id, **options)
223
219
  return nil unless chat
224
-
220
+
225
221
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
226
222
  @bot.api.call('kickChatMember', params)
227
223
  end
228
-
224
+
229
225
  def ban_chat_member(user_id, **options)
230
226
  return nil unless chat
231
-
227
+
232
228
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
233
229
  @bot.api.call('banChatMember', params)
234
230
  end
235
-
231
+
236
232
  def unban_chat_member(user_id, **options)
237
233
  return nil unless chat
238
-
234
+
239
235
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
240
236
  @bot.api.call('unbanChatMember', params)
241
237
  end
242
-
238
+
243
239
  def get_chat_administrators(**options)
244
240
  return nil unless chat
245
-
241
+
246
242
  params = { chat_id: chat.id }.merge(options)
247
243
  @bot.api.call('getChatAdministrators', params)
248
244
  end
249
-
245
+
250
246
  def get_chat_members_count(**options)
251
247
  return nil unless chat
252
-
248
+
253
249
  params = { chat_id: chat.id }.merge(options)
254
250
  @bot.api.call('getChatMembersCount', params)
255
251
  end
256
-
252
+
257
253
  def get_chat(**options)
258
254
  return nil unless chat
259
-
255
+
260
256
  params = { chat_id: chat.id }.merge(options)
261
257
  @bot.api.call('getChat', params)
262
258
  end
263
-
264
- # Keyboard helpers
259
+
265
260
  def keyboard(&block)
266
- require 'telegem/markup/keyboard'
267
261
  Telegem::Markup.keyboard(&block)
268
262
  end
269
-
263
+
270
264
  def inline_keyboard(&block)
271
- require 'telegem/markup/keyboard'
272
265
  Telegem::Markup.inline(&block)
273
266
  end
274
-
267
+
275
268
  def reply_with_keyboard(text, keyboard_markup, **options)
276
269
  return nil unless chat
277
-
270
+
278
271
  reply_markup = keyboard_markup.is_a?(Hash) ? keyboard_markup : keyboard_markup.to_h
279
272
  reply(text, reply_markup: reply_markup, **options)
280
273
  end
281
-
274
+
282
275
  def reply_with_inline_keyboard(text, inline_markup, **options)
283
276
  return nil unless chat
284
-
277
+
285
278
  reply_markup = inline_markup.is_a?(Hash) ? inline_markup : inline_markup.to_h
286
279
  reply(text, reply_markup: reply_markup, **options)
287
280
  end
288
-
281
+
289
282
  def remove_keyboard(text = nil, **options)
290
283
  return nil unless chat
291
-
284
+
292
285
  reply_markup = Telegem::Markup.remove(**options.slice(:selective))
293
286
  if text
294
287
  reply(text, reply_markup: reply_markup, **options.except(:selective))
@@ -296,99 +289,92 @@ module Telegem
296
289
  reply_markup
297
290
  end
298
291
  end
299
-
292
+
300
293
  def edit_message_reply_markup(reply_markup, **options)
301
294
  return nil unless message && chat
302
-
295
+
303
296
  params = {
304
297
  chat_id: chat.id,
305
298
  message_id: message.message_id,
306
299
  reply_markup: reply_markup
307
300
  }.merge(options)
308
-
301
+
309
302
  @bot.api.call('editMessageReplyMarkup', params)
310
303
  end
311
-
312
- # Chat action shortcuts
304
+
313
305
  def typing(**options)
314
306
  send_chat_action('typing', **options)
315
307
  end
316
-
308
+
317
309
  def uploading_photo(**options)
318
310
  send_chat_action('upload_photo', **options)
319
311
  end
320
-
312
+
321
313
  def uploading_video(**options)
322
314
  send_chat_action('upload_video', **options)
323
315
  end
324
-
316
+
325
317
  def uploading_audio(**options)
326
318
  send_chat_action('upload_audio', **options)
327
319
  end
328
-
320
+
329
321
  def uploading_document(**options)
330
322
  send_chat_action('upload_document', **options)
331
323
  end
332
-
324
+
333
325
  def with_typing(&block)
334
- # Start typing action
335
326
  typing_request = typing
336
327
 
337
- # Execute block
338
328
  result = block.call
339
329
 
340
- # Return block's result (could be HTTPX request or nil)
341
330
  result
342
331
  end
343
-
344
- # Command detection
332
+
345
333
  def command?
346
334
  message&.command? || false
347
335
  end
348
-
336
+
349
337
  def command_args
350
338
  message&.command_args if command?
351
339
  end
352
-
353
- # Scene management
340
+
354
341
  def enter_scene(scene_name, **options)
355
342
  return nil unless @bot.scenes[scene_name]
356
-
343
+
357
344
  @scene = scene_name
358
345
  @bot.scenes[scene_name].enter(self, **options)
359
346
  end
360
-
347
+
361
348
  def leave_scene(**options)
362
349
  return nil unless @scene && @bot.scenes[@scene]
363
-
350
+
364
351
  scene_name = @scene
365
352
  @scene = nil
366
353
  @bot.scenes[scene_name].leave(self, **options)
367
354
  end
368
-
355
+
369
356
  def current_scene
370
357
  @bot.scenes[@scene] if @scene
371
358
  end
372
-
373
- # Utilities
359
+
374
360
  def logger
375
361
  @bot.logger
376
362
  end
377
-
363
+
378
364
  def raw_update
379
365
  @update._raw_data
380
366
  end
381
-
367
+
382
368
  def api
383
369
  @bot.api
384
370
  end
385
-
371
+
386
372
  def user_id
387
373
  from&.id
388
374
  end
389
-
375
+
390
376
  private
391
-
377
+
392
378
  def file_object?(obj)
393
379
  obj.is_a?(File) || obj.is_a?(StringIO) || obj.is_a?(Tempfile) ||
394
380
  (obj.is_a?(String) && File.exist?(obj))
File without changes