Skip to content

How to start receiving and answering messages#

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()

Running the application#

python base.py

List of examples#

Description Link to example
How to start receiving and answering messages base.py
How to receive other notifications and handle the notification body event.py
How to filter incoming messages filters.py
How to handle buttons buttons.py
Example of a bot full.py
GREEN-API demo chatbot bot.py