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.



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

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.



4
5
6
# File 'lib/core/context.rb', line 4

def bot
  @bot
end

#matchObject

Returns the value of attribute match.



4
5
6
# File 'lib/core/context.rb', line 4

def match
  @match
end

#sceneObject

Returns the value of attribute scene.



4
5
6
# File 'lib/core/context.rb', line 4

def scene
  @scene
end

#sessionObject

Returns the value of attribute session.



4
5
6
# File 'lib/core/context.rb', line 4

def session
  @session
end

#stateObject

Returns the value of attribute state.



4
5
6
# File 'lib/core/context.rb', line 4

def state
  @state
end

#updateObject

Returns the value of attribute update.



4
5
6
# File 'lib/core/context.rb', line 4

def update
  @update
end

Instance Method Details

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



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/core/context.rb', line 76

def answer_callback_query(text: nil, show_alert: false, **options)
  return Async::Task.new(nil) unless callback_query

  Async do
    params = {
      callback_query_id: callback_query.id,
      show_alert: show_alert
    }.merge(options)

    params[:text] = text if text
    await @bot.api.call('answerCallbackQuery', params)
  end
end

#answer_inline_query(results, **options) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/core/context.rb', line 90

def answer_inline_query(results, **options)
  return Async::Task.new(nil) unless inline_query

  Async do
    params = {
      inline_query_id: inline_query.id,
      results: results.to_json
    }.merge(options)

    await @bot.api.call('answerInlineQuery', params)
  end
end

#apiObject



379
380
381
# File 'lib/core/context.rb', line 379

def api
  @bot.api
end

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



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

def audio(audio, caption: nil, **options)
  Async do
    params = { chat_id: chat.id, caption: caption }.merge(options)

    if file_object?(audio)
      await @bot.api.upload('sendAudio', params.merge(audio: audio))
    else
      await @bot.api.call('sendAudio', params.merge(audio: audio))
    end
  end
end

#ban_chat_member(user_id, **options) ⇒ Object



223
224
225
226
227
228
# File 'lib/core/context.rb', line 223

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

#callback_queryObject



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

def callback_query
  @update.callback_query
end

#chatObject



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

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

#command?Boolean

Command detection

Returns:

  • (Boolean)


338
339
340
# File 'lib/core/context.rb', line 338

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

#command_argsObject



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

def command_args
  message&.command_args if command?
end

#current_sceneObject



366
367
368
# File 'lib/core/context.rb', line 366

def current_scene
  @bot.scenes[@scene] if @scene
end

#dataObject



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

def data
  callback_query&.data
end

#delete_message(message_id = nil) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/core/context.rb', line 67

def delete_message(message_id = nil)
  mid = message_id || message&.message_id
  return Async::Task.new(nil) unless mid && chat

  Async do
    await @bot.api.call('deleteMessage', chat_id: chat.id, message_id: mid)
  end
end

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



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/core/context.rb', line 116

def document(document, caption: nil, **options)
  Async do
    params = { chat_id: chat.id, caption: caption }.merge(options)

    if file_object?(document)
      await @bot.api.upload('sendDocument', params.merge(document: document))
    else
      await @bot.api.call('sendDocument', params.merge(document: document))
    end
  end
end

#edit_message_reply_markup(reply_markup, **options) ⇒ Object



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

def edit_message_reply_markup(reply_markup, **options)
  return Async::Task.new(nil) unless message

  Async do
    params = {
      chat_id: chat.id,
      message_id: message.message_id,
      reply_markup: reply_markup
    }.merge(options)

    await @bot.api.call('editMessageReplyMarkup', params)
  end
end

#edit_message_text(text, **options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/core/context.rb', line 53

def edit_message_text(text, **options)
  return Async::Task.new(nil) unless message

  Async do
    params = {
      chat_id: chat.id,
      message_id: message.message_id,
      text: text
    }.merge(options)

    await @bot.api.call('editMessageText', params)
  end
end

#enter_scene(scene_name, **options) ⇒ Object

Scene management



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

def enter_scene(scene_name, **options)
  Async do
    @scene = scene_name
    if @bot.scenes[scene_name]
      await @bot.scenes[scene_name].enter(self, **options)
    end
  end
end

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



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/core/context.rb', line 190

def forward_message(from_chat_id, message_id, **options)
  Async do
    params = { 
      chat_id: chat.id, 
      from_chat_id: from_chat_id, 
      message_id: message_id 
    }.merge(options)

    await @bot.api.call('forwardMessage', params)
  end
end

#fromObject

Entity shortcuts



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



251
252
253
254
255
256
# File 'lib/core/context.rb', line 251

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

#get_chat_administrators(**options) ⇒ Object



237
238
239
240
241
242
# File 'lib/core/context.rb', line 237

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

#get_chat_members_count(**options) ⇒ Object



