Skip to content

A bot example 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"
)

A bot example#

Link to the example: full.py.

@bot.router.message(command="start")
def message_handler(notification: Notification) -> None:
    sender_data = notification.event["senderData"]
    sender_name = sender_data["senderName"]

    notification.answer(
        (
            f"Hello, {sender_name}. Here's what I can do:\n\n"
            "1. Report a problem\n"
            "2. Show office address\n"
            "3. Show available rates\n"
            "4. Call a support operator\n\n"
            "Choose a number and send to me."
        )
    )


@bot.router.message(text_message=["1", "Report a problem"])
def report_problem_handler(notification: Notification) -> None:
    notification.answer(
        "https://github.com/green-api/issues/issues/new", link_preview=False
    )


@bot.router.message(text_message=["2", "Show office address"])
def show_office_address_handler(notification: Notification) -> None:
    chat = notification.get_chat()

    notification.api.sending.sendLocation(
        chatId=chat, latitude=55.7522200, longitude=37.6155600
    )


@bot.router.message(text_message=["3", "Show available rates"])
def show_available_rates_handler(notification: Notification) -> None:
    notification.answer_with_file("data/rates.png")


@bot.router.message(text_message=["4", "Call a support operator"])
def call_support_operator_handler(notification: Notification) -> None:
    notification.answer("Good. A tech support operator will contact you soon.")


bot.run_forever()

Running the application#

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