Class: Telegem::Core::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/core/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(update, bot) ⇒ Context

Returns a new instance of Context.



8
9
10
11
12
13
14
15
# File 'lib/core/context.rb', line 8

def initialize(update, bot)
  @update = update
  @bot = bot
  @state = {}
  @session = {}
  @match = nil
  @scene = nil
end

Instance Attribute Details

#botObject

Returns the value of attribute bot.



6
7
8
# File 'lib/core/context.rb', line 6

def bot
  @bot
end

#matchObject

Returns the value of attribute match.



6
7
8
# File 'lib/core/context.rb', line 6

def match
  @match
end

#sceneObject

Returns the value of attribute scene.



6
7
8
# File 'lib/core/context.rb', line 6

def scene
  @scene
end

#sessionObject

Returns the value of attribute session.



6
7
8
# File 'lib/core/context.rb', line 6

def session
  @session
end

#stateObject

Returns the value of attribute state.



6
7
8
# File 'lib/core/context.rb', line 6

def state
  @state
end

#updateObject

Returns the value of attribute update.



6
7
8
# File 'lib/core/context.rb', line 6

def update
  @update
end

Instance Method Details

#answer_callback_query(text: nil, show_alert: false, **options) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/core/context.rb', line 203

def answer_callback_query(text: nil, show_alert: false, **options)
  return nil unless callback_query
  
  params = {
    callback_query_id: callback_query.id,
    show_alert: show_alert
  }.merge(options)
  
  params[:text] = text if text
  @bot.api.call('answerCallbackQuery', params)
end

#answer_inline_query(results, **options) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/core/context.rb', line 215

def answer_inline_query(results, **options)
  return nil unless inline_query
  
  params = {
    inline_query_id: inline_query.id,
    results: results.to_json
  }.merge(options)
  
  @bot.api.call('answerInlineQuery', params)
end

#apiObject



537
538
539
# File 'lib/core/context.rb', line 537

def api
  @bot.api
end

#append_to_draft(draft_id = nil, content, **options) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/core/context.rb', line 149

def append_to_draft(draft_id = nil, content, **options)
  return nil unless chat
  
  draft_id ||= session[:telegem_draft_id]
  return nil unless draft_id
  
  rich_message = { blocks: [{ type: "paragraph", content: content }] }
  params = { chat_id: chat.id, draft_id: draft_id, rich_message: rich_message }.merge(options)
  @bot.api.call('sendRichMessageDraft', params)
end

#ask(question, **options) ⇒ Object



460
461
462
463
464
465
466
467
# File 'lib/core/context.rb', line 460

def ask(question, **options)
  scene_data = session[:telegem_scene]
  if scene_data
    scene_data[:waiting_for_response] = true 
    scene_data[:last_question] = question
  end 
  reply(question, **options) 
end

#audio(audio, caption: nil, **options) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/core/context.rb', line 250

def audio(audio, caption: nil, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, caption: caption }.merge(options)
  
  if file_object?(audio)
    @bot.api.upload('sendAudio', params.merge(audio: audio))
  else
    @bot.api.call('sendAudio', params.merge(audio: audio))
  end
end

#ban_chat_member(user_id, **options) ⇒ Object



361
362
363
364
365
366
# File 'lib/core/context.rb', line 361