244
245
246
247
248
249
# File 'lib/core/context.rb', line 244

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

#inline_keyboard(&block) ⇒ Object



264
265
266
267
# File 'lib/core/context.rb', line 264

def inline_keyboard(&block)
  require 'telegem/markup/keyboard'
  Telegem::Markup.inline(&block)
end

#inline_queryObject



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

def inline_query
  @update.inline_query
end

#keyboard(&block) ⇒ Object

Keyboard helpers



259
260
261
262
# File 'lib/core/context.rb', line 259

def keyboard(&block)
  require 'lib/markup/keyboard'
  Telegem::Markup.keyboard(&block)
end

#kick_chat_member(user_id, **options) ⇒ Object



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

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

#leave_scene(**options) ⇒ Object



356
357
358
359
360
361
362
363
364
# File 'lib/core/context.rb', line 356

def leave_scene(**options)
  Async do
    return unless @scene
    if @bot.scenes[@scene]
      await @bot.scenes[@scene].leave(self, **options)
    end
    @scene = nil
  end
end

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



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

def location(latitude, longitude, **options)
  Async do
    params = { 
      chat_id: chat.id, 
      latitude: latitude, 
      longitude: longitude 
    }.merge(options)

    await @bot.api.call('sendLocation', params)
  end
end

#loggerObject

Utilities



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

def logger
  @bot.logger
end

#messageObject

Message shortcuts



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

def message
  @update.message
end

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

Media methods



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/core/context.rb', line 104

def photo(photo, caption: nil, **options)
  Async do
    params = { chat_id: chat.id, caption: caption }.merge(options)

    if file_object?(photo)
      await @bot.api.upload('sendPhoto', params.merge(photo: photo))
    else
      await @bot.api.call('sendPhoto', params.merge(photo: photo))
    end
  end
end

#pin_message(message_id, **options) ⇒ Object



202
203
204
205
206
207
# File 'lib/core/context.rb', line 202

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

#queryObject



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

def query
  inline_query&.query
end

#raw_updateObject



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

def raw_update
  @update._raw_data
end

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



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

def remove_keyboard(text = nil, **options)
  Async do
    reply_markup = Telegem::Markup.remove(**options.slice(:selective))
    if text
      await reply(text, reply_markup: reply_markup, **options.except(:selective))
    else
      reply_markup
    end
  end
end

#reply(text, **options) ⇒ Object

Action methods



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

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

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



276
277
278
279
280
281
# File 'lib/core/context.rb', line 276

def reply_with_inline_keyboard(text, inline_markup, **options)
  Async do
    reply_markup = inline_markup.is_a?(Hash) ? inline_markup : inline_markup.to_h
    await reply(text, reply_markup: reply_markup, **options)
  end
end

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



269
270
271
272
273
274
# File 'lib/core/context.rb', line 269

def reply_with_keyboard(text, keyboard_markup, **options)
  Async do
    reply_markup = keyboard_markup.is_a?(Hash) ? keyboard_markup : keyboard_markup.to_h
    await reply(text, reply_markup: reply_markup, **options)
  end
end

#send_chat_action(action, **options) ⇒ Object



183
184
185
186
187
188
# File 'lib/core/context.rb', line 183

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

#sticker(sticker, **options) ⇒ Object



164
165
166
167
168
169
# File 'lib/core/context.rb', line 164

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

#typing(**options) ⇒ Object

Chat action shortcuts



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

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

#unban_chat_member(user_id, **options) ⇒ Object



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

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

#unpin_message(**options) ⇒ Object



209
210
211
212
213
214
# File 'lib/core/context.rb', line 209

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

#uploading_audio(**options) ⇒ Object



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

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

#uploading_document(**options) ⇒ Object



325
326
327
# File 'lib/core/context.rb', line 325

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

#uploading_photo(**options) ⇒ Object



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

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

#uploading_video(**options) ⇒ Object



317
318
319
# File 'lib/core/context.rb', line 317

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

#user_idObject



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

def user_id
  from&.id
end

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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/core/context.rb', line 140

def video(video, caption: nil, **options)
  Async do
    params = { chat_id: chat.id, caption: caption }.merge(options)

    if file_object?(video)
      await @bot.api.upload('sendVideo', params.merge(video: video))
    else
      await @bot.api.call('sendVideo', params.merge(video: video))
    end
  end
end

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



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/core/context.rb', line 152

def voice(voice, caption: nil, **options)
  Async do
    params = { chat_id: chat.id, caption: caption }.merge(options)

    if file_object?(voice)
      await @bot.api.upload('sendVoice', params.merge(voice: voice))
    else
      await @bot.api.call('sendVoice', params.merge(voice: voice))
    end
  end
end

#with_typing(&block) ⇒ Object



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

def with_typing(&block)
  Async do
    await typing
    result = block.call
    result.is_a?(Async::Task) ? await(result) : result
  end
end