Changeset 0d67456


Ignore:
Timestamp:
Oct 9, 2024, 1:45:08 AM (5 weeks ago)
Author:
Mikhail Kirillov <w96k@…>
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)
Message:

Add /remove and /list commands (unfinished)

Files:
3 added
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • main.py

    r71efb80 r0d67456  
    1414    MessageHandler, filters
    1515from dotenv import load_dotenv
    16 from rich import inspect
    1716import os
    1817import logging
    19 from .strings import INVALID_COMMAND
     18from rich import inspect
     19
     20from strings import INVALID_COMMAND, USER_NOT_PROVIDED, USER_ADDED, \
     21    USER_REMOVED
     22from utils import context_init
     23from persistence import Persistence
     24
    2025
    2126load_dotenv()
     
    4651
    4752
    48 async def add_choose_cycle(
     53async def add(
    4954        update: Update,
    5055        context: ContextTypes.DEFAULT_TYPE
    5156) -> None:
    52     command = update.message.text
     57    context_init(context)
    5358
    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)
    5562
    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))
    5766
    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)
    6468
    65     await update.message.reply_text(update.message.text)
     69
     70async 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
     79async 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)
    6698
    6799
    68100async 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    )
    70105
    71 app = ApplicationBuilder().token(
    72     os.environ.get('TELEGRAM_TOKEN')
    73 ).build()
    74106
    75 app.add_handler(CommandHandler("about", about))
    76 app.add_handler(CommandHandler("add", add_choose_cycle))
     107if __name__ == "__main__":   
     108    app = ApplicationBuilder().token(
     109        os.environ.get("TELEGRAM_TOKEN")
     110    ).persistence(Persistence).build()
    77111
    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))
    79116
    80 app.run_polling()
     117
     118    app.add_handler(MessageHandler(filters.COMMAND, unknown))
     119
     120    app.run_polling()
  • strings.py

    r71efb80 r0d67456  
    1111
    1212INVALID_COMMAND = "Invalid command. Available commands: /add"
     13USER_NOT_PROVIDED = "User(s) is not provided"
     14USER_ADDED = "User {user} has been added"
     15USER_REMOVED = "User {user} has been removed"
Note: See TracChangeset for help on using the changeset viewer.