Class: XNM::Telegram::Message
- Inherits:
-
Object
- Object
- XNM::Telegram::Message
- Defined in:
- lib/xnm/telegram/Message.rb
Instance Attribute Summary collapse
-
#chat ⇒ Object
readonly
Returns the Chat object this message was sent in.
-
#command ⇒ Object
readonly
Optional, command included in this message.
-
#handled ⇒ Object
Whether a command already handled this message.
-
#message_id ⇒ Object
readonly
Returns the message ID of this message.
-
#reply_to_id ⇒ Object
readonly
Optional argument, ID of the message that was replied to.
-
#text ⇒ Object
readonly
String, text of the message.
-
#timestamp ⇒ Object
readonly
Timestamp, from Telegram, that the message was sent on.
-
#user ⇒ Object
readonly
Returns the User Object that sent this message.
-
#valid ⇒ Object
readonly
Return whether or not parsing of the message was successful.
Instance Method Summary collapse
-
#delete! ⇒ Object
Try to delete this message.
-
#edit_text(text) ⇒ Object
(also: #text=)
Edit the text of the message.
-
#initialize(handler, message_object) ⇒ Message
constructor
Initialize a message object.
-
#reply(text) ⇒ Object
Send a text message with it’s reply set to this.
-
#send_message(text, **opts) ⇒ Object
Send a message to the chat this message originated from.
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(handler, message_object) ⇒ Message
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.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/xnm/telegram/Message.rb', line 46 def initialize(handler, ) @handler = handler return if .nil? @valid = true; @message_id = [:message_id] @chat = handler[[:chat]] @user = handler[[:from] || [:sender_chat]] @reply_to_id = .dig(:reply_to_message, :message_id) @text = [:text] || ""; @timestamp = Time.at([:date] || 0) m = /\/([\S]*)/.match(@text) @command = m[1] if m @handled = false end |
Instance Attribute Details
#chat ⇒ Object (readonly)
Returns the Chat object this message was sent in.
12 13 14 |
# File 'lib/xnm/telegram/Message.rb', line 12 def chat @chat end |
#command ⇒ Object (readonly)
Optional, command included in this message. Can be nil.
29 30 31 |
# File 'lib/xnm/telegram/Message.rb', line 29 def command @command end |
#handled ⇒ Object
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.
35 36 37 |
# File 'lib/xnm/telegram/Message.rb', line 35 def handled @handled end |
#message_id ⇒ Object (readonly)
Returns the message ID of this message.
10 11 12 |
# File 'lib/xnm/telegram/Message.rb', line 10 def @message_id end |
#reply_to_id ⇒ Object (readonly)
Optional argument, ID of the message that was replied to.
17 18 19 |
# File 'lib/xnm/telegram/Message.rb', line 17 def reply_to_id @reply_to_id end |
#text ⇒ Object (readonly)
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.
22 23 24 |
# File 'lib/xnm/telegram/Message.rb', line 22 def text @text end |
#timestamp ⇒ Object (readonly)
Timestamp, from Telegram, that the message was sent on.
25 26 27 |
# File 'lib/xnm/telegram/Message.rb', line 25 def @timestamp end |
#user ⇒ Object (readonly)
Returns the User Object that sent this message.
14 15 16 |
# File 'lib/xnm/telegram/Message.rb', line 14 def user @user end |
#valid ⇒ Object (readonly)
Return whether or not parsing of the message was successful. TODO Actually perfom validity checks.
7 8 9 |
# File 'lib/xnm/telegram/Message.rb', line 7 def valid @valid end |
Instance Method Details
#delete! ⇒ Object
Try to delete this message. Wrapper for Telegram’s deleteMessage function
89 90 91 92 93 94 95 96 |
# File 'lib/xnm/telegram/Message.rb', line 89 def delete! @handler.core.perform_post('deleteMessage', { chat_id: @chat.chat_id, message_id: @message_id } ); end |
#edit_text(text) ⇒ Object Also known as: text=
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.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/xnm/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 |
#reply(text) ⇒ Object
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.
103 104 105 |
# File 'lib/xnm/telegram/Message.rb', line 103 def reply(text) @chat.(text, reply_to: self) end |
#send_message(text, **opts) ⇒ Object
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
113 114 115 |
# File 'lib/xnm/telegram/Message.rb', line 113 def (text, **opts) @chat.(text, **opts) end |
#to_i ⇒ Object
121 122 123 |
# File 'lib/xnm/telegram/Message.rb', line 121 def to_i @message_id end |
#to_s ⇒ Object
117 118 119 |
# File 'lib/xnm/telegram/Message.rb', line 117 def to_s @text end |