class Xasin::Telegram::Message
Attributes
Returns the {Chat} object this message was sent in.
Optional, command included in this message. Can be nil.
Whether a command already handled this message. Usually means that it should not be processed any further, in order to prevent multiple commands from acting on the same message and causing weird behaviors.
Returns the message ID of this message.
Optional argument, ID of the message that was replied to.
String, text of the message. Even if the message is not a String (i.e. a Sticker etc.), this will be at least an empty string.
Timestamp, from Telegram
, that the message was sent on.
Returns the {User} Object that sent this message.
Return whether or not parsing of the message was successful. TODO Actually perfom validity checks.
Public Class Methods
Initialize a message object. This will create a new message object with the given Telegram
“Message” Hash. It can be taken directly from the Telegram
API.
The Message
will automatically try to fetch the matching {Chat} and {User} that sent the message, and will also parse any additional metadata such as commands, stickers, etc.
# File lib/xasin/telegram/Message.rb, line 46 def initialize(handler, message_object) @handler = handler return if message_object.nil? @valid = true; @message_id = message_object[:message_id] @chat = handler[message_object[:chat]] @user = handler[message_object[:from]] @reply_to_id = message_object.dig(:reply_to_message, :id) @text = message_object[:text] || ""; @timestamp = Time.at(message_object[:date] || 0) m = /\/([\S]*)/.match(@text) @command = m[1] if m @handled = false end
Public Instance Methods
Try to delete this message. Wrapper for Telegram's deleteMessage function
# File lib/xasin/telegram/Message.rb, line 89 def delete! @handler.core.perform_post('deleteMessage', { chat_id: @chat.chat_id, message_id: @message_id } ); end
Edit the text of the message. Simple wrapper for the 'editMessageText' Telegram
API function, and will directly set the messge's text.
parse_mode is set to HTML.
# File lib/xasin/telegram/Message.rb, line 75 def edit_text(text) out_data = { chat_id: @chat.chat_id, message_id: @message_id, parse_mode: 'HTML', text: text } @handler.core.perform_post('editMessageText', out_data); end
Send a text message with it's reply set to this.
This will send a new message with given text, whose reply message ID is set to this message. Makes it easy to respond to certain events quite cleanly.
# File lib/xasin/telegram/Message.rb, line 103 def reply(text) @chat.send_message(text, reply_to: self) end
Send a message to the chat this message originated from.
This is a wrapper for message.chat.send_message, as it allows the Bot to easily respond to a {User}'s action in the same chat the user wrote it in. It will simply forward all arguments to {Chat#send_message}
# File lib/xasin/telegram/Message.rb, line 113 def send_message(text, **opts) @chat.send_message(text, **opts) end
# File lib/xasin/telegram/Message.rb, line 121 def to_i @message_id end
# File lib/xasin/telegram/Message.rb, line 117 def to_s @text end