Class: Telegem::Core::Context
- Inherits:
-
Object
- Object
- Telegem::Core::Context
- Defined in:
- lib/core/context.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
Returns the value of attribute bot.
-
#match ⇒ Object
Returns the value of attribute match.
-
#scene ⇒ Object
Returns the value of attribute scene.
-
#session ⇒ Object
Returns the value of attribute session.
-
#state ⇒ Object
Returns the value of attribute state.
-
#update ⇒ Object
Returns the value of attribute update.
Instance Method Summary collapse
- #answer_callback_query(text: nil, show_alert: false, **options) ⇒ Object
- #answer_inline_query(results, **options) ⇒ Object
- #api ⇒ Object
- #ask(question, **options) ⇒ Object
- #audio(audio, caption: nil, **options) ⇒ Object
- #ban_chat_member(user_id, **options) ⇒ Object
- #callback_query ⇒ Object
- #callback_query? ⇒ Boolean
- #caption ⇒ Object
- #caption_entities ⇒ Object
- #channel_post? ⇒ Boolean
- #chat ⇒ Object
- #command? ⇒ Boolean
- #command_args ⇒ Object
- #command_name ⇒ Object
- #copy_message(from_chat_id, message_id, **options) ⇒ Object
- #current_scene ⇒ Object
- #data ⇒ Object
- #delete_message(message_id = nil) ⇒ Object
- #document(document, caption: nil, **options) ⇒ Object
- #download_file(file_id, destination_path = nil) ⇒ Object
- #edit_date ⇒ Object
- #edit_message_reply_markup(reply_markup, **options) ⇒ Object
- #edit_message_text(text, **options) ⇒ Object
- #enter_scene(scene_name, **options) ⇒ Object
- #entities ⇒ Object
- #forward_message(from_chat_id, message_id, **options) ⇒ Object
- #from ⇒ Object
- #get_chat(**options) ⇒ Object
- #get_chat_administrators(**options) ⇒ Object
- #get_chat_members_count(**options) ⇒ Object
- #has_media? ⇒ Boolean
- #in_scene? ⇒ Boolean
-
#initialize(update, bot) ⇒ Context
constructor
A new instance of Context.
- #inline_keyboard(&block) ⇒ Object
- #inline_query ⇒ Object
- #inline_query? ⇒ Boolean
- #is_edited? ⇒ Boolean
- #keyboard(&block) ⇒ Object
- #kick_chat_member(user_id, **options) ⇒ Object
- #leave_scene(**options) ⇒ Object
- #location(latitude, longitude, **options) ⇒ Object
- #logger ⇒ Object
- #media_type ⇒ Object
- #message ⇒ Object
- #message_date ⇒ Object
- #message_id ⇒ Object
- #next_step(step_name = nil) ⇒ Object
- #photo(photo, caption: nil, **options) ⇒ Object
- #pin_message(message_id, **options) ⇒ Object
- #query ⇒ Object
- #raw_update ⇒ Object
- #remove_keyboard(text = nil, **options) ⇒ Object
- #replied_chat ⇒ Object
- #replied_from ⇒ Object
- #replied_message ⇒ Object
- #replied_text ⇒ Object
- #reply(text, **options) ⇒ Object
- #reply? ⇒ Boolean
- #reply_with_inline_keyboard(text, inline_markup, **options) ⇒ Object
- #reply_with_keyboard(text, keyboard_markup, **options) ⇒ Object
- #scene_data ⇒ Object
- #send_chat_action(action, **options) ⇒ Object
- #sticker(sticker, **options) ⇒ Object
- #typing(**options) ⇒ Object
- #unban_chat_member(user_id, **options) ⇒ Object
- #unpin_message(**options) ⇒ Object
- #update_type ⇒ Object
- #uploading_audio(**options) ⇒ Object
- #uploading_document(**options) ⇒ Object
- #uploading_photo(**options) ⇒ Object
- #uploading_video(**options) ⇒ Object
- #user_id ⇒ Object
- #video(video, caption: nil, **options) ⇒ Object
- #voice(voice, caption: nil, **options) ⇒ Object
- #with_typing(&block) ⇒ Object
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
#bot ⇒ Object
Returns the value of attribute bot.
6 7 8 |
# File 'lib/core/context.rb', line 6 def bot @bot end |
#match ⇒ Object
Returns the value of attribute match.
6 7 8 |
# File 'lib/core/context.rb', line 6 def match @match end |
#scene ⇒ Object
Returns the value of attribute scene.
6 7 8 |
# File 'lib/core/context.rb', line 6 def scene @scene end |
#session ⇒ Object
Returns the value of attribute session.
6 7 8 |
# File 'lib/core/context.rb', line 6 def session @session end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/core/context.rb', line 6 def state @state end |
#update ⇒ Object
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, **) return nil unless callback_query params = { callback_query_id: callback_query.id, show_alert: show_alert }.merge() 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, **) return nil unless inline_query params = { inline_query_id: inline_query.id, results: results.to_json }.merge() @bot.api.call('answerInlineQuery', params) end |
#api ⇒ Object
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, **) scene_data = session[:telegem_scene] if scene_data scene_data[:waiting_for_response] = true scene_data[:last_question] = question end reply(question, **) 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, **) return nil unless chat params = { chat_id: chat.id, caption: caption }.merge() 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, **) return nil unless chat params = { chat_id: chat.id, user_id: user_id }.merge() @bot.api.call('banChatMember', params) end |
#callback_query ⇒ Object
21 22 23 |
# File 'lib/core/context.rb', line 21 def callback_query @update.callback_query end |
#callback_query? ⇒ Boolean
113 114 115 |
# File 'lib/core/context.rb', line 113 def callback_query? update_type == :callback_query end |
#caption ⇒ Object
77 78 79 |
# File 'lib/core/context.rb', line 77 def caption &.caption end |
#caption_entities ⇒ Object
73 74 75 |
# File 'lib/core/context.rb', line 73 def caption_entities &.caption_entities || [] end |
#channel_post? ⇒ Boolean
109 110 111 |
# File 'lib/core/context.rb', line 109 def channel_post? update_type == :channel_post end |
#chat ⇒ Object
33 34 35 |
# File 'lib/core/context.rb', line 33 def chat &.chat || callback_query&.&.chat end |
#command? ⇒ Boolean
451 452 453 |
# File 'lib/core/context.rb', line 451 def command? &.command? || false end |
#command_args ⇒ Object
455 456 457 |
# File 'lib/core/context.rb', line 455 def command_args &.command_args if command? end |
#command_name ⇒ Object
57 58 59 |
# File 'lib/core/context.rb', line 57 def command_name &.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 (from_chat_id, , **) return nil unless chat params = { chat_id: chat.id, from_chat_id: from_chat_id, message_id: }.merge() @bot.api.call('copyMessage', params) end |
#current_scene ⇒ Object
415 416 417 |
# File 'lib/core/context.rb', line 415 def current_scene @session[:telegem_scene]&.[](:id) end |
#data ⇒ Object
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 ( = nil) mid = || &. 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, **) return nil unless chat params = { chat_id: chat.id, caption: caption }.merge() 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_date ⇒ Object
53 54 55 |
# File 'lib/core/context.rb', line 53 def edit_date &.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 (reply_markup, **) return nil unless && chat params = { chat_id: chat.id, message_id: ., reply_markup: reply_markup }.merge() @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 (text, **) return nil unless && chat params = { chat_id: chat.id, message_id: ., text: text }.merge() @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, **) scene = @bot.scenes[scene_name] return nil unless scene leave_scene if in_scene? scene.enter(self, [:step], .except(:step)) scene_name end |
#entities ⇒ Object
69 70 71 |
# File 'lib/core/context.rb', line 69 def entities &.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 (from_chat_id, , **) return nil unless chat params = { chat_id: chat.id, from_chat_id: from_chat_id, message_id: }.merge() @bot.api.call('forwardMessage', params) end |
#from ⇒ Object
29 30 31 |
# File 'lib/core/context.rb', line 29 def from &.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(**) return nil unless chat params = { chat_id: chat.id }.merge() @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(**) return nil unless chat params = { chat_id: chat.id }.merge() @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(**) return nil unless chat params = { chat_id: chat.id }.merge() @bot.api.call('getChatMembersCount', params) end |
#has_media? ⇒ Boolean
61 62 63 |
# File 'lib/core/context.rb', line 61 def has_media? &.has_media? || false end |
#in_scene? ⇒ 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_query ⇒ Object
25 26 27 |
# File 'lib/core/context.rb', line 25 def inline_query @update.inline_query end |
#inline_query? ⇒ Boolean
117 118 119 |
# File 'lib/core/context.rb', line 117 def inline_query? update_type == :inline_query end |
#is_edited? ⇒ Boolean
105 106 107 |
# File 'lib/core/context.rb', line 105 def is_edited? !!@update. 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, **) return nil unless chat params = { chat_id: chat.id, user_id: user_id }.merge() @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(**) 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, [: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, **) return nil unless chat params = { chat_id: chat.id, latitude: latitude, longitude: longitude }.merge() @bot.api.call('sendLocation', params) end |
#logger ⇒ Object
467 468 469 |
# File 'lib/core/context.rb', line 467 def logger @bot.logger end |
#media_type ⇒ Object
65 66 67 |
# File 'lib/core/context.rb', line 65 def media_type &.media_type end |
#message ⇒ Object
17 18 19 |
# File 'lib/core/context.rb', line 17 def @update. end |
#message_date ⇒ Object
49 50 51 |
# File 'lib/core/context.rb', line 49 def &.date end |
#message_id ⇒ Object
45 46 47 |
# File 'lib/core/context.rb', line 45 def &. 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, **) return nil unless chat params = { chat_id: chat.id, caption: caption }.merge() 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 (, **) return nil unless chat params = { chat_id: chat.id, message_id: }.merge() @bot.api.call('pinChatMessage', params) end |
#query ⇒ Object
41 42 43 |
# File 'lib/core/context.rb', line 41 def query inline_query&.query end |
#raw_update ⇒ Object
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, **) return nil unless chat reply_markup = Telegem::Markup.remove(**.slice(:selective)) if text reply(text, reply_markup: reply_markup, **.except(:selective)) else reply_markup end end |
#replied_chat ⇒ Object
97 98 99 |
# File 'lib/core/context.rb', line 97 def replied_chat &.chat end |
#replied_from ⇒ Object
93 94 95 |
# File 'lib/core/context.rb', line 93 def replied_from &.from end |
#replied_message ⇒ Object
85 86 87 |
# File 'lib/core/context.rb', line 85 def &. end |
#replied_text ⇒ Object
89 90 91 |
# File 'lib/core/context.rb', line 89 def replied_text &.text end |
#reply(text, **options) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/core/context.rb', line 121 def reply(text, **) return nil unless chat params = { chat_id: chat.id, text: text }.merge() @bot.api.call('sendMessage', params) end |
#reply? ⇒ Boolean
81 82 83 |
# File 'lib/core/context.rb', line 81 def reply? &.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, **) return nil unless chat reply_markup = inline_markup.is_a?(Hash) ? inline_markup : inline_markup.to_h reply(text, reply_markup: reply_markup, **) 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, **) return nil unless chat reply_markup = keyboard_markup.is_a?(Hash) ? keyboard_markup : keyboard_markup.to_h reply(text, reply_markup: reply_markup, **) end |
#scene_data ⇒ Object
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, **) return nil unless chat params = { chat_id: chat.id, action: action }.merge() @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, **) return nil unless chat params = { chat_id: chat.id, sticker: sticker }.merge() @bot.api.call('sendSticker', params) end |
#typing(**options) ⇒ Object
385 386 387 |
# File 'lib/core/context.rb', line 385 def typing(**) send_chat_action('typing', **) 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, **) return nil unless chat params = { chat_id: chat.id, user_id: user_id }.merge() @bot.api.call('unbanChatMember', params) end |
#unpin_message(**options) ⇒ Object
291 292 293 294 295 296 |
# File 'lib/core/context.rb', line 291 def (**) return nil unless chat params = { chat_id: chat.id }.merge() @bot.api.call('unpinChatMessage', params) end |
#update_type ⇒ Object
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(**) send_chat_action('upload_audio', **) end |
#uploading_document(**options) ⇒ Object
401 402 403 |
# File 'lib/core/context.rb', line 401 def uploading_document(**) send_chat_action('upload_document', **) end |
#uploading_photo(**options) ⇒ Object
389 390 391 |
# File 'lib/core/context.rb', line 389 def uploading_photo(**) send_chat_action('upload_photo', **) end |
#uploading_video(**options) ⇒ Object
393 394 395 |
# File 'lib/core/context.rb', line 393 def uploading_video(**) send_chat_action('upload_video', **) end |
#user_id ⇒ Object
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, **) return nil unless chat params = { chat_id: chat.id, caption: caption }.merge() 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, **) return nil unless chat params = { chat_id: chat.id, caption: caption }.merge() 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 |