Class: Whatsapp::Message
- Inherits:
-
Object
- Object
- Whatsapp::Message
- 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.
Instance Attribute Summary collapse
-
#from ⇒ String?
readonly
Sender JID (e.g. "33612345678@s.whatsapp.net").
-
#id ⇒ String?
readonly
Message ID.
-
#raw ⇒ Hash
readonly
Raw Baileys message data.
-
#text ⇒ String?
readonly
Message text content.
-
#timestamp ⇒ Time?
readonly
Message timestamp.
-
#to ⇒ String?
readonly
Recipient phone number (outgoing only).
-
#type ⇒ Symbol
readonly
Message type (:text, :image, :video, :audio, :document, :sticker, :location, :contact).
Instance Method Summary collapse
-
#caption ⇒ String?
Caption for media messages (image, video, document).
-
#contact? ⇒ Boolean
Whether this message is a contact card.
-
#contact_info ⇒ Hash{Symbol => String}?
Parsed contact card info.
-
#from_me? ⇒ Boolean
Whether this message was sent by the authenticated account.
-
#group? ⇒ Boolean
Whether this message is from a group chat.
-
#initialize(data) ⇒ Message
constructor
A new instance of Message.
-
#location ⇒ Hash{Symbol => Float, String, nil}?
Parsed location data.
-
#location? ⇒ Boolean
Whether this message is a location share.
-
#media? ⇒ Boolean
Whether this message contains media (image, video, audio, document, sticker).
-
#quoted_message_id ⇒ String?
ID of the quoted message (when this is a reply).
-
#reply? ⇒ Boolean
Whether this message is a reply to another message.
-
#reply_to(client, text:) ⇒ Message
Send a reply to the sender of this message.
Constructor Details
#initialize(data) ⇒ Message
Returns a new instance of Message.
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
#from ⇒ String? (readonly)
Returns sender JID (e.g. "33612345678@s.whatsapp.net").
20 21 22 |
# File 'lib/whatsapp/message.rb', line 20 def from @from end |
#id ⇒ String? (readonly)
Returns message ID.
17 18 19 |
# File 'lib/whatsapp/message.rb', line 17 def id @id end |
#raw ⇒ Hash (readonly)
Returns raw Baileys message data.
35 36 37 |
# File 'lib/whatsapp/message.rb', line 35 def raw @raw end |
#text ⇒ String? (readonly)
Returns message text content.
26 27 28 |
# File 'lib/whatsapp/message.rb', line 26 def text @text end |
#timestamp ⇒ Time? (readonly)
Returns message timestamp.
29 30 31 |
# File 'lib/whatsapp/message.rb', line 29 def @timestamp end |
#to ⇒ String? (readonly)
Returns recipient phone number (outgoing only).
23 24 25 |
# File 'lib/whatsapp/message.rb', line 23 def to @to end |
#type ⇒ Symbol (readonly)
Returns 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
#caption ⇒ String?
Caption for media messages (image, video, document).
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.
80 81 82 |
# File 'lib/whatsapp/message.rb', line 80 def contact? type == :contact end |
#contact_info ⇒ Hash{Symbol => String}?
Parsed contact card info.
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.
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.
59 60 61 |
# File 'lib/whatsapp/message.rb', line 59 def group? from&.end_with?("@g.us") end |
#location ⇒ Hash{Symbol => Float, String, nil}?
Parsed location data.
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.
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).
66 67 68 |
# File 'lib/whatsapp/message.rb', line 66 def media? %i[image video audio document sticker].include?(type) end |
#quoted_message_id ⇒ String?
ID of the quoted message (when this is a reply).
122 123 124 125 126 127 128 |
# File 'lib/whatsapp/message.rb', line 122 def 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.
133 134 135 |
# File 'lib/whatsapp/message.rb', line 133 def reply? !.nil? end |
#reply_to(client, text:) ⇒ Message
Send a reply to the sender of this message.
142 143 144 |
# File 'lib/whatsapp/message.rb', line 142 def reply_to(client, text:) client.(to: from, text: text) end |