Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ Select a number from the list to check how the sending method works!
*11*. Create a group with the bot 👥
*12*. Quote message ©️
*13*. About PYTHON GREEN API chatbot 🦎
*14*. 🔥 Conversation with ChatGPT 🤖
*15*. Interactive buttons 📌

To return to the beginning, write *stop* or *0*
```
Expand All @@ -243,7 +245,7 @@ This message was sent via the sendMessage method
To find out how the method works, follow the link
https://green-api.com/docs/api/sending/SendMessage/
```
If you send something other than numbers 1-13, the chatbot will briefly respond:
If you send something other than numbers 1-15, the chatbot will briefly respond:
```
Sorry, I didn't quite understand you, write a menu to see the possible options
```
Expand Down
4 changes: 3 additions & 1 deletion README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ GREEN API предоставляет отправку данных следую
*11*. Создать группу с ботом 👥
*12*. Цитировать сообщение ©️
*13*. О PYTHON GREEN API чат-боте 🦎
*14.* 🔥 Разговор с ChatGPT 🤖
*15.* Интерактивные кнопки 📌

Чтобы вернуться в начало напишите *стоп* или *0*
```
Expand All @@ -261,7 +263,7 @@ GREEN API предоставляет отправку данных следую
Чтобы узнать как работает метод, пройдите по ссылке
https://green-api.com/docs/api/sending/SendMessage/
```
Если отправить что-то помимо чисел 1-13, то чатбот лаконично ответит:
Если отправить что-то помимо чисел 1-15, то чатбот лаконично ответит:
```
Извините, я не совсем вас понял, напишите меню, чтобы посмотреть возможные опции
```
Expand Down
112 changes: 112 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,77 @@ def main_menu_option_13_handler(notification: Notification) -> None:
caption=about_text,
)

@bot.router.message(
type_message=TEXT_TYPES,
state=States.MENU.value,
regexp=r"^\s*15\s*$",
)
@debug_profiler(logger=logger)
def main_menu_option_15_handler(notification: Notification) -> None:
"""
"Send interactive buttons" option handler for senders with `MENU` state.
"""
if sender_state_data_updater(notification):
return initial_handler(notification)

sender = notification.sender
sender_state_data = notification.state_manager.get_state_data(sender)

try:
sender_lang_code = sender_state_data[LANGUAGE_CODE_KEY]

message_text = (
f'{answers_data["sending_buttons_notice"][sender_lang_code]}'
f'{answers_data["buttons_warning"][sender_lang_code]}'
f'{answers_data["send_poll_message_1"][sender_lang_code]}'
f'{answers_data["links"][sender_lang_code]["send_interactive_buttons_documentation"]}\n'
f'{answers_data["links"][sender_lang_code]["send_interactive_buttons_reply_documentation"]}'
)

buttons_demo_title = answers_data["buttons_demo_title"][sender_lang_code]
buttons_demo_message = answers_data["buttons_demo_message"][sender_lang_code] + answers_data["links"][sender_lang_code]["send_interactive_buttons_documentation"]
buttons_demo_footer = answers_data["buttons_demo_footer"][sender_lang_code]

reply_buttons_title = answers_data["reply_buttons_title"][sender_lang_code]
reply_buttons_message = answers_data["reply_buttons_message"][sender_lang_code] + answers_data["links"][sender_lang_code]["send_interactive_buttons_reply_documentation"]
reply_buttons_footer = answers_data["reply_buttons_footer"][sender_lang_code]

notification.answer(message_text)

notification.answer_with_interactive_buttons(
buttons_demo_message,
[{
"type": "call",
"buttonId": "1",
"buttonText": answers_data["call_me_button"][sender_lang_code],
"phoneNumber": "79123456789"
},
{
"type": "url",
"buttonId": "2",
"buttonText": answers_data["link_button"][sender_lang_code],
"url": answers_data["links"][sender_lang_code]["send_interactive_buttons_documentation"]
}],
buttons_demo_title,
buttons_demo_footer
)

notification.answer_with_interactive_buttons_reply(
reply_buttons_message,
[{
"buttonId": "menu_button",
"buttonText": answers_data["menu_button"][sender_lang_code]
},
{
"buttonId": "stop_button",
"buttonText": answers_data["stop_button"][sender_lang_code]
}],
reply_buttons_title,
reply_buttons_footer
)
except Exception as e:
logger.exception(e)
return

@bot.router.message(
type_message=TEXT_TYPES,
Expand Down Expand Up @@ -794,6 +865,47 @@ def main_menu_menu_handler(notification: Notification) -> None:
caption=answer_text,
)

@bot.router.message(type_message="templateButtonsReplyMessage")
@debug_profiler(logger=logger)
def template_buttons_reply_handler(notification: Notification) -> None:
"""
Handler for template buttons replies
"""
if sender_state_data_updater(notification):
return initial_handler(notification)

sender = notification.sender
sender_state_data = notification.state_manager.get_state_data(sender)

try:
event = notification.event
message_data = event.get("messageData", {})

template_button_data = message_data.get("templateButtonsReplyMessage", {})
if not template_button_data:
template_button_data = message_data

selected_button_id = template_button_data.get("selectedId", "unknown")

if selected_button_id == "unknown" and "templateButtonReplyMessage" in message_data:
button_data = message_data["templateButtonReplyMessage"]
selected_button_id = button_data.get("selectedId", "unknown")

sender_lang_code = sender_state_data.get(LANGUAGE_CODE_KEY, "en")

if selected_button_id == "menu_button":
notification.state_manager.update_state(sender, States.MENU.value)
return main_menu_menu_handler(notification)
elif selected_button_id == "stop_button":
notification.state_manager.update_state(sender, States.MENU.value)
return main_menu_stop_handler(notification)
else:
unknown_button_answer = answers_data.get("unknown_button", {}).get(sender_lang_code, "Unknown button clicked")
notification.answer(unknown_button_answer)

except Exception as e:
logger.exception(f"Error in template_buttons_reply_handler: {e}")
notification.answer("Error processing button click")

@bot.router.message(
state=States.CHAT_GPT.value,
Expand Down
Loading