From 0d48ea28aeb8a6290d2aeab202efa9c8d6e30fe3 Mon Sep 17 00:00:00 2001 From: Mikhail Kirillov Date: Wed, 9 Oct 2024 03:40:27 +0400 Subject: Add /set command; Add error_handler; --- commands/__init__.py | 2 +- commands/users.py | 30 +++++++++++++++++++----------- main.py | 15 ++++++++++++++- strings.py | 10 +++++++--- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/commands/__init__.py b/commands/__init__.py index e2fd670..b494867 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -10,4 +10,4 @@ # from .meta import about -from .users import add_users, list_users, remove_users, chooser_user +from .users import set_users, add_users, list_users, remove_users, chooser_user diff --git a/commands/users.py b/commands/users.py index affdb53..413b681 100644 --- a/commands/users.py +++ b/commands/users.py @@ -13,11 +13,23 @@ from telegram import Update, error from telegram.ext import ContextTypes from collections import deque -from strings import USER_NOT_PROVIDED, USER_ADDED, USER_REMOVED, EXPECTED_ONE_USER +from strings import USER_NOT_PROVIDED, USERS_ADDED, USERS_REMOVED, \ + EXPECTED_ONE_USER, USER_SET, USER_ADD, USER_REMOVE from utils import context_init -from rich import inspect +async def set_users( + update: Update, + context: ContextTypes.DEFAULT_TYPE +) -> None: + context_init(context) + + if context.args == []: + raise error.TelegramError(USER_NOT_PROVIDED) + + context.chat_data["users"] = context.args + + await update.message.reply_text(USER_SET) async def add_users( update: Update, @@ -26,14 +38,13 @@ async def add_users( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) for user in context.args: context.chat_data["users"].append(user) - await update.message.reply_text(USER_ADDED.format(user=user)) + await update.message.reply_text(USER_ADD.format(user=user)) - await update.message.reply_text(context.chat_data["users"]) + await update.message.reply_text(USERS_ADDED) async def list_users( @@ -52,7 +63,6 @@ async def remove_users( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) for user in context.args: @@ -61,9 +71,9 @@ async def remove_users( break context.chat_data["users"].remove(user) - await update.message.reply_text(USER_REMOVED.format(user=user)) + await update.message.reply_text(USER_REMOVE.format(user=user)) - await update.message.reply_text(context.chat_data["users"]) + await update.message.reply_text(USERS_REMOVED) async def chooser_user( @@ -73,11 +83,9 @@ async def chooser_user( context_init(context) if context.args == []: - await update.message.reply_text(USER_NOT_PROVIDED) raise error.TelegramError(USER_NOT_PROVIDED) if len(context.args) > 1: - await update.message.reply_text(EXPECTED_ONE_USER) raise error.TelegramError(EXPECTED_ONE_USER) chooser = context.args[0] @@ -88,4 +96,4 @@ async def chooser_user( context.chat_data["users"] = list(users) - await update.message.reply_text(context.chat_data) + await update.message.reply_text(context.chat_data["users"]) diff --git a/main.py b/main.py index 7ca8b59..10c05a7 100644 --- a/main.py +++ b/main.py @@ -35,19 +35,32 @@ async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE): text=INVALID_COMMAND ) +async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None: + logging.error("Exception:", exc_info=context.error) + + await context.bot.send_message( + chat_id=update.effective_chat.id, + text=context.error.message + ) if __name__ == "__main__": app = ApplicationBuilder().token( os.environ.get("TELEGRAM_TOKEN") ).persistence(Persistence).build() + # Meta commands app.add_handler(CommandHandler("about", commands.about)) + + # Movie commands + app.add_handler(CommandHandler("set", commands.set_users)) app.add_handler(CommandHandler("add", commands.add_users)) app.add_handler(CommandHandler("list", commands.list_users)) app.add_handler(CommandHandler("remove", commands.remove_users)) app.add_handler(CommandHandler("chooser", commands.chooser_user)) - app.add_handler(MessageHandler(filters.COMMAND, unknown)) + app.add_error_handler(error_handler) + app.run_polling() + diff --git a/strings.py b/strings.py index 0a7efb9..84171b8 100644 --- a/strings.py +++ b/strings.py @@ -9,8 +9,12 @@ # with this software. If not, see: # -INVALID_COMMAND = "Invalid command. Available commands: /add" +INVALID_COMMAND = "Invalid command. Available commands: /add /list /remove /chooser /about" USER_NOT_PROVIDED = "User(s) is not provided" EXPECTED_ONE_USER = "Expected only one user" -USER_ADDED = "User {user} has been added" -USER_REMOVED = "User {user} has been removed" +USER_ADD = "User {user} has been added" +USERS_ADDED = "Users have been added successfully. Use /list to view." +USER_REMOVE = "User {user} has been removed. Use /list to view." +USERS_REMOVED = "Users has been removed" +USER_SET = "Users have been set successfully. Use /list to view." + -- cgit v1.2.3