From 36a7d296c99c09c614125015cd1b44469d9b2e57 Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Sun, 28 Apr 2024 21:40:26 +0300 Subject: [PATCH 1/6] add test bot --- .gitignore | 2 ++ run_todobot.py | 4 +++ todobot/__init__.py | 0 todobot/bot.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 run_todobot.py create mode 100644 todobot/__init__.py create mode 100644 todobot/bot.py diff --git a/.gitignore b/.gitignore index ab12272..ec50bae 100644 --- a/.gitignore +++ b/.gitignore @@ -356,3 +356,5 @@ dmypy.json # Cython debug symbols cython_debug/ +# Config of todo bot +todobot/config.py \ No newline at end of file diff --git a/run_todobot.py b/run_todobot.py new file mode 100644 index 0000000..9200781 --- /dev/null +++ b/run_todobot.py @@ -0,0 +1,4 @@ +from todobot.bot import main + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/todobot/__init__.py b/todobot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todobot/bot.py b/todobot/bot.py new file mode 100644 index 0000000..86128e5 --- /dev/null +++ b/todobot/bot.py @@ -0,0 +1,70 @@ +# !/usr/bin/env python +# pylint: disable=unused-argument +# This program is dedicated to the public domain under the CC0 license. + +""" +Simple Bot to reply to Telegram messages. +First, a few handler functions are defined. Then, those functions are passed to +the Application and registered at their respective places. +Then, the bot is started and runs until we press Ctrl-C on the command line. +Usage: +Basic Echobot example, repeats messages. +Press Ctrl-C on the command line or send a signal to the process to stop the +bot. +""" + +import logging + +from telegram import ForceReply, Update +from telegram.ext import ( + Application, + CommandHandler, + ContextTypes, + MessageHandler, + filters, +) + +from todobot.config import TELEGRAM_TOKEN + +# Enable logging +logging.basicConfig( + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO +) +# set higher logging level for httpx to avoid all GET and POST requests being logged +logging.getLogger("httpx").setLevel(logging.WARNING) + +logger = logging.getLogger(__name__) + + +# Define a few command handlers. These usually take the two arguments update and +# context. +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /start is issued.""" + user = update.effective_user + await update.message.reply_text("Hello, my friend!") + + +async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /help is issued.""" + await update.message.reply_text("Help!") + + +async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Echo the user message.""" + await update.message.reply_text(update.message.text) + + +def main() -> None: + """Start the bot.""" + # Create the Application and pass it your bot's token. + application = Application.builder().token(TELEGRAM_TOKEN).build() + + # on different commands - answer in Telegram + application.add_handler(CommandHandler("start", start)) + application.add_handler(CommandHandler("help", help_command)) + + # on non command i.e message - echo the message on Telegram + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) + + # Run the bot until the user presses Ctrl-C + application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file From e93858e3095c28f15dd09dad3a7449db2616dec9 Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Sun, 28 Apr 2024 21:43:59 +0300 Subject: [PATCH 2/6] realisation of adding tasks --- todobot/bot.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/todobot/bot.py b/todobot/bot.py index 86128e5..4fae20f 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -4,9 +4,11 @@ """ Simple Bot to reply to Telegram messages. + First, a few handler functions are defined. Then, those functions are passed to the Application and registered at their respective places. Then, the bot is started and runs until we press Ctrl-C on the command line. + Usage: Basic Echobot example, repeats messages. Press Ctrl-C on the command line or send a signal to the process to stop the @@ -26,6 +28,8 @@ from todobot.config import TELEGRAM_TOKEN +TASKS: dict[int, list[str]] = dict() + # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -42,6 +46,7 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Send a message when the command /start is issued.""" user = update.effective_user await update.message.reply_text("Hello, my friend!") + await update.message.reply_text("I'm todo bot and I can add tasks to your list! 😃") async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -49,9 +54,15 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await update.message.reply_text("Help!") -async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Echo the user message.""" - await update.message.reply_text(update.message.text) +async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Add new task.""" + + user = update.effective_user + TASKS.setdefault(user.id, []).append(update.message.text) + + tasks = '\n'.join(TASKS[user.id]) + message = f"Task added to list. All list:\n{tasks}" + await update.message.reply_text(message) def main() -> None: @@ -64,7 +75,7 @@ def main() -> None: application.add_handler(CommandHandler("help", help_command)) # on non command i.e message - echo the message on Telegram - application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, text_handler)) # Run the bot until the user presses Ctrl-C application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file From 53ca25a57a1f385cf113bdd67cd92bda39fd8bca Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Mon, 29 Apr 2024 13:03:22 +0300 Subject: [PATCH 3/6] realise /add command --- todobot/bot.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/todobot/bot.py b/todobot/bot.py index 4fae20f..e9ea5de 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -17,7 +17,7 @@ import logging -from telegram import ForceReply, Update +from telegram import Update from telegram.ext import ( Application, CommandHandler, @@ -54,15 +54,17 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await update.message.reply_text("Help!") -async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Add new task.""" - +async def add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Add new task using /add command.""" user = update.effective_user - TASKS.setdefault(user.id, []).append(update.message.text) - - tasks = '\n'.join(TASKS[user.id]) - message = f"Task added to list. All list:\n{tasks}" - await update.message.reply_text(message) + task_description = ' '.join(context.args) + if task_description: + TASKS.setdefault(user.id, []).append(task_description) + tasks = '\n'.join(TASKS[user.id]) + message = f"Task added to list. All list:\n{tasks}" + await update.message.reply_text(message) + else: + await update.message.reply_text("Please provide a task description.") def main() -> None: @@ -73,9 +75,10 @@ def main() -> None: # on different commands - answer in Telegram application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("help", help_command)) + application.add_handler(CommandHandler("add", add_task)) # on non command i.e message - echo the message on Telegram - application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, text_handler)) + application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, add_task)) # Run the bot until the user presses Ctrl-C application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file From 2496cb7a778c4d7efbb580da8b07bff4cd41afb0 Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Mon, 29 Apr 2024 13:15:42 +0300 Subject: [PATCH 4/6] add /remove command --- todobot/bot.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/todobot/bot.py b/todobot/bot.py index e9ea5de..08ffb90 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -25,9 +25,9 @@ MessageHandler, filters, ) - from todobot.config import TELEGRAM_TOKEN + TASKS: dict[int, list[str]] = dict() # Enable logging @@ -66,6 +66,31 @@ async def add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: else: await update.message.reply_text("Please provide a task description.") +async def remove_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Remove a task using /remove command.""" + user = update.effective_user + args = context.args + if not args: + await update.message.reply_text("Please provide a task description or a task index.") + return + + # Check if the argument is a task description or a task index + try: + task_index = int(args[0]) - 1 + tasks = TASKS.get(user.id, []) + if 0 <= task_index < len(tasks): + removed_task = tasks.pop(task_index) + await update.message.reply_text(f"Removed task: {removed_task}") + else: + await update.message.reply_text("Invalid task index.") + except ValueError: + task_description = ' '.join(args) + tasks = TASKS.get(user.id, []) + if task_description in tasks: + tasks.remove(task_description) + await update.message.reply_text(f"Removed task: {task_description}") + else: + await update.message.reply_text("Task not found.") def main() -> None: """Start the bot.""" @@ -76,6 +101,7 @@ def main() -> None: application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("help", help_command)) application.add_handler(CommandHandler("add", add_task)) + application.add_handler(CommandHandler("remove", remove_task)) # on non command i.e message - echo the message on Telegram application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, add_task)) From 4865ba12150be41eab1cfb621c6109456d8b3b7e Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Mon, 29 Apr 2024 14:44:24 +0300 Subject: [PATCH 5/6] add /view command --- todobot/bot.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/todobot/bot.py b/todobot/bot.py index 08ffb90..905bd73 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -92,6 +92,16 @@ async def remove_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non else: await update.message.reply_text("Task not found.") +async def view_tasks(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """View all tasks in the user's list.""" + user = update.effective_user + tasks = TASKS.get(user.id, []) + if tasks: + tasks_message = '\n'.join(tasks) + await update.message.reply_text(f"Your tasks:\n{tasks_message}") + else: + await update.message.reply_text("You have no tasks.") + def main() -> None: """Start the bot.""" # Create the Application and pass it your bot's token. @@ -102,6 +112,7 @@ def main() -> None: application.add_handler(CommandHandler("help", help_command)) application.add_handler(CommandHandler("add", add_task)) application.add_handler(CommandHandler("remove", remove_task)) + application.add_handler(CommandHandler("view", view_tasks)) # on non command i.e message - echo the message on Telegram application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, add_task)) From b6324b8a57a79ceef7e542a39d278730bf34586b Mon Sep 17 00:00:00 2001 From: ArtemOvsyannikov Date: Thu, 6 Jun 2024 14:38:23 +0300 Subject: [PATCH 6/6] fix add_task func + linter --- todobot/bot.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/todobot/bot.py b/todobot/bot.py index 905bd73..c2f10b0 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -57,21 +57,26 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No async def add_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Add new task using /add command.""" user = update.effective_user - task_description = ' '.join(context.args) - if task_description: + task_description = " ".join(context.args) + + if not task_description: + await update.message.reply_text("Please provide a task description.") + return + else: TASKS.setdefault(user.id, []).append(task_description) - tasks = '\n'.join(TASKS[user.id]) + tasks = "\n".join(TASKS[user.id]) message = f"Task added to list. All list:\n{tasks}" await update.message.reply_text(message) - else: - await update.message.reply_text("Please provide a task description.") + async def remove_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Remove a task using /remove command.""" user = update.effective_user args = context.args if not args: - await update.message.reply_text("Please provide a task description or a task index.") + await update.message.reply_text( + "Please provide a task description or a task index." + ) return # Check if the argument is a task description or a task index @@ -84,7 +89,7 @@ async def remove_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non else: await update.message.reply_text("Invalid task index.") except ValueError: - task_description = ' '.join(args) + task_description = " ".join(args) tasks = TASKS.get(user.id, []) if task_description in tasks: tasks.remove(task_description) @@ -92,16 +97,18 @@ async def remove_task(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non else: await update.message.reply_text("Task not found.") + async def view_tasks(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """View all tasks in the user's list.""" user = update.effective_user tasks = TASKS.get(user.id, []) if tasks: - tasks_message = '\n'.join(tasks) + tasks_message = "\n".join(tasks) await update.message.reply_text(f"Your tasks:\n{tasks_message}") else: await update.message.reply_text("You have no tasks.") + def main() -> None: """Start the bot.""" # Create the Application and pass it your bot's token. @@ -118,4 +125,4 @@ def main() -> None: application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, add_task)) # Run the bot until the user presses Ctrl-C - application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file + application.run_polling(allowed_updates=Update.ALL_TYPES)