[go: up one dir, main page]

Skip to content

How to filter incoming 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 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()

Running the application#

python filters.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