Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/menu_buttons #32

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions src/bot/constants/info/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
key.DESCRIPTION: "Временное описание кнопки главного меню",
key.MENU: [
f"{key.MENU}_SUB",
f"{key.MENU}_SUB2",
],
}
}
Expand All @@ -16,14 +17,11 @@
key.NAME: "Кнопка 1",
key.DESCRIPTION: "Описание 1",
key.PARENT: f"{key.MENU}_MAIN",
key.MENU: [
f"{key.MENU}_SUB2",
],
},
f"{key.MENU}_SUB2": {
key.NAME: "Кнопка 2",
key.DESCRIPTION: "Описание 2",
key.PARENT: f"{key.MENU}_SUB",
key.PARENT: f"{key.MENU}_MAIN",
},
}

Expand Down
3 changes: 3 additions & 0 deletions src/bot/constants/info/text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# MAIN MENU
START_MESSAGE = 'Здесь должно быть приветственное сообщение (Заглушка)!'

# MAIN
BACK = "Назад"

WELCOME_MESSAGE = (
'Добро пожаловать {}, в группу по реабилитации '
'<a href="https://rsmu.ru/index.php?id=1313">РНИМУ</a> и благотворительного фонда '
Expand Down
1 change: 1 addition & 0 deletions src/bot/constants/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
NAME = "NAME"
DESCRIPTION = "DESCRIPTION"
ASK = "ASK"
PARENT = "PARENT"
9 changes: 3 additions & 6 deletions src/bot/conversations/main_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from telegram.constants import ParseMode
from telegram.ext import ContextTypes, ConversationHandler

from bot.constants import state
# from bot.constants.info.menu import ALL_MENU
# uncomment after adding the menu manager
from bot.constants import state, key
from bot.constants.info.menu import ALL_MENU
from bot.constants.info.text import START_MESSAGE, STOP_MESSAGE, WELCOME_MESSAGE
from bot.conversations.menu_application import menu

Expand All @@ -16,8 +15,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):


async def main_menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
# context.user_data[key.MENU] = ALL_MENU[f"{key.MENU}_MAIN"]
# uncomment after adding the menu manager
context.user_data[key.MENU] = ALL_MENU[f"{key.MENU}_MAIN"]
await menu(update, context)
return state.MAIN_MENU

Expand All @@ -34,4 +32,3 @@ async def stop(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Stops the conversation and replies with the given message."""
await update.message.reply_text(STOP_MESSAGE)
return ConversationHandler.END

31 changes: 30 additions & 1 deletion src/bot/conversations/menu_application.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
from telegram import InlineKeyboardButton as Button
from telegram import InlineKeyboardMarkup as Keyboard
from telegram import Update
from telegram.ext import ContextTypes

from bot.constants import key, state
from bot.constants.info import text
from bot.constants.info.menu import ALL_MENU
from bot.utils import send_message


async def menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
pass
"""Show the selected menu or sub-menu to the user."""

callback = update.callback_query
user_data = context.user_data

if callback and callback.data and callback.data.startswith(key.MENU):
menu = ALL_MENU[callback.data]
user_data[key.MENU] = menu
else:
menu = user_data[key.MENU]

buttons = [
[Button(text=ALL_MENU[menu_name][key.NAME], callback_data=menu_name)]
for menu_name in menu.get(key.MENU, [])
]
if parent_menu := menu.get(key.PARENT):
buttons.append([Button(text=text.BACK, callback_data=parent_menu)])

menu_keyboard = Keyboard(buttons)

await send_message(update, menu[key.DESCRIPTION], keyboard=menu_keyboard)

return state.MAIN_MENU
11 changes: 6 additions & 5 deletions src/bot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bot.conversations import form_application
from bot.conversations.main_application import greet_new_member

from bot.conversations import menu_application

form_handler = ConversationHandler(
entry_points=[
Expand Down Expand Up @@ -46,21 +47,21 @@
main_handler = ConversationHandler(
entry_points=[
CommandHandler('start', start),
CallbackQueryHandler(start, pattern='^start$'),
],
states={
MAIN_MENU: [
form_handler,
# CallbackQueryHandler(
# menu_application.menu, pattern=fr"^{key.MENU}_\S*$"
# ),
# uncomment after adding the menu manager
CallbackQueryHandler(
menu_application.menu, pattern=fr"^{key.MENU}_\S*$"
),
],
},
fallbacks=[
CommandHandler('menu', main_menu),
CommandHandler('stop', stop)
],
allow_reentry=True,
allow_reentry=True
)

greet_new_member_handler = MessageHandler(
Expand Down
24 changes: 24 additions & 0 deletions src/bot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from email.mime.text import MIMEText
from smtplib import SMTP_SSL, SMTPException

from telegram import InlineKeyboardMarkup, Update

from bot.core.settings import settings


Expand Down Expand Up @@ -30,3 +32,25 @@ def send_email_message(message: str, subject: str, recipient: str) -> bool:
return True
except SMTPException:
return False


async def send_message(
update: Update,
text: str,
keyboard: InlineKeyboardMarkup,
link_preview: bool = False
):
"""Send a message to a user using the inline keyboard"""
query = update.callback_query
if query:
await query.answer()
await query.message.edit_text(
text=text,
reply_markup=keyboard,
disable_web_page_preview=not link_preview
)
await update.message.reply_text(
text=text,
reply_markup=keyboard,
disable_web_page_preview=not link_preview
)