π Client#
Note
WORK IN PROGRESS
The WhatsApp
client has two responsibilities. Sending messages & handling updates.
- class pywa.client.WhatsApp#
- __init__(phone_id: str | int, token: str, base_url: str = 'https://graph.facebook.com', api_version: str | int | float | Literal[utils.Version.GRAPH_API] = Version.GRAPH_API, session: requests.Session | None = None, server: Flask | FastAPI | None = None, webhook_endpoint: str = '/', verify_token: str | None = None, filter_updates: bool = True, business_account_id: str | int | None = None, callback_url: str | None = None, fields: Iterable[str] | None = None, app_id: int | None = None, app_secret: str | None = None, verify_timeout: int | None = None, business_private_key: str | None = None, business_private_key_password: str | None = None, flows_request_decryptor: utils.FlowRequestDecryptor | None = <function default_flow_request_decryptor>, flows_response_encryptor: utils.FlowResponseEncryptor | None = <function default_flow_response_encryptor>) None #
- The WhatsApp client.
Full documentation on pywa.readthedocs.io.
Example without webhook:
>>> from pywa import WhatsApp >>> wa = WhatsApp(phone_id="100944",token="EAADKQl9oJxx")
Example with webhook (using Flask):
>>> from pywa import WhatsApp >>> from flask import Flask >>> flask_app = Flask(__name__) >>> wa = WhatsApp( ... phone_id="100944", ... token="EAADKQl9oJxx", ... server=flask_app, ... callback_url='https://6b3e.ngrok.io', ... verify_token="XYZ123", ... app_id=1234567890, ... app_secret="my_app_secret", ... ) >>> @wa.on_message() ... def message_handler(_: WhatsApp, msg: Message): print(msg) >>> flask_app.run(port=8000)
- Parameters:
phone_id β The Phone number ID (Not the phone number itself, the ID can be found in the App dashboard).
token β The token of the WhatsApp business account (In production, you should use permanent token).
base_url β The base URL of the WhatsApp API (Do not change unless you know what youβre doing).
api_version β The API version of the WhatsApp Cloud API (default to the latest version).
session β The session to use for requests (default: new
requests.Session()
, For cases where you want to use a custom session, e.g. for proxy support. Do not use the same session across multiple WhatsApp clients!).server β The Flask or FastAPI app instance to use for the webhook. required when you want to handle incoming updates.
callback_url β The callback URL to register (optional, only if you want pywa to register the callback URL for you).
verify_token β The verify token of the registered
callback_url
(Required whenserver
is provided. The verify token can be any string. It is used to challenge the webhook endpoint to verify that the endpoint is valid).verify_timeout β The timeout (in seconds) to wait for the verify token to be sent to the server (optional, for cases where the server/network is slow or the server is taking a long time to start).
fields β The fields to register for the callback URL (optional, if not provided, all supported fields will be registered. modify this if you want to reduce the number of unused requests to your server).
app_id β The ID of the app in the App Basic Settings (optional, required when registering a
callback_url
).app_secret β
The secret of the app in the App Basic Settings (optional, required when registering a
callback_url
).webhook_endpoint β The endpoint to listen for incoming messages (if youβre using the server for another purpose, or for multiple WhatsApp clients, you can change this to avoid conflicts).
filter_updates β Whether to filter out user updates that are not sent to this phone_id (default:
True
, does not apply to raw updates or updates that are not user-related).business_account_id β The WhatsApp business account ID that owns the phone ID (optional, required for some API methods).
business_private_key β The global private key to use in the
flows_request_decryptor
business_private_key_password β The global private key password (if needed) to use in the
flows_request_decryptor
flows_request_decryptor β The global flows requests decryptor implementation to use to decrypt Flows requests.
flows_response_encryptor β The global flows response encryptor implementation to use to encrypt Flows responses.
The available methods are:
Category |
Methods |
---|---|
Sending messages |
|
Media |
|
Templates |
|
Flows |
|
Business profile |
|
Commerce |
- Client Reference
WhatsApp.send_message()
WhatsApp.send_image()
WhatsApp.send_video()
WhatsApp.send_audio()
WhatsApp.send_document()
WhatsApp.send_location()
WhatsApp.send_contact()
WhatsApp.send_sticker()
WhatsApp.send_catalog()
WhatsApp.send_template()
WhatsApp.send_product()
WhatsApp.send_products()
WhatsApp.send_reaction()
WhatsApp.remove_reaction()
WhatsApp.mark_message_as_read()
WhatsApp.upload_media()
WhatsApp.download_media()
WhatsApp.get_media_url()
WhatsApp.get_business_profile()
WhatsApp.get_business_phone_number()
WhatsApp.update_business_profile()
WhatsApp.update_conversational_automation()
WhatsApp.set_business_public_key()
WhatsApp.get_commerce_settings()
WhatsApp.update_commerce_settings()
WhatsApp.create_template()
WhatsApp.create_flow()
WhatsApp.update_flow_metadata()
WhatsApp.update_flow_json()
WhatsApp.publish_flow()
WhatsApp.delete_flow()
WhatsApp.deprecate_flow()
WhatsApp.get_flow()
WhatsApp.get_flows()
WhatsApp.get_flow_assets()
WhatsApp.register_phone_number()
- API Reference