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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bot_whatsapp-0.0.6.tar.gz
.
File metadata
- Download URL: bot_whatsapp-0.0.6.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86f43b81257e5b4f203250874e494375197e63493a81a7c37bc44dad7c710c42 |
|
MD5 | b84f5480bcc721746a438023ae36fb2c |
|
BLAKE2b-256 | cd0b16bba731ae825954b5285c081bd999e6100a8f5d6b2864bc612a48a7f1f6 |
File details
Details for the file bot_whatsapp-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: bot_whatsapp-0.0.6-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92033a3567a4c2c92e855cccc606b70c23975d5d504ede6dfc16b81c21493b04 |
|
MD5 | 853fd3a0b3e0856cd0127b4194da86f7 |
|
BLAKE2b-256 | b0edae76ec4d823c7f15a5f491887f5c1c75580a17349e325a4dd5064def8670 |