From 7220c57a5be0e380bd1e24218345e4b3c2c588f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Thu, 25 Apr 2024 17:02:21 +0300 Subject: [PATCH 1/3] echoooooooboooot --- .gitignore | 2 ++ run_todobot.py | 4 +++ todobot/__init__.py | 0 todobot/bot.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ todobot/config.py | 1 + 5 files changed, 75 insertions(+) create mode 100644 run_todobot.py create mode 100644 todobot/__init__.py create mode 100644 todobot/bot.py create mode 100644 todobot/config.py diff --git a/.gitignore b/.gitignore index ab12272..fa849b4 100644 --- a/.gitignore +++ b/.gitignore @@ -356,3 +356,5 @@ dmypy.json # Cython debug symbols cython_debug/ +.todobot/config.py + diff --git a/run_todobot.py b/run_todobot.py new file mode 100644 index 0000000..75fa8ab --- /dev/null +++ b/run_todobot.py @@ -0,0 +1,4 @@ +from todobot.bot import main + +if __name__ == '__main__': + main() 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..aad2964 --- /dev/null +++ b/todobot/bot.py @@ -0,0 +1,68 @@ +#!/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( + rf"Hello homie {user.name}!" + ) + + +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 diff --git a/todobot/config.py b/todobot/config.py new file mode 100644 index 0000000..6b37c11 --- /dev/null +++ b/todobot/config.py @@ -0,0 +1 @@ +TELEGRAM_TOKEN='7044892636:AAGrYFjs8m4SqsZWab-rzUD1KOZWQTeiuTE' \ No newline at end of file From 512ca39e834f1412fdf44e42bb6d3c0e3cef670b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Thu, 25 Apr 2024 17:12:19 +0300 Subject: [PATCH 2/3] echoooooooboooot2 --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fa849b4..c7216f6 100644 --- a/.gitignore +++ b/.gitignore @@ -356,5 +356,5 @@ dmypy.json # Cython debug symbols cython_debug/ -.todobot/config.py +todobot/config.py From a02f063ed5303832af47710744ab7fd1d5b3591f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= Date: Thu, 25 Apr 2024 18:50:54 +0300 Subject: [PATCH 3/3] todobot with commands(view help) --- todobot/bot.py | 45 ++++++++++++++++++++++++++++++++++++++++++--- todobot/config.py | 2 +- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/todobot/bot.py b/todobot/bot.py index aad2964..cd63de4 100644 --- a/todobot/bot.py +++ b/todobot/bot.py @@ -22,6 +22,10 @@ from todobot.config import TELEGRAM_TOKEN +TASKS= dict() +commands = {'/help' : 'view commands','/start':'hi)', + '/add_action':'add action in todo list','/review_actions':' view actions from todo list'} + # Enable logging logging.basicConfig( format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO @@ -45,10 +49,42 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: 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!") + for command,description in commands.items(): + await update.message.reply_text(f'{command} - {description}') + + +async def add_action_to_list(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + user = update.effective_user + await update.message.reply_text("Пожалуйста, отправьте текст задачи после команды /add_action.") + context.chat_data["waiting_for_task"] = True + +async def get_next_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + if "waiting_for_task" in context.chat_data and context.chat_data["waiting_for_task"]: + user = update.effective_user + next_message = update.message.text + if user.id not in TASKS or not TASKS[user.id]: + TASKS[user.id] = [] + TASKS[user.id].append(next_message) + del context.chat_data["waiting_for_task"] + await update.message.reply_text("Задача добавлена.") + else: + pass -async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - """Echo the user message.""" + +async def return_actions_from_list(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + user = update.effective_user + if user.id not in TASKS or not TASKS[user.id]: + message='У вас нет задач' + await update.message.reply_text(message) + else: + message='Список задач:\n\n' + tasks_list='\n'.join(TASKS[user.id]) + message=f'{message}{tasks_list}' + await update.message.reply_text(message) + + +async def text_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text(update.message.text) @@ -60,9 +96,12 @@ 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_action", add_action_to_list)) + application.add_handler(CommandHandler("review_actions", return_actions_from_list)) + # 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, get_next_message)) # Run the bot until the user presses Ctrl-C application.run_polling(allowed_updates=Update.ALL_TYPES) \ No newline at end of file diff --git a/todobot/config.py b/todobot/config.py index 6b37c11..85d72ba 100644 --- a/todobot/config.py +++ b/todobot/config.py @@ -1 +1 @@ -TELEGRAM_TOKEN='7044892636:AAGrYFjs8m4SqsZWab-rzUD1KOZWQTeiuTE' \ No newline at end of file +TELEGRAM_TOKEN='7044892636:AAEShwyC-DR_yBZa55rW0SlCBogbo6m1Qnw' \ No newline at end of file