From 1a1e28fd20eb9b91f27128cf87fb4984f6d3428d Mon Sep 17 00:00:00 2001 From: LeTim42 Date: Thu, 25 Apr 2024 17:16:51 +0300 Subject: [PATCH 1/2] Add todo bot --- .gitignore | 2 ++ todo_bot/bot.py | 73 ++++++++++++++++++++++++++++++++++++++++ todo_bot/run_todo_bot.py | 4 +++ 3 files changed, 79 insertions(+) create mode 100644 todo_bot/bot.py create mode 100644 todo_bot/run_todo_bot.py diff --git a/.gitignore b/.gitignore index ab12272..02035ab 100644 --- a/.gitignore +++ b/.gitignore @@ -356,3 +356,5 @@ dmypy.json # Cython debug symbols cython_debug/ +.idea/ +todo_bot/config.py \ No newline at end of file diff --git a/todo_bot/bot.py b/todo_bot/bot.py new file mode 100644 index 0000000..970b7f7 --- /dev/null +++ b/todo_bot/bot.py @@ -0,0 +1,73 @@ +#!/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 config import TELEGRAM_BOT_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_html( + rf"Hi {user.mention_html()}!", + reply_markup=ForceReply(selective=True), + ) + + +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_BOT_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) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/todo_bot/run_todo_bot.py b/todo_bot/run_todo_bot.py new file mode 100644 index 0000000..52cea9f --- /dev/null +++ b/todo_bot/run_todo_bot.py @@ -0,0 +1,4 @@ +from bot import main + +if __name__ == "__main__": + main() From d4d7b5c3e69417661c366c15e7f16acfee321a60 Mon Sep 17 00:00:00 2001 From: LeTim42 Date: Thu, 25 Apr 2024 17:31:43 +0300 Subject: [PATCH 2/2] Update todo bot --- todo_bot/bot.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/todo_bot/bot.py b/todo_bot/bot.py index 970b7f7..6e91e53 100644 --- a/todo_bot/bot.py +++ b/todo_bot/bot.py @@ -22,6 +22,8 @@ from config import TELEGRAM_BOT_TOKEN +TASKS = dict() + # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -38,19 +40,33 @@ 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_html( - rf"Hi {user.mention_html()}!", + rf"Привет, {user.mention_html()}!", reply_markup=ForceReply(selective=True), ) 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!") + await update.message.reply_text("<тут должна была быть помощь>") + + +async def tasks_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + """Send a message when the command /help is issued.""" + user_id = update.effective_user.id + if user_id in TASKS: + await update.message.reply_text("Список задач:\n" + '\n'.join(TASKS[user_id])) + else: + await update.message.reply_text("У вас пока нет задач") -async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: +async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Echo the user message.""" - await update.message.reply_text(update.message.text) + user_id = update.effective_user.id + task = update.message.text + if user_id not in TASKS: + TASKS[user_id] = [] + TASKS[user_id].append(task) + await update.message.reply_text("Задача добавлена!") def main() -> None: @@ -61,9 +77,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("tasks", tasks_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)