From 0898b8c78f70ecd03b31fbe2687b6691526a0bcc Mon Sep 17 00:00:00 2001 From: mispasyuk Date: Thu, 25 Apr 2024 17:14:42 +0300 Subject: [PATCH 1/2] echobot --- .gitignore | 1 + Todobot/__init__.py | 0 Todobot/bot.py | 69 +++++++++++++++++++++++++++++++++++++++++++++ run_todobot.py | 4 +++ 4 files changed, 74 insertions(+) create mode 100644 Todobot/__init__.py create mode 100644 Todobot/bot.py create mode 100644 run_todobot.py diff --git a/.gitignore b/.gitignore index ab12272..579fbb1 100644 --- a/.gitignore +++ b/.gitignore @@ -356,3 +356,4 @@ dmypy.json # Cython debug symbols cython_debug/ +Todobot/config.py \ 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..9da8a2d --- /dev/null +++ b/Todobot/bot.py @@ -0,0 +1,69 @@ +#!/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 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, Pythonchik") + + +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(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/run_todobot.py b/run_todobot.py new file mode 100644 index 0000000..f6fe9de --- /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 From 326111cc4bb47f01f821a377df02e56f59887ea7 Mon Sep 17 00:00:00 2001 From: mispasyuk Date: Thu, 25 Apr 2024 17:30:37 +0300 Subject: [PATCH 2/2] Add todolist --- Todobot/bot.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Todobot/bot.py b/Todobot/bot.py index 9da8a2d..9b76cb5 100644 --- a/Todobot/bot.py +++ b/Todobot/bot.py @@ -21,6 +21,8 @@ from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters from Todobot.config import TOKEN +Tasks = dict() + # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -44,9 +46,19 @@ 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: +async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: """Echo the user message.""" - await update.message.reply_text(update.message.text) + user = update.effective_user + some_text = update.message.text + if not Tasks.get(user.id): + Tasks[user.id] = [] + Tasks[user.id].append(some_text) + + message = 'Задача добавлена \n\n' + message = f'{message}Список задач:\n' + tasks_list = '\n'.join(Tasks[user.id]) + message = f'{message}{tasks_list}' + await update.message.reply_text(message) def main() -> None: @@ -59,7 +71,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)