Skip to content

Commit 44a3241

Browse files
committed
feat: automatically sync bot commands to Telegram menu on startup
1 parent 1efaece commit 44a3241

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

main.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ async def log_update(update, context):
3939
logger.info(log_message)
4040

4141

42+
async def post_init(application: Application):
43+
"""Set the bot commands on startup."""
44+
from telegram import BotCommand
45+
commands = []
46+
for command_name, command_info in COMMANDS.items():
47+
if 'description' in command_info and command_info['description']:
48+
# Telegram commands must be lowercase and 1-32 chars
49+
commands.append(BotCommand(command_name.lower(), command_info['description'][:256]))
50+
51+
try:
52+
await application.bot.set_my_commands(commands)
53+
logging.getLogger("DCUBABOT").info("Successfully set bot commands.")
54+
except Exception as e:
55+
logging.getLogger("DCUBABOT").error(f"Failed to set bot commands: {e}")
56+
4257
def main():
4358
"""Start the bot."""
4459
logging.basicConfig(
@@ -47,7 +62,7 @@ def main():
4762
)
4863

4964

50-
application = Application.builder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build()
65+
application = Application.builder().token(os.environ["TELEGRAM_BOT_TOKEN"]).post_init(post_init).build()
5166

5267
application.add_error_handler(error_handler)
5368

0 commit comments

Comments
 (0)