Class: Whatsapp::Message

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

Overview

Represents a WhatsApp message (incoming or outgoing).

Parses the raw Baileys message format into a friendly Ruby object. Supports text, image, video, audio, document, sticker, location, contact, and reply messages.

Examples:

message = Whatsapp::Message.new(raw_data)
message.text       # => "Hello!"
message.type       # => :text
message.from_me?   # => false
message.group?     # => false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Message

Returns a new instance of Message.

Parameters:

  • data (Hash)

    raw message data from Baileys



38
39
40
41
42
43
44
45
46
47
# File 'lib/whatsapp/message.rb', line 38

def initialize(data)
  @raw = data
  @id = data[:id] || data.dig(:key, :id)
  @from = data[:from] || data.dig(:key, :remoteJid)
  @to = data[:to]
  @text = data[:text] || data.dig(:message, :conversation) ||
    data.dig(:message, :extendedTextMessage, :text)
  @timestamp = data[:timestamp] ? Time.at(data[:timestamp].to_i) : nil
  @type = detect_type(data)
end

Instance Attribute Details

#fromString? (readonly)

Returns sender JID (e.g. "33612345678@s.whatsapp.net").

Returns:



20
21
22
# File 'lib/whatsapp/message.rb', line 20

def from
  @from
end

#idString? (readonly)

Returns message ID.

Returns:

  • (String, nil)

    message ID



17
18
19
# File 'lib/whatsapp/message.rb', line 17

def id
  @id
end

#rawHash (readonly)

Returns raw Baileys message data.

Returns:

  • (Hash)

    raw Baileys message data



35
36
37
# File 'lib/whatsapp/message.rb', line 35

def raw
  @raw
end

#textString? (readonly)

Returns message text content.

Returns:

  • (String, nil)

    message text content



26
27
28
# File 'lib/whatsapp/message.rb', line 26

def text
  @text
end

#timestampTime? (readonly)

Returns message timestamp.

Returns:

  • (Time, nil)

    message timestamp



29
30
31
# File 'lib/whatsapp/message.rb', line 29

def timestamp
  @timestamp
end

#toString? (readonly)

Returns recipient phone number (outgoing only).

Returns:

  • (String, nil)

    recipient phone number (outgoing only)



23
24
25
# File 'lib/whatsapp/message.rb', line 23

def to
  @to
end

#typeSymbol (readonly)

Returns message type (:text, :image, :video, :audio, :document, :sticker, :location, :contact).

Returns:

  • (Symbol)

    message type (:text, :image, :video, :audio, :document, :sticker, :location, :contact)



32
33
34
# File 'lib/whatsapp/message.rb', line 32

def type
  @type
end

Instance Method Details

#captionString?

Caption for media messages (image, video, document).

Returns:

  • (String, nil)


112
113
114
115
116
117
# File 'lib/whatsapp/message.rb', line 112

def caption
  msg = raw[:message] || {}
  msg.dig(:imageMessage, :caption) ||
    msg.dig(:videoMessage, :caption) ||
    msg.dig(:documentMessage, :caption)
end

#contact?Boolean

Whether this message is a contact card.

Returns:

  • (Boolean)


80
81
82
# File 'lib/whatsapp/message.rb', line 80

def contact?
  type == :contact
end

#contact_infoHash{Symbol => String}?

Parsed contact card info.

Returns:

  • (Hash{Symbol => String}, nil)

    +{ display_name:, vcard: }+ or nil



102
103
104
105
106
107
# File 'lib/whatsapp/message.rb', line 102

def contact_info
  ct = raw.dig(:message, :contactMessage)
  return nil unless ct

  {display_name: ct[:displayName], vcard: ct[:vcard]}
end

#from_me?Boolean

Whether this message was sent by the authenticated account.

Returns:

  • (Boolean)


52
53
54
# File 'lib/whatsapp/message.rb', line 52

def from_me?
  raw.dig(:key, :fromMe) == true
end

#group?Boolean

Whether this message is from a group chat.

Returns:

  • (Boolean)


59
60
61
# File 'lib/whatsapp/message.rb', line 59

def group?
  from&.end_with?("@g.us")
end

#locationHash{Symbol => Float, String, nil}?

Parsed location data.

Returns:

  • (Hash{Symbol => Float, String, nil}, nil)

    +{ latitude:, longitude:, name:, address: }+ or nil



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/whatsapp/message.rb', line 87

def location
  loc = raw.dig(:message, :locationMessage)
  return nil unless loc

  {
    latitude: loc[:degreesLatitude],
    longitude: loc[:degreesLongitude],
    name: loc[:name],
    address: loc[:address]
  }
end

#location?Boolean

Whether this message is a location share.

Returns:

  • (Boolean)


73
74
75
# File 'lib/whatsapp/message.rb', line 73

def location?
  type == :location
end

#media?Boolean

Whether this message contains media (image, video, audio, document, sticker).

Returns:

  • (Boolean)


66
67
68
# File 'lib/whatsapp/message.rb', line 66

def media?
  %i[image video audio document sticker].include?(type)
end

#quoted_message_idString?

ID of the quoted message (when this is a reply).

Returns:

  • (String, nil)


122
123
124
125
126
127
128
# File 'lib/whatsapp/message.rb', line 122

def quoted_message_id
  ctx = raw.dig(:message, :extendedTextMessage, :contextInfo) ||
    raw.dig(:message, :imageMessage, :contextInfo) ||
    raw.dig(:message, :videoMessage, :contextInfo) ||
    raw.dig(:message, :documentMessage, :contextInfo)
  ctx&.dig(:stanzaId)
end

#reply?Boolean

Whether this message is a reply to another message.

Returns:

  • (Boolean)


133
134
135
# File 'lib/whatsapp/message.rb', line 133

def reply?
  !quoted_message_id.nil?
end

#reply_to(client, text:) ⇒ Message

Send a reply to the sender of this message.

Parameters:

  • client (Client)

    the client to send through

  • text (String)

    reply text

Returns:



142
143
144
# File 'lib/whatsapp/message.rb', line 142

def reply_to(client, text:)
  client.send_message(to: from, text: text)
end