Skip to main content

python-telegram-bot for WhatsApp

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bot_whatsapp-0.0.6.tar.gz (8.2 kB view hashes)

Uploaded Source

Built Distribution

bot_whatsapp-0.0.6-py3-none-any.whl (10.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page