Skip to content

Commit

Permalink
Added the start function and initiated the bot in echo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
merkme-portfolio committed Apr 27, 2024
1 parent 3a3f189 commit 8afacbc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/bot/handlers.py
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 added src/bot/management/__init__.py
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions src/bot/management/commands/runbot.py
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()
2 changes: 2 additions & 0 deletions src/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@
STATIC_URL = "static/"

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

TELEGRAM_TOKEN = 'нужно импортировать из environ'

0 comments on commit 8afacbc

Please sign in to comment.