Class: Message

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

Overview

Is a representation of Telegrams Message object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_id:, from: nil, chat:, text:) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
21
# File 'lib/telegram/message.rb', line 16

def initialize(message_id:, from:nil, chat:, text:)
  @message_id = message_id
  @from = from
  @chat = chat
  @text = text
end

Instance Attribute Details

#chatObject (readonly)

Returns the value of attribute chat.



5
6
7
# File 'lib/telegram/message.rb', line 5

def chat
  @chat
end

#textObject (readonly)

Returns the value of attribute text.



5
6
7
# File 'lib/telegram/message.rb', line 5

def text
  @text
end

Class Method Details

.from_hash(message) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/telegram/message.rb', line 7

def self.from_hash(message)
  return nil unless message.is_a? Hash

  chat = Chat.from_hash message['chat']
  from = User.from_hash message['from']

  Message.new(message_id: message['message_id'], from: from, chat: chat, text: message['text'])
end

Instance Method Details

#contains_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/telegram/message.rb', line 23

def contains_command?(command)
  if command.is_a?(Symbol) && text.split(' ').first.eql?("/#{command}")
    true
  elsif text.start_with?(command.to_s)
    true
  else
    false
  end
end

#reply(text, opts = {}) ⇒ Object



33
34
35
# File 'lib/telegram/message.rb', line 33

def reply(text, opts = {})
  chat.reply text, opts.merge(reply_to_message_id: @message_id)
end