Amarula.Protocol.Messages.MessageEncoder (amarula v0.1.0)

View Source

Encode an outgoing Proto.Message to the padded plaintext bytes that the Signal cipher encrypts, ported from Baileys encodeWAMessage (src/Utils/generics.ts):

encodeWAMessage(m) = writeRandomPadMax16(proto.Message.encode(m))

writeRandomPadMax16 appends padLength bytes each equal to padLength, where padLength = (rand_byte & 0x0f) + 1 (1..16). unpadRandomMax16 (the receive side) reads the last byte as the pad length and strips it.

Summary

Functions

Build a contact message from a display_name + vcard string. Pass a list of {display_name, vcard} to build a contactsArrayMessage (multiple contacts).

Edit the message identified by target_key, replacing its body with new_text. Sent to target_key.remoteJid with the edit="1" stanza attr (added by the send path); the recipient replaces the original message's content.

Encode a %Proto.Message{} (or message map) and append random PKCS-style pad.

A PEER_DATA_OPERATION on-demand history request — sent to our own devices to ask the phone for older messages of a chat (Baileys fetchMessageHistory). oldest_key is the oldest message we already have; the phone replies with an ON_DEMAND HistorySync notification carrying up to count older messages.

Convenience: media(:image, info, opts).

Build a location message. lat/lng are floats (proto double). opts: :name, :address, :url, :is_live.

Build a media message of type (:image/:video/:audio/:document/ :sticker) from an encrypt+upload result. info carries the uploaded :url/:direct_path, the encrypt result (:media_key, :file_sha256, :file_enc_sha256, :file_length) and :mimetype. opts adds per-type extras (:caption, :width, :height, :seconds, :ptt, :file_name, :title).

A PEER_DATA_OPERATION placeholder-resend request for message_key — sent to our own devices to ask the phone to re-deliver that message (Baileys requestPlaceholderResend).

Build a poll-creation message. name is the question, options the choice strings. opts

Build a reaction to the message identified by target_key. emoji is the reaction (e.g. "👍"); pass "" to REMOVE a previous reaction (Baileys semantics).

Build a delete-for-everyone (revoke) of the message identified by target_key. Sent to target_key.remoteJid; the recipient replaces the message with a "this message was deleted" tombstone.

Build a plain-text conversation message.

Functions

contact(display_name, vcard)

@spec contact(String.t(), String.t()) :: Amarula.Protocol.Proto.Message.t()

Build a contact message from a display_name + vcard string. Pass a list of {display_name, vcard} to build a contactsArrayMessage (multiple contacts).

contacts(display_name, pairs)

@spec contacts(String.t(), [{String.t(), String.t()}, ...]) ::
  Amarula.Protocol.Proto.Message.t()

edit(target_key, new_text)

@spec edit(Amarula.Protocol.Proto.MessageKey.t(), String.t()) ::
  Amarula.Protocol.Proto.Message.t()

Edit the message identified by target_key, replacing its body with new_text. Sent to target_key.remoteJid with the edit="1" stanza attr (added by the send path); the recipient replaces the original message's content.

encode(message)

@spec encode(struct() | map()) :: binary()

Encode a %Proto.Message{} (or message map) and append random PKCS-style pad.

history_sync_on_demand_request(oldest_key, oldest_ts, count)

@spec history_sync_on_demand_request(
  Amarula.Protocol.Proto.MessageKey.t(),
  integer(),
  non_neg_integer()
) :: Amarula.Protocol.Proto.Message.t()

A PEER_DATA_OPERATION on-demand history request — sent to our own devices to ask the phone for older messages of a chat (Baileys fetchMessageHistory). oldest_key is the oldest message we already have; the phone replies with an ON_DEMAND HistorySync notification carrying up to count older messages.

image(info, opts \\ [])

@spec image(
  map(),
  keyword()
) :: Amarula.Protocol.Proto.Message.t()

Convenience: media(:image, info, opts).

location(lat, lng, opts \\ [])

@spec location(float(), float(), keyword()) :: Amarula.Protocol.Proto.Message.t()

Build a location message. lat/lng are floats (proto double). opts: :name, :address, :url, :is_live.

media(type, info, opts \\ [])

@spec media(:image | :video | :audio | :document | :sticker, map(), keyword()) ::
  Amarula.Protocol.Proto.Message.t()

Build a media message of type (:image/:video/:audio/:document/ :sticker) from an encrypt+upload result. info carries the uploaded :url/:direct_path, the encrypt result (:media_key, :file_sha256, :file_enc_sha256, :file_length) and :mimetype. opts adds per-type extras (:caption, :width, :height, :seconds, :ptt, :file_name, :title).

placeholder_resend_request(message_key)

@spec placeholder_resend_request(Amarula.Protocol.Proto.MessageKey.t()) ::
  Amarula.Protocol.Proto.Message.t()

A PEER_DATA_OPERATION placeholder-resend request for message_key — sent to our own devices to ask the phone to re-deliver that message (Baileys requestPlaceholderResend).

poll(name, options, opts \\ [])

@spec poll(String.t(), [String.t(), ...], keyword()) ::
  {Amarula.Protocol.Proto.Message.t(), binary()}

Build a poll-creation message. name is the question, options the choice strings. opts:

  • :selectable — max selectable options (0 = unlimited; default 1 = single).
  • :announcement — community-announcement-group poll (v2).
  • :message_secret — 32-byte vote-encryption key (default: random). Keep it; tallying incoming votes needs it (Poll.tally/3).

Picks the proto variant like Baileys: v2 for announcement, v3 for single-select, v1 for multi. Returns {message, message_secret}.

reaction(target_key, emoji)

@spec reaction(Amarula.Protocol.Proto.MessageKey.t(), String.t()) ::
  Amarula.Protocol.Proto.Message.t()

Build a reaction to the message identified by target_key. emoji is the reaction (e.g. "👍"); pass "" to REMOVE a previous reaction (Baileys semantics).

revoke(target_key)

@spec revoke(Amarula.Protocol.Proto.MessageKey.t()) ::
  Amarula.Protocol.Proto.Message.t()

Build a delete-for-everyone (revoke) of the message identified by target_key. Sent to target_key.remoteJid; the recipient replaces the message with a "this message was deleted" tombstone.

text(body)

@spec text(String.t()) :: Amarula.Protocol.Proto.Message.t()

Build a plain-text conversation message.