Skip to content
Open

BSUID #174

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pywa/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

__all__ = [
"resolve_recipient",
"resolve_buttons_param",
"resolve_media_param",
"resolve_tracker_param",
Expand Down Expand Up @@ -56,6 +57,7 @@
VoiceCallButton,
)
from .types.media import Media
from .types.sent_update import RecipientType
from .types.templates import (
BaseParams,
Carousel,
Expand Down Expand Up @@ -814,6 +816,27 @@ def resolve_tracker_param(tracker: str | CallbackData | None) -> str | None:
return tracker.to_str() if isinstance(tracker, CallbackData) else tracker


_BSUID_RE = re.compile(r"^[A-Z]{2}\.\d+$")
_WA_ID_RE = re.compile(r"^\d+$")


def resolve_recipient(to: str | int) -> tuple[dict[str, str], RecipientType]:
recipient_type = RecipientType.from_recipient(to)
_logger.debug(f"Resolved recipient {to} to type {recipient_type}")
match recipient_type:
case RecipientType.WA_ID | RecipientType.PHONE_NUMBER:
return {"to": str(to), "recipient_type": "individual"}, recipient_type
case RecipientType.BSUID:
return {
"recipient": str(to),
"recipient_type": "individual",
}, recipient_type
case RecipientType.GROUP_ID:
return {"to": str(to), "recipient_type": "group"}, recipient_type
case _:
raise ValueError(f"Invalid recipient: {to}")


def resolve_arg(
*,
wa: WhatsApp,
Expand Down
26 changes: 18 additions & 8 deletions pywa/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,21 +530,25 @@ def send_raw_request(
def send_message(
self,
sender: str,
to: str,
to: str | None,
recipient: str | None,
recipient_type: str,
typ: str,
msg: dict[str, str | list[str]] | tuple[dict],
msg: dict,
reply_to_message_id: str | None = None,
biz_opaque_callback_data: str | None = None,
recipient_identity_key_hash: str | None = None,
) -> dict[str, dict | list]:
) -> dict:
"""
Send a message to a WhatsApp user.
Send a message to a WhatsApp user/group.

- Read more at `developers.facebook.com <https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages>`_.

Args:
sender: The phone id to send the message from.
to: The phone number to send the message to.
to: The WhatsApp ID to send the message to.
recipient: The recipient unique identifier (BSUID).
recipient_type: The type of the recipient (e.g. ``individual``, ``group``).
typ: The type of the message (e.g. ``text``, ``image``, etc.).
msg: The message object to send.
reply_to_message_id: The ID of the message to reply to.
Expand All @@ -554,13 +558,18 @@ def send_message(
Returns:
The response from the WhatsApp Cloud API.
"""
if not to and not recipient:
raise ValueError("Either 'to' or 'recipient' must be provided")
data = {
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": to,
"recipient_type": recipient_type,
"type": typ,
typ: msg,
}
if to:
data["to"] = to
if recipient:
data["recipient"] = recipient
if reply_to_message_id:
data["context"] = {"message_id": reply_to_message_id}
if biz_opaque_callback_data:
Expand All @@ -577,7 +586,8 @@ def send_marketing_message(
self,
sender: str,
to: str,
template: dict[str, str | list[str]],
recipient: str,
template: dict,
reply_to_message_id: str | None = None,
message_activity_sharing: bool | None = None,
biz_opaque_callback_data: str | None = None,
Expand Down
Loading
Loading