This library helps you easily create a Python chatbot with WhatsApp API.
Project description
whatsapp-chatbot-python
whatsapp-chatbot-python is a library for integration with WhatsApp messenger using the API service green-api.com. You should get a registration token and an account ID in your personal cabinet to use the library. There is a free developer account tariff.
API
The documentation for the REST API can be found at the link. The library is a wrapper for the REST API, so the documentation at the link above also applies.
Authorization
To send a message or perform other Green API methods, the WhatsApp account in the phone app must be authorized. To authorize the account, go to your cabinet and scan the QR code using the WhatsApp app.
Installation
Installation:
python -m pip install whatsapp-chatbot-python
Import
from whatsapp_chatbot_python import GreenAPIBot, Notification
Examples
How to initialize an object
bot = GreenAPIBot(
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)
How to start receiving and answering messages
To start receiving messages, you must create a handler function with one parameter (notification
). The notification
parameter is the class where the notification object (event
) and the functions to answer the message are stored. To
send a text message in response to a notification, you need to call the notification.answer
function and pass there
the text of the message. You don't need to pass the chatId
parameter because it is automatically taken from the
notification.
Next, you need to add the handler function to the list of handlers. This can be done with the bot.router.message
decorator as in the example or with the bot.router.message.add_handler
function. The decorator must be called with
brackets.
To start the bot, call the bot.run_forever
function. You can stop the bot with the key combination Ctrl + C.
Link to example: base.py.
@bot.router.message(text_message="message")
def message_handler(notification: Notification) -> None:
notification.answer("Hello")
bot.run_forever()
How to receive other notifications and handle the notification body
You can receive not only incoming messages but also outgoing messages. You can also get the status of the sent message.
- To receive outgoing messages, you need to use the
bot.router.outgoing_message
object; - To receive outgoing API messages, you need to use the
bot.router.outgoing_api_message
object; - To receive the status of sent messages, you need to use the
bot.router.outgoing_message_status
object.
The body of the notification is in notification.event
. In this example, we get the message type from the notification
body.
Link to example: event.py.
@bot.router.message()
def message_handler(notification: Notification) -> None:
print(notification.event)
bot.run_forever()
How to filter incoming messages
Messages can be filtered by chat, sender, message type, and text. To filter chat, sender, and message type, you can use
a string (str
) or a list of strings (list[str]
). The message text can be filtered by text, command, and regular
expressions. Below is a table with filter names and possible values.
Filter name | Description | Possible values |
---|---|---|
from_chat |
Chats or chats from which you want to receive messages | "11001234567@c.us" or ["11001234567@c.us", "11002345678@c.us"] |
from_sender |
The sender or senders from whom you want to receive messages | "11001234567@c.us" or ["11001234567@c.us", "11002345678@c.us"] |
type_message |
The type or types of message to be handled | "textMessage" or ["textMessage", "extendedTextMessage"] |
text_message |
Your function will be executed if the text fully matches the text | "Hello. I need help." |
regexp |
Your function will be executed if the text matches the regular expression pattern | r"Hello. I need help." |
command |
Your function will be executed if the prefix and the command match your values completely | "help" or ("help", "!/") |
How to add filters through the decorator
@bot.router.message(command="command")
How to add filters with the function
bot.router.message.add_handler(handler, command="command")
How to filter messages by chat, sender, or message type
To filter messages by chat, sender, or message type, you must add a string (str
) or a list of strings (list[str]
).
from_chat = "11001234567@c.us"
from_sender = "11001234567@c.us"
type_message = ["textMessage", "extendedTextMessage"]
How to filter messages by message text or regular expressions
You must add a string (str
) to filter messages by text or regular expressions.
text_message = "Hello. I need help."
regexp = r"Hello. I need help."
How to filter messages by command
Add a string (str
) or a tuple (tuple
) to filter messages by command. You need to specify either a command name or a
command name and a prefix string. The default prefix is /
.
command = "help"
command = ("help", "!/")
Example
Link to example: filters.py.
@bot.router.message(command="help")
def message_handler(notification: Notification) -> None:
notification.answer_with_file(file="help.png")
bot.run_forever()
How to handle buttons
To be notified when a button is pressed, you must use the bot.router.buttons
object.
Link to example: buttons.py.
@bot.router.buttons()
def buttons_handler(notification: Notification) -> None:
notification.answer_buttons("Choose a color", [
{
"buttonId": 1,
"buttonText": "Red"
},
{
"buttonId": 2,
"buttonText": "Green"
},
{
"buttonId": 3,
"buttonText": "Blue"
}
])
bot.run_forever()
Service methods documentation
License
Licensed under Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0) terms. Please see file LICENSE.
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 whatsapp-chatbot-python-0.2.3.tar.gz
.
File metadata
- Download URL: whatsapp-chatbot-python-0.2.3.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77da47e7180198286e6a6073a5ffc005f5e16264c49e8c2034eaa60604ed311b |
|
MD5 | 36d62ae4874b9bbcc4a083fadad4acb8 |
|
BLAKE2b-256 | f9df8e07f11e88b15e50f3f28a5dc2e276795ab5bc4b7a7935241f0ff89654cb |
File details
Details for the file whatsapp_chatbot_python-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: whatsapp_chatbot_python-0.2.3-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85c7e381e628fad1564ed1149195faa69dbd9b1c4399e81f4a30615b6abff231 |
|
MD5 | f074dd819ee214c4069aefa875a44cae |
|
BLAKE2b-256 | ac3fb6dc6321d9cbaa8cc28e40fa36892535665464a23642d06692f9164f35ac |