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



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

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



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

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



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

def api
  @bot.api
end

#ask(question, **options) ⇒ Object



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

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



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/core/context.rb', line 194

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



305
306
307
308
309
310
# File 'lib/core/context.rb', line 305

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

#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

#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)


451
452
453
# File 'lib/core/context.rb', line 451

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

#command_argsObject



455
456
457
# File 'lib/core/context.rb', line 455

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



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

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



415
416
417
# File 'lib/core/context.rb', line 415

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



140
141
142
143
144
145
# File 'lib/core/context.rb', line 140

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



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

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



230
231
232
# File 'lib/core/context.rb', line 230

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



373
374
375
376
377
378
379
380
381
382
383
# File 'lib/core/context.rb', line 373

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



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/core/context.rb', line 128

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



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

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



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

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



333
334
335
336
337
338
# File 'lib/core/context.rb', line 333

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



319
320
321
322
323
324
# File 'lib/core/context.rb', line 319

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



326
327
328
329
330
331
# File 'lib/core/context.rb', line 326

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)


418
419
420
# File 'lib/core/context.rb', line 418

def in_scene?
  !current_scene.nil?
end

#inline_keyboard(&block) ⇒ Object



344
345
346
# File 'lib/core/context.rb', line 344

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



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

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

#kick_chat_member(user_id, **options) ⇒ Object



298
299
300
301
302
303
# File 'lib/core/context.rb', line 298

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



421
422
423
424
425
426
427
428
429
430
# File 'lib/core/context.rb', line 421

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



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/core/context.rb', line 241

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



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

def logger
  @bot.logger
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



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

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



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

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



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

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

#queryObject



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

def query
  inline_query&.query
end

#raw_updateObject



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

def raw_update
  @update._raw_data
end

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



362
363
364
365
366
367
368
369
370
371
# File 'lib/core/context.rb', line 362

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_with_inline_keyboard(text, inline_markup, **options) ⇒ Object



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

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



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

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



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

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

#send_chat_action(action, **options) ⇒ Object



253
254
255
256
257
258
# File 'lib/core/context.rb', line 253

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

#sticker(sticker, **options) ⇒ Object



234
235
236
237
238
239
# File 'lib/core/context.rb', line 234

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



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

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

#unban_chat_member(user_id, **options) ⇒ Object



312
313
314
315
316
317
# File 'lib/core/context.rb', line 312

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



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

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



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

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

#uploading_document(**options) ⇒ Object



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

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

#uploading_photo(**options) ⇒ Object



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

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

#uploading_video(**options) ⇒ Object



393
394
395
# File 'lib/core/context.rb', line 393

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

#user_idObject



479
480
481
# File 'lib/core/context.rb', line 479

def user_id
  from&.id
end

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



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/core/context.rb', line 206

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



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/core/context.rb', line 218

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



438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/core/context.rb', line 438

def with_typing(&block)
  thread = Thread.new do 
    while @typing_active
      typing
      sleep 5
    end 
  end 
  result = block.call
  @typing_active = false
  thread.join
  result
end