Class: Diligence::Types::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, api_client: nil) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
# File 'lib/diligence/types/message.rb', line 10

def initialize(raw, api_client: nil)
  @raw = raw
  @api_client = api_client

  @id = raw.fetch('message_id')
  @created_at = Time.at(raw.fetch('date')).to_datetime
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/diligence/types/message.rb', line 8

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



8
9
10
# File 'lib/diligence/types/message.rb', line 8

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/diligence/types/message.rb', line 8

def id
  @id
end

Instance Method Details

#has_attachments?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/diligence/types/message.rb', line 22

def has_attachments?
  attachment_keys = %w[photo video document audio voice video_note sticker animation]
  attachment_keys.any? { |key| @raw.key?(key) }
end

#reply(text) ⇒ Object



27
28
29
30
31
32
# File 'lib/diligence/types/message.rb', line 27

def reply(text)
  raise 'API client not set' unless @api_client

  chat_id = @raw.dig('chat', 'id')
  @api_client.send_message(chat_id: chat_id, text: text)
end

#textObject



18
19
20
# File 'lib/diligence/types/message.rb', line 18

def text
  @raw['text']
end