class Xasin::Telegram::Message

Attributes

chat[R]

Returns the {Chat} object this message was sent in.

command[R]

Optional, command included in this message. Can be nil.

handled[RW]

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.

message_id[R]

Returns the message ID of this message.

reply_to_id[R]

Optional argument, ID of the message that was replied to.

text[R]

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[R]

Timestamp, from Telegram, that the message was sent on.

user[R]

Returns the {User} Object that sent this message.

valid[R]

Return whether or not parsing of the message was successful. TODO Actually perfom validity checks.

Public Class Methods

new(handler, message_object) click to toggle source

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

delete!() click to toggle source

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_text(text) click to toggle source

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
Also aliased as: text=
reply(text) click to toggle source

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_message(text, **opts) click to toggle source

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
text=(text)
Alias for: edit_text
to_i() click to toggle source
# File lib/xasin/telegram/Message.rb, line 121
def to_i
        @message_id
end
to_s() click to toggle source
# File lib/xasin/telegram/Message.rb, line 117
def to_s
        @text
end