Class: XNM::Telegram::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/xnm/telegram/Message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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] || message_object[:sender_chat]]

	@reply_to_id = message_object.dig(:reply_to_message, :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

Instance Attribute Details

#chatObject (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

#commandObject (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

#handledObject

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_idObject (readonly)

Returns the message ID of this message.



10
11
12
# File 'lib/xnm/telegram/Message.rb', line 10

def message_id
  @message_id
end

#reply_to_idObject (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

#textObject (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

#timestampObject (readonly)

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



25
26
27
# File 'lib/xnm/telegram/Message.rb', line 25

def timestamp
  @timestamp
end

#userObject (readonly)

Returns the User Object that sent this message.



14
15
16
# File 'lib/xnm/telegram/Message.rb', line 14

def user
  @user
end

#validObject (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.send_message(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 send_message(text, **opts)
	@chat.send_message(text, **opts)
end

#to_iObject



121
122
123
# File 'lib/xnm/telegram/Message.rb', line 121

def to_i
	@message_id
end

#to_sObject



117
118
119
# File 'lib/xnm/telegram/Message.rb', line 117

def to_s
	@text
end