Amarula.Protocol.Binary.JID (amarula v0.1.0)

View Source

JID (Jabber ID) utilities for WhatsApp protocol. Ported from src/WABinary/jid-utils.ts

This module provides functions for encoding, decoding, and manipulating WhatsApp JIDs (Jabber IDs) with support for devices, agents, and different server types.

Summary

Functions

Compares two JIDs to see if they represent the same user.

Decodes a JID string into its components.

Encodes a JID from its components.

Checks if a JID represents a group. This is an alias for jid_group?/1 for compatibility with the messages module.

Checks if a JID represents a hosted LID user.

Checks if a JID represents a hosted PN user.

Checks if a JID represents a bot.

Checks if a JID represents a broadcast.

Checks if a JID represents a group.

Checks if a JID represents Meta AI.

Checks if a JID represents a newsletter.

Normalizes a JID to user format, converting c.us to s.whatsapp.net.

Checks if a JID is the status broadcast.

Checks if a JID represents a user (PN user).

Checks if a JID represents a LID user.

Returns the META_AI_JID constant

Returns the OFFICIAL_BIZ_JID constant

Returns the PSA_WID constant

Returns the S_WHATSAPP_NET constant

Returns the SERVER_JID constant

Returns the STORIES_JID constant

Transfers device from one JID to another.

Functions

are_jids_same_user?(jid1, jid2)

@spec are_jids_same_user?(binary() | nil, binary() | nil) :: boolean()

Compares two JIDs to see if they represent the same user.

decode(jid)

@spec decode(binary() | nil) :: map() | nil

Decodes a JID string into its components.

Parameters

  • jid: The JID string to decode

Returns

A map with keys: :user, :server, :device (optional), :domain_type

Examples

iex> JID.decode("1234@s.whatsapp.net")
%{user: "1234", server: "s.whatsapp.net", domain_type: 0}

iex> JID.decode("1234:0@s.whatsapp.net")
%{user: "1234", device: 0, server: "s.whatsapp.net", domain_type: 0}

encode(map)

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

Encodes a JID from its components.

Parameters

  • user: The user identifier (string, number, or nil)
  • server: The server part of the JID (optional, defaults to "s.whatsapp.net")
  • device: Optional device number
  • agent: Optional agent number

Examples

iex> JID.encode(%{user: "1234", server: "s.whatsapp.net"})
"1234@s.whatsapp.net"

iex> JID.encode(%{user: "1234", device: 0, server: "s.whatsapp.net"})
"1234:0@s.whatsapp.net"

iex> JID.encode(%{user: "1234", agent: 1, server: "s.whatsapp.net"})
"1234_1@s.whatsapp.net"

group?(jid)

@spec group?(binary() | nil) :: boolean()

Checks if a JID represents a group. This is an alias for jid_group?/1 for compatibility with the messages module.

hosted_lid_user?(jid)

@spec hosted_lid_user?(binary() | nil) :: boolean()

Checks if a JID represents a hosted LID user.

hosted_pn_user?(jid)

@spec hosted_pn_user?(binary() | nil) :: boolean()

Checks if a JID represents a hosted PN user.

jid_bot?(jid)

@spec jid_bot?(binary() | nil) :: boolean()

Checks if a JID represents a bot.

jid_broadcast?(jid)

@spec jid_broadcast?(binary() | nil) :: boolean()

Checks if a JID represents a broadcast.

jid_group?(jid)

@spec jid_group?(binary() | nil) :: boolean()

Checks if a JID represents a group.

jid_meta_ai?(jid)

@spec jid_meta_ai?(binary() | nil) :: boolean()

Checks if a JID represents Meta AI.

jid_newsletter?(jid)

@spec jid_newsletter?(binary() | nil) :: boolean()

Checks if a JID represents a newsletter.

jid_normalized_user(jid)

@spec jid_normalized_user(binary() | nil) :: binary()

Normalizes a JID to user format, converting c.us to s.whatsapp.net.

jid_status_broadcast?(jid)

@spec jid_status_broadcast?(binary()) :: boolean()

Checks if a JID is the status broadcast.

jid_user?(jid)

@spec jid_user?(binary() | nil) :: boolean()

Checks if a JID represents a user (PN user).

lid_user?(jid)

@spec lid_user?(binary() | nil) :: boolean()

Checks if a JID represents a LID user.

meta_ai_jid()

@spec meta_ai_jid() :: binary()

Returns the META_AI_JID constant

official_biz_jid()

@spec official_biz_jid() :: binary()

Returns the OFFICIAL_BIZ_JID constant

psa_wid()

@spec psa_wid() :: binary()

Returns the PSA_WID constant

s_whatsapp_net()

@spec s_whatsapp_net() :: binary()

Returns the S_WHATSAPP_NET constant

server_jid()

@spec server_jid() :: binary()

Returns the SERVER_JID constant

stories_jid()

@spec stories_jid() :: binary()

Returns the STORIES_JID constant

transfer_device(from_jid, to_jid)

@spec transfer_device(binary(), binary()) :: binary()

Transfers device from one JID to another.