def ban_chat_member(user_id, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
  @bot.api.call('banChatMember', params)
end

#business_connection_idObject

Essential 9.6 methods for context.rb



516
517
518
# File 'lib/core/context.rb', line 516

def business_connection_id
  message&.business_connection_id || @update.business_connection_id 
end

#callback_queryObject



21
22
23
# File 'lib/core/context.rb', line 21

def callback_query
  @update.callback_query
end

#callback_query?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/core/context.rb', line 113

def callback_query?
  update_type == :callback_query
end

#cancel_draft(draft_id = nil, **options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/core/context.rb', line 172

def cancel_draft(draft_id = nil, **options)
  return nil unless chat
  
  draft_id ||= session[:telegem_draft_id]
  return nil unless draft_id
  
  params = { chat_id: chat.id, draft_id: draft_id }.merge(options)
  result = @bot.api.call('cancelRichMessageDraft', params)
  session.delete(:telegem_draft_id)
  result
end

#captionObject



77
78
79
# File 'lib/core/context.rb', line 77

def caption 
  message&.caption 
end

#caption_entitiesObject



73
74
75
# File 'lib/core/context.rb', line 73

def caption_entities
  message&.caption_entities || []
end

#channel_post?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/core/context.rb', line 109

def channel_post?
  update_type == :channel_post
end

#chatObject



33
34
35
# File 'lib/core/context.rb', line 33

def chat
  message&.chat || callback_query&.message&.chat
end

#command?Boolean

Returns:

  • (Boolean)


499
500
501
# File 'lib/core/context.rb', line 499

def command?
  message&.command? || false
end

#command_argsObject



503
504
505
# File 'lib/core/context.rb', line 503

def command_args
  message&.command_args if command?
end

#command_nameObject



57
58
59
# File 'lib/core/context.rb', line 57

def command_name 
  message&.command_name
end

#copy_message(from_chat_id, message_id, **options) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/core/context.rb', line 328

def copy_message(from_chat_id, message_id, **options)
  return nil unless chat
  
  params = { 
    chat_id: chat.id, 
    from_chat_id: from_chat_id, 
    message_id: message_id 
  }.merge(options)
  
  @bot.api.call('copyMessage', params)
end

#current_sceneObject



471
472
473
# File 'lib/core/context.rb', line 471

def current_scene
  @session[:telegem_scene]&.[](:id)
end

#dataObject



37
38
39
# File 'lib/core/context.rb', line 37

def data
  callback_query&.data
end

#delete_message(message_id = nil) ⇒ Object



196
197
198
199
200
201
# File 'lib/core/context.rb', line 196

def delete_message(message_id = nil)
  mid = message_id || message&.message_id
  return nil unless mid && chat
  
  @bot.api.call('deleteMessage', chat_id: chat.id, message_id: mid)
end

#document(document, caption: nil, **options) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/core/context.rb', line 238

def document(document, caption: nil, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, caption: caption }.merge(options)
  
  if file_object?(document)
    @bot.api.upload('sendDocument', params.merge(document: document))
  else
    @bot.api.call('sendDocument', params.merge(document: document))
  end
end

#download_file(file_id, destination_path = nil) ⇒ Object



286
287
288
# File 'lib/core/context.rb', line 286

def download_file(file_id, destination_path = nil)
  @bot.api.download(file_id, destination_path) 
end

#edit_dateObject



53
54
55
# File 'lib/core/context.rb', line 53

def edit_date 
  message&.edit_date
end

#edit_message_reply_markup(reply_markup, **options) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
# File 'lib/core/context.rb', line 429

def edit_message_reply_markup(reply_markup, **options)
  return nil unless message && chat
  
  params = {
    chat_id: chat.id,
    message_id: message.message_id,
    reply_markup: reply_markup
  }.merge(options)
  
  @bot.api.call('editMessageReplyMarkup', params)
end

#edit_message_text(text, **options) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/core/context.rb', line 184

def edit_message_text(text, **options)
  return nil unless message && chat
  
  params = {
    chat_id: chat.id,
    message_id: message.message_id,
    text: text
  }.merge(options)
  
  @bot.api.call('editMessageText', params)
end

#enter_scene(scene_name, **options) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/core/context.rb', line 507

def enter_scene(scene_name, **options)
  scene = @bot.scenes[scene_name]
  return nil unless scene
  leave_scene if in_scene?
  scene.enter(self, options[:step], options.except(:step))
   scene_name
end

#entitiesObject



69
70
71
# File 'lib/core/context.rb', line 69

def entities
  message&.entities || []
end

#forward_message(from_chat_id, message_id, **options) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/core/context.rb', line 316

def forward_message(from_chat_id, message_id, **options)
  return nil unless chat
  
  params = { 
    chat_id: chat.id, 
    from_chat_id: from_chat_id, 
    message_id: message_id 
  }.merge(options)
  
  @bot.api.call('forwardMessage', params)
end

#fromObject



29
30
31
# File 'lib/core/context.rb', line 29

def from
  message&.from || callback_query&.from || inline_query&.from
end

#get_chat(**options) ⇒ Object



389
390
391
392
393
394
# File 'lib/core/context.rb', line 389

def get_chat(**options)
  return nil unless chat
  
  params = { chat_id: chat.id }.merge(options)
  @bot.api.call('getChat', params)
end

#get_chat_administrators(**options) ⇒ Object



375
376
377
378
379
380
# File 'lib/core/context.rb', line 375

def get_chat_administrators(**options)
  return nil unless chat
  
  params = { chat_id: chat.id }.merge(options)
  @bot.api.call('getChatAdministrators', params)
end

#get_chat_members_count(**options) ⇒ Object



382
383
384
385
386
387
# File 'lib/core/context.rb', line 382

def get_chat_members_count(**options)
  return nil unless chat
  
  params = { chat_id: chat.id }.merge(options)
  @bot.api.call('getChatMembersCount', params)
end

#has_media?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/core/context.rb', line 61

def has_media?
  message&.has_media? || false
end

#in_scene?Boolean

Returns:

  • (Boolean)


474
475
476
# File 'lib/core/context.rb', line 474

def in_scene?
  !current_scene.nil?
end

#inline_keyboard(&block) ⇒ Object



400
401
402
# File 'lib/core/context.rb', line 400

def inline_keyboard(&block)
  Telegem::Markup.inline(&block)
end

#inline_queryObject



25
26
27
# File 'lib/core/context.rb', line 25

def inline_query
  @update.inline_query
end

#inline_query?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/core/context.rb', line 117

def inline_query?
  update_type == :inline_query
end

#is_edited?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/core/context.rb', line 105

def is_edited?
  !!@update.edited_message
end

#keyboard(&block) ⇒ Object



396
397
398
# File 'lib/core/context.rb', line 396

def keyboard(&block)
  Telegem::Markup.keyboard(&block)
end

#kick_chat_member(user_id, **options) ⇒ Object



354
355
356
357
358
359
# File 'lib/core/context.rb', line 354

def kick_chat_member(user_id, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
  @bot.api.call('kickChatMember', params)
end

#leave_scene(**options) ⇒ Object



477
478
479
480
481
482
483
484
485
486
# File 'lib/core/context.rb', line 477

def leave_scene(**options)
  scene_data = @session[:telegem_scene] 
  return unless scene_data 
  scene_id = scene_data[:id].to_sym 
  scene = @bot.scenes[scene_id] 
  result = scene&.leave(self, options[:reason] || :manual) 
  @session.delete(:telegem_scene)
  @scene = nil 
  result
end

#location(latitude, longitude, **options) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/core/context.rb', line 297

def location(latitude, longitude, **options)
  return nil unless chat
  
  params = { 
    chat_id: chat.id, 
    latitude: latitude, 
    longitude: longitude 
  }.merge(options)
  
  @bot.api.call('sendLocation', params)
end

#loggerObject



529
530
531
# File 'lib/core/context.rb', line 529

def logger
  @bot.logger
end

#managed_botObject



525
526
527
# File 'lib/core/context.rb', line 525

def managed_bot
@update.managed_bot
end

#media_typeObject



65
66
67
# File 'lib/core/context.rb', line 65

def media_type
  message&.media_type
end

#messageObject



17
18
19
# File 'lib/core/context.rb', line 17

def message
  @update.message
end

#message_dateObject



49
50
51
# File 'lib/core/context.rb', line 49

def message_date
  message&.date
end

#message_idObject



45
46
47
# File 'lib/core/context.rb', line 45

def message_id 
  message&.message_id
end

#next_step(step_name = nil) ⇒ Object



487
488
489
490
491
492
493
# File 'lib/core/context.rb', line 487

def next_step(step_name = nil)
 scene_data = @session[:telegem_scene]
 return unless scene_data
 scene_id = scene_data[:id].to_sym 
 scene = @bot.scenes[scene_id] 
 scene&.next_step(self, step_name) 
end

#photo(photo, caption: nil, **options) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/core/context.rb', line 226

def photo(photo, caption: nil, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, caption: caption }.merge(options)
  
  if file_object?(photo)
    @bot.api.upload('sendPhoto', params.merge(photo: photo))
  else
    @bot.api.call('sendPhoto', params.merge(photo: photo))
  end
end

#pin_message(message_id, **options) ⇒ Object



340
341
342
343
344
345
# File 'lib/core/context.rb', line 340

def pin_message(message_id, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, message_id: message_id }.merge(options)
  @bot.api.call('pinChatMessage', params)
end

#publish_draft(draft_id = nil, **options) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/core/context.rb', line 160

def publish_draft(draft_id = nil, **options)
  return nil unless chat
  
  draft_id ||= session[:telegem_draft_id]
  return nil unless draft_id
  
  params = { chat_id: chat.id, draft_id: draft_id }.merge(options)
  result = @bot.api.call('publishRichMessageDraft', params)
  session.delete(:telegem_draft_id)
  result
end

#queryObject



41
42
43
# File 'lib/core/context.rb', line 41

def query
  inline_query&.query
end

#raw_updateObject



533
534
535
# File 'lib/core/context.rb', line 533

def raw_update
  @update._raw_data
end

#remove_keyboard(text = nil, **options) ⇒ Object



418
419
420
421
422
423
424
425
426
427
# File 'lib/core/context.rb', line 418

def remove_keyboard(text = nil, **options)
  return nil unless chat
  
  reply_markup = Telegem::Markup.remove(**options.slice(:selective))
  if text
    reply(text, reply_markup: reply_markup, **options.except(:selective))
  else
    reply_markup
  end
end

#replied_chatObject



97
98
99
# File 'lib/core/context.rb', line 97

def replied_chat
  replied_message&.chat 
end

#replied_fromObject



93
94
95
# File 'lib/core/context.rb', line 93

def replied_from 
  replied_message&.from 
end

#replied_messageObject



85
86
87
# File 'lib/core/context.rb', line 85

def replied_message
  message&.reply_to_message 
end

#replied_textObject



89
90
91
# File 'lib/core/context.rb', line 89

def replied_text
  replied_message&.text
end

#reply(text, **options) ⇒ Object



121
122
123
124
125
126
# File 'lib/core/context.rb', line 121

def reply(text, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, text: text }.merge(options)
  @bot.api.call('sendMessage', params)
end

#reply?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/core/context.rb', line 81

def reply?
  message&.reply?
end

#reply_draft(text, **options) ⇒ Object



520
521
522
523
# File 'lib/core/context.rb', line 520

def reply_draft(text, **options)
return unless chat
@bot.api.call('sendMessageDraft', { chat_id: chat.id, text: text }.merge(options))
end

#reply_rich(rich_message, **options) ⇒ Object



128
129
130
131
132
133
# File 'lib/core/context.rb', line 128

def reply_rich(rich_message, **options)
 return nil unless chat
  
 params = { chat_id: chat.id, rich_message: rich_message }.merge(options)
 @bot.api.call('sendRichMessage', params)
end

#reply_with_inline_keyboard(text, inline_markup, **options) ⇒ Object



411
412
413
414
415
416
# File 'lib/core/context.rb', line 411

def reply_with_inline_keyboard(text, inline_markup, **options)
  return nil unless chat
  
  reply_markup = inline_markup.is_a?(Hash) ? inline_markup : inline_markup.to_h
  reply(text, reply_markup: reply_markup, **options)
end

#reply_with_keyboard(text, keyboard_markup, **options) ⇒ Object



404
405
406
407
408
409
# File 'lib/core/context.rb', line 404

def reply_with_keyboard(text, keyboard_markup, **options)
  return nil unless chat
  
  reply_markup = keyboard_markup.is_a?(Hash) ? keyboard_markup : keyboard_markup.to_h
  reply(text, reply_markup: reply_markup, **options)
end

#scene_dataObject



468
469
470
# File 'lib/core/context.rb', line 468

def scene_data 
  @session[:telegem_scene]&.[](:data) || {} 
end

#send_chat_action(action, **options) ⇒ Object



309
310
311
312
313
314
# File 'lib/core/context.rb', line 309

def send_chat_action(action, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, action: action }.merge(options)
  @bot.api.call('sendChatAction', params)
end

#start_draft(initial_text = "", **options) ⇒ Object

Draft streaming support (Bot API 10.1)



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/core/context.rb', line 136

def start_draft(initial_text = "", **options)
  return nil unless chat
  
  draft_id = "draft_#{chat.id}_#{Time.now.to_i}_#{rand(1000)}"
  session[:telegem_draft_id] = draft_id
  
  rich_message = { blocks: [{ type: "paragraph", content: initial_text }] }
  params = { chat_id: chat.id, draft_id: draft_id, rich_message: rich_message }.merge(options)
  @bot.api.call('sendRichMessageDraft', params)
  
  draft_id
end

#sticker(sticker, **options) ⇒ Object



290
291
292
293
294
295
# File 'lib/core/context.rb', line 290

def sticker(sticker, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, sticker: sticker }.merge(options)
  @bot.api.call('sendSticker', params)
end

#typing(**options) ⇒ Object



441
442
443
# File 'lib/core/context.rb', line 441

def typing(**options)
  send_chat_action('typing', **options)
end

#unban_chat_member(user_id, **options) ⇒ Object



368
369
370
371
372
373
# File 'lib/core/context.rb', line 368

def unban_chat_member(user_id, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, user_id: user_id }.merge(options)
  @bot.api.call('unbanChatMember', params)
end

#unpin_message(**options) ⇒ Object



347
348
349
350
351
352
# File 'lib/core/context.rb', line 347

def unpin_message(**options)
  return nil unless chat
  
  params = { chat_id: chat.id }.merge(options)
  @bot.api.call('unpinChatMessage', params)
end

#update_typeObject



101
102
103
# File 'lib/core/context.rb', line 101

def update_type
  @update.type 
end

#uploading_audio(**options) ⇒ Object



453
454
455
# File 'lib/core/context.rb', line 453

def uploading_audio(**options)
  send_chat_action('upload_audio', **options)
end

#uploading_document(**options) ⇒ Object



457
458
459
# File 'lib/core/context.rb', line 457

def uploading_document(**options)
  send_chat_action('upload_document', **options)
end

#uploading_photo(**options) ⇒ Object



445
446
447
# File 'lib/core/context.rb', line 445

def uploading_photo(**options)
  send_chat_action('upload_photo', **options)
end

#uploading_video(**options) ⇒ Object



449
450
451
# File 'lib/core/context.rb', line 449

def uploading_video(**options)
  send_chat_action('upload_video', **options)
end

#user_idObject



541
542
543
# File 'lib/core/context.rb', line 541

def user_id
  from&.id
end

#video(video, caption: nil, **options) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/core/context.rb', line 262

def video(video, caption: nil, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, caption: caption }.merge(options)
  
  if file_object?(video)
    @bot.api.upload('sendVideo', params.merge(video: video))
  else
    @bot.api.call('sendVideo', params.merge(video: video))
  end
end

#voice(voice, caption: nil, **options) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
# File 'lib/core/context.rb', line 274

def voice(voice, caption: nil, **options)
  return nil unless chat
  
  params = { chat_id: chat.id, caption: caption }.merge(options)
  
  if file_object?(voice)
    @bot.api.upload('sendVoice', params.merge(voice: voice))
  else
    @bot.api.call('sendVoice', params.merge(voice: voice))
  end
end

#with_typing(&block) ⇒ Object



494
495
496
497
# File 'lib/core/context.rb', line 494

def with_typing(&block)
  typing
  block.call
end