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
- #audio(audio, caption: nil, **options) ⇒ Object
- #ban_chat_member(user_id, **options) ⇒ Object
- #callback_query ⇒ Object
- #chat ⇒ Object
-
#command? ⇒ Boolean
Command detection.
- #command_args ⇒ Object
- #current_scene ⇒ Object
- #data ⇒ Object
- #delete_message(message_id = nil) ⇒ Object
- #document(document, caption: nil, **options) ⇒ Object
- #edit_message_reply_markup(reply_markup, **options) ⇒ Object
- #edit_message_text(text, **options) ⇒ Object
-
#enter_scene(scene_name, **options) ⇒ Object
Scene management.
- #forward_message(from_chat_id, message_id, **options) ⇒ Object
-
#from ⇒ Object
Entity shortcuts.
- #get_chat(**options) ⇒ Object
- #get_chat_administrators(**options) ⇒ Object
- #get_chat_members_count(**options) ⇒ Object
-
#initialize(update, bot) ⇒ Context
constructor
A new instance of Context.
- #inline_keyboard(&block) ⇒ Object
- #inline_query ⇒ Object
-
#keyboard(&block) ⇒ Object
Keyboard helpers.
- #kick_chat_member(user_id, **options) ⇒ Object
- #leave_scene(**options) ⇒ Object
- #location(latitude, longitude, **options) ⇒ Object
-
#logger ⇒ Object
Utilities.
-
#message ⇒ Object
Message shortcuts.
-
#photo(photo, caption: nil, **options) ⇒ Object
Media methods.
- #pin_message(message_id, **options) ⇒ Object
- #query ⇒ Object
- #raw_update ⇒ Object
- #remove_keyboard(text = nil, **options) ⇒ Object
-
#reply(text, **options) ⇒ Object
Action methods.
- #reply_with_inline_keyboard(text, inline_markup, **options) ⇒ Object
- #reply_with_keyboard(text, keyboard_markup, **options) ⇒ Object
- #send_chat_action(action, **options) ⇒ Object
- #sticker(sticker, **options) ⇒ Object
-
#typing(**options) ⇒ Object
Chat action shortcuts.
- #unban_chat_member(user_id, **options) ⇒ Object
- #unpin_message(**options) ⇒ 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.
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
#bot ⇒ Object
Returns the value of attribute bot.
4 5 6 |
# File 'lib/core/context.rb', line 4 def bot @bot end |
#match ⇒ Object
Returns the value of attribute match.
4 5 6 |
# File 'lib/core/context.rb', line 4 def match @match end |
#scene ⇒ Object
Returns the value of attribute scene.
4 5 6 |
# File 'lib/core/context.rb', line 4 def scene @scene end |
#session ⇒ Object
Returns the value of attribute session.
4 5 6 |
# File 'lib/core/context.rb', line 4 def session @session end |
#state ⇒ Object
Returns the value of attribute state.
4 5 6 |
# File 'lib/core/context.rb', line 4 def state @state end |
#update ⇒ Object
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, **) return Async::Task.new(nil) unless callback_query Async do params = { callback_query_id: callback_query.id, show_alert: show_alert }.merge() 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, **) return Async::Task.new(nil) unless inline_query Async do params = { inline_query_id: inline_query.id, results: results.to_json }.merge() await @bot.api.call('answerInlineQuery', params) end end |
#api ⇒ Object
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, **) Async do params = { chat_id: chat.id, caption: caption }.merge() 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, **) Async do params = { chat_id: chat.id, user_id: user_id }.merge() await @bot.api.call('banChatMember', params) end end |
#callback_query ⇒ Object
20 21 22 |
# File 'lib/core/context.rb', line 20 def callback_query @update.callback_query end |
#chat ⇒ Object
33 34 35 |
# File 'lib/core/context.rb', line 33 def chat &.chat || callback_query&.&.chat end |
#command? ⇒ Boolean
Command detection
338 339 340 |
# File 'lib/core/context.rb', line 338 def command? &.command? || false end |
#command_args ⇒ Object
342 343 344 |
# File 'lib/core/context.rb', line 342 def command_args &.command_args if command? end |
#current_scene ⇒ Object
366 367 368 |
# File 'lib/core/context.rb', line 366 def current_scene @bot.scenes[@scene] if @scene 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
67 68 69 70 71 72 73 74 |
# File 'lib/core/context.rb', line 67 def ( = nil) mid = || &. 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, **) Async do params = { chat_id: chat.id, caption: caption }.merge() 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 (reply_markup, **) return Async::Task.new(nil) unless Async do params = { chat_id: chat.id, message_id: ., reply_markup: reply_markup }.merge() 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 (text, **) return Async::Task.new(nil) unless Async do params = { chat_id: chat.id, message_id: ., text: text }.merge() 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, **) Async do @scene = scene_name if @bot.scenes[scene_name] await @bot.scenes[scene_name].enter(self, **) 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 (from_chat_id, , **) Async do params = { chat_id: chat.id, from_chat_id: from_chat_id, message_id: }.merge() await @bot.api.call('forwardMessage', params) end end |
#from ⇒ Object
Entity shortcuts
29 30 31 |
# File 'lib/core/context.rb', line 29 def from &.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(**) Async do params = { chat_id: chat.id }.merge() 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(**) Async do params = { chat_id: chat.id }.merge() 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(**) Async do params = { chat_id: chat.id }.merge() 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_query ⇒ Object
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, **) Async do params = { chat_id: chat.id, user_id: user_id }.merge() 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(**) Async do return unless @scene if @bot.scenes[@scene] await @bot.scenes[@scene].leave(self, **) 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, **) Async do params = { chat_id: chat.id, latitude: latitude, longitude: longitude }.merge() await @bot.api.call('sendLocation', params) end end |
#logger ⇒ Object
Utilities
371 372 373 |
# File 'lib/core/context.rb', line 371 def logger @bot.logger end |
#message ⇒ Object
Message shortcuts
16 17 18 |
# File 'lib/core/context.rb', line 16 def @update. 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, **) Async do params = { chat_id: chat.id, caption: caption }.merge() 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 (, **) Async do params = { chat_id: chat.id, message_id: }.merge() await @bot.api.call('pinChatMessage', params) end end |
#query ⇒ Object
41 42 43 |
# File 'lib/core/context.rb', line 41 def query inline_query&.query end |
#raw_update ⇒ Object
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, **) Async do reply_markup = Telegem::Markup.remove(**.slice(:selective)) if text await reply(text, reply_markup: reply_markup, **.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, **) Async do params = { chat_id: chat.id, text: text }.merge() 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, **) Async do reply_markup = inline_markup.is_a?(Hash) ? inline_markup : inline_markup.to_h await reply(text, reply_markup: reply_markup, **) 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, **) Async do reply_markup = keyboard_markup.is_a?(Hash) ? keyboard_markup : keyboard_markup.to_h await reply(text, reply_markup: reply_markup, **) 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, **) Async do params = { chat_id: chat.id, action: action }.merge() 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, **) Async do params = { chat_id: chat.id, sticker: sticker }.merge() 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(**) send_chat_action('typing', **) 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, **) Async do params = { chat_id: chat.id, user_id: user_id }.merge() 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 (**) Async do params = { chat_id: chat.id }.merge() 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(**) send_chat_action('upload_audio', **) end |
#uploading_document(**options) ⇒ Object
325 326 327 |
# File 'lib/core/context.rb', line 325 def uploading_document(**) send_chat_action('upload_document', **) end |
#uploading_photo(**options) ⇒ Object
313 314 315 |
# File 'lib/core/context.rb', line 313 def uploading_photo(**) send_chat_action('upload_photo', **) end |
#uploading_video(**options) ⇒ Object
317 318 319 |
# File 'lib/core/context.rb', line 317 def uploading_video(**) send_chat_action('upload_video', **) end |
#user_id ⇒ Object
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, **) Async do params = { chat_id: chat.id, caption: caption }.merge() 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, **) Async do params = { chat_id: chat.id, caption: caption }.merge() 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 |