Receiving and processing messages in Python#
Installation#
python -m pip install telegram-chatbot-python
Import#
from telegram_chatbot_python import GreenAPIBot, Notification
Examples#
How to initialize an object#
bot = GreenAPIBot(
"4100000000", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)
How to start receiving messages and replying to them#
To start receiving messages, you need to create a handler function with one parameter (notification). The notification parameter is a class that stores the notification object (event) and the functions for replying to a message. To send a text message in reply to a notification, you need to call the notification.answer function and pass the message text to it. You do not need to specify the chatId parameter, as it is substituted from the notification automatically.
Next, you need to add the handler function to the list of handlers. You can do this 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 parentheses.
To start the bot, you need to call the bot.run_forever function. You can stop the bot with the Ctrl + C key combination.
Link to the 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
The list of examples#
| Description | Link to the example |
|---|---|
| How to start receiving messages and replying to them | base.py |
| How to receive other notifications and process the notification body | event.py |
| How to filter incoming messages | filters.py |
| A bot example | full.py |