Skip to main content

python-whatsapp-bot

A whatsapp client library for python utilizing the WhatsApp Business Cloud API.

Made in Nigeria Downloads Downloads Downloads

Features supported

Getting started

To start, install with pip:

pip3 install --upgrade python-whatsapp-bot

Setting up

To get started using this library, you have to obtain a TOKEN and PHONE NUMBER ID from Facebook Developer Portal. You get these after setting up a developer account and setting up an app.

Here is a tutorial on the platform on how to go about the process

If you followed the tutorial, you should now have a TOKEN and TEST WHATSAPP NUMBER and its phone_number_id.activeYou might have even already sent your first message on the platform using the provided curl request.

Now you have all you need to start using this library. Note: The given token is temporary. This tutorial on the platform guides you to create a permanent token. This guide shows how to register an authentic phone number.

Initialization

To initialize the app instance, you need to specify the TOKEN and phone_number_id you obtained from the steps above. Note that phone number id is not phone number.

>>> from python_whatsapp_bot import Whatsapp
>>> wa_bot = Whatsapp(number_id='305xxxxxx', token=TOKEN)

Once initialized, you can start using some of the bot's features right away.

Sending Messages

To send a text message

>>> wa_bot.send_message('mobile eg: 2348145xxxxx3', 'Your message here')

Example

Here is an example

>>> wa_bot.send_message('2348945434343', 'Your message here')

Sending Interactive Messages

For buttons and lists, use the same send_message endpoint but with a reply_markup parameter. e.g

For buttons

>>> from from python_whatsapp_bot import Inline_keyboard # Import inline_keyboard for interactive buttons
>>> wa_bot.send_message('2348945434343', 'This is a message with two buttons',reply_markup=Inline_keyboard(['First button', 'Second button']))

For lists

>>> from python_whatsapp_bot import Inline_list, List_item # Import inline_list and List_item for interactive list
>>> wa_bot.send_message('2348945434343', 'This is a message with lists',reply_markup=Inline_list("Show list",list_items=[[List_item("one list item")]])

Sending Template Messages

To send a pre-approved template message:

>>> wa_bot.send_template_message("255757xxxxxx","hello_world")

Handling Incoming Messages

A short note about Webhooks

For every message sent to your bot business account, whatsapp sends an object containing the message as a post request to a url which you have to provide beforehand. The url you provide should be able to process simple get and post requests. This url is the webhook url, and the object whatsapp sends to your url is the webhook.

Now, you can write a small server with the Python Flask library to handle the webhook requests, but another problem arises if you're developing on a local server; whatsapp will not be able to send requests to your localhost url, so a quick fix would be to deploy your project to an online server each time you make a change to be able to test it. Once deployed, you can proceed to register the url of your deployed app using this tutorial from the platform.

If you're like me however, you wouldn't want to always deploy before you test, you want to run everything on local first. In this case, you might decide to use Ngrok to tunnel a live url to your local server, but another issue arises; Since Ngrok generates a new url each time it is restarted, you'd have to constantly log in to facebook servers to register the newly generated url. I presume you don't want that hassle either. In this situation, a webhook forwarder can be deployed to a virtual server like Heroku, and it doesn't get modified. You register the deployed forwarder's url on Whatsapp servers, it receives all the webhook requests and forwards them to your local machine using ngrok.

To continue with this fowarding process, open this repository https://github.com/Radi-dev/webhook-forwarder and follow the readme instructions to deploy it and setup a client for it on your device, then register the url following this guide.

Issues

Please open an issue to draw my attention to mistake or suggestion

Contributing

This is an opensource project under MIT License so anyone is welcome to contribute from typo, to source code to documentation, JUST FORK IT.

References

  1. WhatsApp Cloud API official documentation

All the credit

  1. All contributors

Release files for python-whatsapp-bot 2.0.3

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

Source distribution (sdist)

Source distribution for python-whatsapp-bot 2.0.3
File Size Uploaded
python_whatsapp_bot-2.0.3.tar.gz 17.2 kB Details

Built distribution (wheel)

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

Total release size: 35.2 kB

Release files / python_whatsapp_bot-2.0.3.tar.gz

Download URL python_whatsapp_bot-2.0.3.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
fb962bdbf2b74871b102706d1137d537e9a0d6073925b0f920a7233ce6829ebe
BLAKE2b-256 checksum
How to use checksums
61562d72f864158a39faac700b76cbe1c2d81fe396d29e28c83763b0b64eb3e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / python_whatsapp_bot-2.0.3-py3-none-any.whl

Download URL python_whatsapp_bot-2.0.3-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdeb17f2abdecb73dfa077b8fb3c36aa3d69d03117fe81a1faaf11787634ce5a
BLAKE2b-256 checksum
How to use checksums
f6e906b4f88534c53923579bd8ceafa63991fbbec844aa910cdf1b450838a09d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

2.0.3 This release

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1

2 release files

1.0.92

2 release files

1.0.91

2 release files

0.0.91

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.1

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