Changeset 0d67456 in python-cinema-club-bot
- Timestamp:
- Oct 9, 2024, 1:45:08 AM (5 weeks ago)
- Branches:
- master
- Children:
- cc34991
- Parents:
- 71efb80
- git-author:
- Mikhail Kirillov <w96k@…> (10/09/24 01:22:02)
- git-committer:
- Mikhail Kirillov <w96k@…> (10/09/24 01:45:08)
- Files:
-
- 3 added
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
main.py
r71efb80 r0d67456 14 14 MessageHandler, filters 15 15 from dotenv import load_dotenv 16 from rich import inspect17 16 import os 18 17 import logging 19 from .strings import INVALID_COMMAND 18 from rich import inspect 19 20 from strings import INVALID_COMMAND, USER_NOT_PROVIDED, USER_ADDED, \ 21 USER_REMOVED 22 from utils import context_init 23 from persistence import Persistence 24 20 25 21 26 load_dotenv() … … 46 51 47 52 48 async def add _choose_cycle(53 async def add( 49 54 update: Update, 50 55 context: ContextTypes.DEFAULT_TYPE 51 56 ) -> None: 52 co mmand = update.message.text57 context_init(context) 53 58 54 user = command.replace("/add", "") 59 if context.args == []: 60 await update.message.reply_text(USER_NOT_PROVIDED) 61 raise error.TelegramError(USER_NOT_PROVIDED) 55 62 56 inspect(user) 63 for user in context.args: 64 context.chat_data["users"].append(user) 65 await update.message.reply_text(USER_ADDED.format(user=user)) 57 66 58 try: 59 if user == "": 60 raise error.TelegramError("User is not provided. Use /add @username") 61 except error.TelegramError as err: 62 await update.message.reply_text(err.message) 63 return 67 await update.message.reply_text(context.chat_data) 64 68 65 await update.message.reply_text(update.message.text) 69 70 async def list( 71 update: Update, 72 context: ContextTypes.DEFAULT_TYPE 73 ) -> None: 74 context_init(context) 75 76 await update.message.reply_text(context.chat_data) 77 78 79 async def remove( 80 update: Update, 81 context: ContextTypes.DEFAULT_TYPE 82 ) -> None: 83 context_init(context) 84 85 if context.args == []: 86 await update.message.reply_text(USER_NOT_PROVIDED) 87 raise error.TelegramError(USER_NOT_PROVIDED) 88 89 for user in context.args: 90 if user == "*": 91 context.chat_data["users"] = [] 92 break 93 94 context.chat_data["users"].remove(user) 95 await update.message.reply_text(USER_REMOVED.format(user=user)) 96 97 await update.message.reply_text(context.chat_data) 66 98 67 99 68 100 async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE): 69 await context.bot.send_message(chat_id=update.effective_chat.id, text=INVALID_COMMAND) 101 await context.bot.send_message( 102 chat_id=update.effective_chat.id, 103 text=INVALID_COMMAND 104 ) 70 105 71 app = ApplicationBuilder().token(72 os.environ.get('TELEGRAM_TOKEN')73 ).build()74 106 75 app.add_handler(CommandHandler("about", about)) 76 app.add_handler(CommandHandler("add", add_choose_cycle)) 107 if __name__ == "__main__": 108 app = ApplicationBuilder().token( 109 os.environ.get("TELEGRAM_TOKEN") 110 ).persistence(Persistence).build() 77 111 78 app.add_handler(MessageHandler(filters.COMMAND, unknown)) 112 app.add_handler(CommandHandler("about", about)) 113 app.add_handler(CommandHandler("add", add)) 114 app.add_handler(CommandHandler("list", list)) 115 app.add_handler(CommandHandler("remove", remove)) 79 116 80 app.run_polling() 117 118 app.add_handler(MessageHandler(filters.COMMAND, unknown)) 119 120 app.run_polling() -
strings.py
r71efb80 r0d67456 11 11 12 12 INVALID_COMMAND = "Invalid command. Available commands: /add" 13 USER_NOT_PROVIDED = "User(s) is not provided" 14 USER_ADDED = "User {user} has been added" 15 USER_REMOVED = "User {user} has been removed"
Note:
See TracChangeset
for help on using the changeset viewer.