Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,5 @@ dmypy.json
# Cython debug symbols
cython_debug/

todobot/config.py

4 changes: 4 additions & 0 deletions run_todobot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from todobot.bot import main

if __name__ == '__main__':
main()
Empty file added todobot/__init__.py
Empty file.
107 changes: 107 additions & 0 deletions todobot/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/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

TASKS= dict()
commands = {'/help' : 'view commands','/start':'hi)',
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше в аппер кейсе

'/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
)
# 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!")
for command,description in commands.items():
await update.message.reply_text(f'{command} - {description}')
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не нужно отправлять разными сообщениями - лучше отформатируй и отправь одним



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 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)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ниже можно вписать return и сдвинуть нижний код левее

старайся держать код левее - это хорошая практика

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)


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))
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, get_next_message))

# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
1 change: 1 addition & 0 deletions todobot/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TELEGRAM_TOKEN='7044892636:AAEShwyC-DR_yBZa55rW0SlCBogbo6m1Qnw'