-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the start function and initiated the bot in echo mode
- Loading branch information
1 parent
3a3f189
commit 8afacbc
Showing
5 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from telegram import Update | ||
from telegram.ext import ( | ||
filters, | ||
ContextTypes, | ||
CommandHandler, | ||
MessageHandler | ||
) | ||
|
||
def log_errors(f): | ||
def inner(*args, **kwargs): | ||
try: | ||
return f(*args, **kwargs) | ||
except Exception as e: | ||
error_message = f'Произошла ошибка: {e}' | ||
print(error_message) | ||
raise e | ||
return inner | ||
|
||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
await context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text=( | ||
'Привет, я бот онлайн школы GoodStart.\n\n' | ||
'Здесь ты найдёшь лучших преподавателей.') | ||
) | ||
|
||
@log_errors | ||
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE): | ||
await context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text=update.message.text | ||
) | ||
|
||
start_handler = CommandHandler('start', start) | ||
echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import logging | ||
import sys | ||
|
||
from django.core.management.base import BaseCommand | ||
from django.conf import settings | ||
from telegram.error import Forbidden | ||
from telegram.ext import ApplicationBuilder | ||
|
||
from bot.handlers import start_handler, echo_handler | ||
|
||
class Command(BaseCommand): | ||
help = 'Телеграм-бот' | ||
|
||
def handle(self, *args, **kwargs): | ||
try: | ||
pass | ||
except Forbidden: | ||
logging.error("Invalid TELEGRAM_TOKEN.") | ||
sys.exit(1) | ||
|
||
bot = ApplicationBuilder().token(settings.TELEGRAM_TOKEN).build() | ||
bot.add_handler(start_handler) | ||
bot.add_handler(echo_handler) | ||
bot.run_polling() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters