Перейти к содержанию

Пример бота#

Установка#

Установка:

python -m pip install whatsapp-chatbot-python

Импорт#

from whatsapp_chatbot_python import GreenAPIBot, Notification

Примеры#

Как инициализировать объект#

bot = GreenAPIBot(
    "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
)

Пример бота#

Ссылка на пример: 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()

Запуск приложения#

python full.py

Список примеров#

Описание Ссылка на пример
Как начать получать сообщения и отвечать на них base.py
Как получать другие уведомления и обрабатывать тело уведомления event.py
Как фильтровать входящие сообщения filters.py
Как обрабатывать кнопки buttons.py
Пример бота full.py
GREEN-API демо чатбот bot.py