From c763e76ec7633217183315c1f298945e8b7bc2de Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Sat, 10 May 2025 16:51:17 +0200 Subject: [PATCH] Set name, description and short description at startup. Notify guests of being granted access. Use new users functions. --- bot.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index be022e1..91254c2 100644 --- a/bot.py +++ b/bot.py @@ -1,9 +1,18 @@ from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes -from config import BOT_TOKEN +from config import get_bot_token, get_commands from access_control import can_open_gate, grant_access, get_grantor from gates import get_name, open_gate -from users import get_role, set_credentials, get_credentials, get_grantor_credentials, get_admins, update_user +from users import get_role, set_credentials, get_credentials, get_grantor_credentials, get_admins, update_user, get_fullname, get_username + +BOT_NAME = "lagomareGateKeeperBot" + +async def post_init(application: Application) -> None: + bot_commands = get_commands(BOT_NAME) + await application.bot.set_my_commands(bot_commands) + await application.bot.set_my_name("Lagomare GateKeeper Bot") + await application.bot.set_my_description("This bot is used to open Lagomare gates by members. You can also request timed access if you are a guest.") + await application.bot.set_my_short_description("Open Lagomare gates or request guest access") async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE): user_id = str(update.effective_user.id) @@ -61,15 +70,15 @@ async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE): if not context.args: return await update.message.reply_text("Usage: `/requestaccess `") gate = context.args[0] - requester = update.effective_user.username or update.effective_user.full_name - text = (f"Access request: @{requester} ({user_id}) requests access to *{gate}*.\nUse `/approve {user_id} {gate} 60` to grant access.") + requester = get_fullname(user_id) or get_username(user_id) + text = (f"Access request: {requester} ({user_id}) requests access to *{gate}*.\nUse `/approve {user_id} {gate} 60` to grant access.") await update.message.reply_text("Your request has been submitted.") admins = get_admins() for admin_id in admins: try: await context.bot.send_message(chat_id=admin_id, text=text, parse_mode="Markdown") - except: - pass + except Exception as e: + print(f"Failed to notify {admin_id} that guest {user_id} requested access for {gate}: {e}") async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE): approver_id = str(update.effective_user.id) @@ -80,13 +89,16 @@ async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE): gate = context.args[1] expires_at = context.args[2] grant_access(user_id, gate, expires_at, grantor_id=approver_id) - await update.message.reply_text(f"Access to {gate} granted for user {user_id}.") - #TODO: notify guest of granted access + await update.message.reply_text(f"Access to {gate} granted to user {user_id}.") + try: + await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}") + except Exception as e: + print(f"Failed to notify {user_id} that admin {approver_id} approved access for {gate} up to {expires_at}: {e}") except: await update.message.reply_text("Usage: `/approve `") def main(): - app = Application.builder().token(BOT_TOKEN).build() + app = Application.builder().token(get_bot_token(BOT_NAME)).post_init(post_init).build() app.add_handler(MessageHandler(None, updateuser), group=1) app.add_handler(CommandHandler("start", start)) app.add_handler(CommandHandler("setcredentials", setcredentials))