Skip to main content

WhatsApp Python SDK

A Python SDK for building bots and integrating with the WhatsApp Business API. Minimally working, but working.

To use it, you must obtain WhatsApp Business API credentials. The process is not as kafkian as it may look like.

Features

  • Send and receive messages
  • Handle different message types (text, image, audio, document)
  • Webhook verification
  • Easy-to-use handler system

Installation

Install the package using poetry:

poetry add bot-whatsapp

Usage

  • The code is 100% type hinted and is pretty simple, you can understand what you need just by reading it.
  • examples/echo_bot.py has an example of usage.

Setting Up the Bot

Create a bot instance and add handlers:

import os
from dotenv import load_dotenv
from bot_whatsapp import Bot
from bot_whatsapp.filters import MessageContentFilter
from bot_whatsapp.handler import MessageHandler
from bot_whatsapp.models import OutputMessage, Text, WebhookEvent

async def handle_echo(event: WebhookEvent, bot: Bot):
    message = event.entry[0].changes[0].value.messages[0]
    text = message.text.body
    wa_id = event.entry[0].changes[0].value.contacts[0].wa_id

    output_text = f"Echo: {text}"
    output_message = OutputMessage(
        to=wa_id,
        text=Text(body=output_text),
        type="text",
        messaging_product="whatsapp",
    )
    await bot.send_message(output_message)

if __name__ == "__main__":
    load_dotenv()
    whatsapp_token = os.getenv("WHATSAPP_TOKEN")
    phone_number_id = os.getenv("PHONE_NUMBER_ID")
    webhook_verify_token = os.getenv("WEBHOOK_VERIFY_TOKEN")
    bot = Bot(
        whatsapp_token=whatsapp_token,
        phone_number_id=phone_number_id,
        webhook_verify_token=webhook_verify_token,
    )
    bot.add_handler(MessageHandler(MessageContentFilter.TEXT, handle_echo))
    bot.start()
poetry run python examples/echo_bot.py

Alternatively, you can serve with the fastapi API:

# do not use if __name__ == "__main__" because this runs via fastapi cli
load_dotenv()
whatsapp_token = os.getenv("WHATSAPP_TOKEN")
phone_number_id = os.getenv("PHONE_NUMBER_ID")
webhook_verify_token = os.getenv("WEBHOOK_VERIFY_TOKEN")
bot = Bot(
    whatsapp_token=whatsapp_token,
    phone_number_id=phone_number_id,
    webhook_verify_token=webhook_verify_token,
)
bot.add_handler(MessageHandler(MessageContentFilter.TEXT, handle_echo))
bot.start()

Then run

poetry run fastapi dev examples/echo_bot.py

Environment Variables

Create a .env file with the following variables:

WHATSAPP_TOKEN=your_whatsapp_token
PHONE_NUMBER_ID=your_phone_number_id
WEBHOOK_VERIFY_TOKEN=your_webhook_verify_token

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Contact

For any inquiries, please contact piEsposito.

Metadata

Release files for bot-whatsapp 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bot-whatsapp 0.0.6
File Size Uploaded
bot_whatsapp-0.0.6.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bot-whatsapp 0.0.6
File Interpreter ABI Platform
bot_whatsapp-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / bot_whatsapp-0.0.6.tar.gz

Download URL bot_whatsapp-0.0.6.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
86f43b81257e5b4f203250874e494375197e63493a81a7c37bc44dad7c710c42
BLAKE2b-256 checksum
How to use checksums
cd0b16bba731ae825954b5285c081bd999e6100a8f5d6b2864bc612a48a7f1f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.4

Release files / bot_whatsapp-0.0.6-py3-none-any.whl

Download URL bot_whatsapp-0.0.6-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92033a3567a4c2c92e855cccc606b70c23975d5d504ede6dfc16b81c21493b04
BLAKE2b-256 checksum
How to use checksums
b0edae76ec4d823c7f15a5f491887f5c1c75580a17349e325a4dd5064def8670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page