GitHub

WhatsAppChatService

API reference for WhatsAppChatService.

The WhatsApp side of the adapter, for what the core has no op for.

  • send_template sends an approved template, which is how to write outside the 24 hour window. An authentication template with one-tap, zero-tap or copy-code buttons can't be sent to a BSUID: Meta refuses it with error 131062, which is InvalidRequest. Send those to a phone number address.
  • mark_read marks a received message read, which shows blue ticks, and with typing shows the typing indicator until the next message or for up to 25 seconds.
  • receive runs one webhook delivery, already verified and parsed, as layer_webhook does, for a consumer's own transport. It needs a ChatHandler, and a chat_interaction.Handler when there are clicks: without one a click is logged and dropped. deadline is a click handler's share, 3 seconds by default.

Attributes

AttributeTypeDescription
phone_number_idstr

The business phone number this adapter writes from.

Functions

__init__

__init__(*, phone_number_id, adapter) -> None
ParameterTypeDescription
phone_number_idstr
adapterWhatsAppAdapter
def __init__(self, *, phone_number_id: str, adapter: WhatsAppAdapter) -> None:
    self._phone_number_id = phone_number_id
    self._adapter = adapter

send_template

send_template(thread, template) -> Effect[Posted, ChatError]

Sends an approved template to thread.

ParameterTypeDescription
threadChatThread
templateTemplate

Returns Effect[Posted, ChatError]

def send_template(self, thread: ChatThread, template: Template) -> Effect[Posted, ChatError]:
    """Sends an approved template to ``thread``."""
    return self._adapter.send_template(thread, template)

mark_read

mark_read(thread, message_id, *, typing=False) -> Effect[None, ChatError]

Marks message_id read, with the typing indicator when typing.

ParameterTypeDescription
threadChatThread
message_idstr
typingbool = False

Returns Effect[None, ChatError]

def mark_read(
    self, thread: ChatThread, message_id: str, *, typing: bool = False
) -> Effect[None, ChatError]:
    """Marks ``message_id`` read, with the typing indicator when ``typing``."""
    return self._adapter.mark_read(thread, message_id, typing=typing)

receive

receive(envelope, *, on_update=None, deadline=_webhook.CLICK_DEADLINE) -> Effect[None, NotAccepted, ChatHandler]

Runs one parsed webhook delivery; see the class.

ParameterTypeDescription
envelopeobject
on_updateOnUpdate | None = None
deadlineDurationInput = _webhook.CLICK_DEADLINE

Returns Effect[None, NotAccepted, ChatHandler]

def receive(
    self,
    envelope: object,
    *,
    on_update: OnUpdate | None = None,
    deadline: DurationInput = _webhook.CLICK_DEADLINE,
) -> Effect[None, NotAccepted, ChatHandler]:
    """Runs one parsed webhook delivery; see the class."""
    click_deadline = fx.succeed(from_input_unsafe(deadline))
    items = _normalize.normalize(envelope, phone_number_id=self._phone_number_id)
    return ChatHandler().flat_map(
        lambda handler: _webhook.receive(
            items,
            _webhook.ReceiveOptions(
                handle=handler.handle, on_update=on_update, click_deadline=click_deadline
            ),
        )
    )