Skip to content

Commit

Permalink
Fixed problem with Django and telegram-api-bot
Browse files Browse the repository at this point in the history
  • Loading branch information
merkme-portfolio committed Apr 30, 2024
1 parent 8afacbc commit 5d45b81
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 35 deletions.
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ python = "^3.12"
django = "^5.0.4"
python-telegram-bot = "^21.1.1"
poetry-plugin-export = "^1.7.1"
django-asgi-lifespan = "^0.3.1"


[build-system]
Expand Down
19 changes: 19 additions & 0 deletions src/bot/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
from django.apps import AppConfig
from django_asgi_lifespan.signals import asgi_shutdown


class BotConfig(AppConfig):
"""Configuration class for the bot application."""

default_auto_field = "django.db.models.BigAutoField"
name = "bot"

def stop_bot(self, **kwargs):
self.bot.stop()

def ready(self) -> None:
"""Perform actions when the application is ready."""
import os

if os.environ.get('RUN_MAIN', None) != 'true':
from bot.bot_interface import Bot

self.bot = Bot()

asgi_shutdown.connect(self.stop_bot)

self.bot.start()
55 changes: 55 additions & 0 deletions src/bot/bot_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import asyncio
import signal
import threading

from django.conf import settings
from telegram.ext import ApplicationBuilder

from bot.handlers import start_handler, echo_handler


class Bot:
"""A class representing a Telegram bot."""

def __init__(self):
"""Initialize the bot."""
self._app: None
self._stop_event = threading.Event()

def start(self):
"""Start the bot."""
self._stop_event.clear()
bot_thread = threading.Thread(target=self._run)
bot_thread.start()
signal.signal(signal.SIGINT, self._handle_sigint)

def stop(self):
"""Stop the bot."""
self._stop_event.set()

async def _build_app(self):
"""Build the application."""
app = ApplicationBuilder().token(settings.TELEGRAM_TOKEN).build()
app.add_handler(start_handler)
app.add_handler(echo_handler)
return app

def _run(self):
"""Run the bot."""
asyncio.set_event_loop(asyncio.new_event_loop())
asyncio.get_event_loop().run_until_complete(self._start_bot())

async def _start_bot(self):
"""Start the bot."""
self._app = await self._build_app()
await self._app.initialize()
await self._app.updater.start_polling()
await self._app.start()
while not self._stop_event.is_set():
await asyncio.sleep(1)

await self._app.stop()

def _handle_sigint(self, signum, frame):
"""Handle pressing CTRL+C in terminal."""
self.stop()
16 changes: 6 additions & 10 deletions src/bot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
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
from core.logging import log_errors


async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handler for the /start command."""
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):
"""Handler for echoing user messages."""
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)
echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo)
Empty file removed src/bot/management/__init__.py
Empty file.
Empty file.
24 changes: 0 additions & 24 deletions src/bot/management/commands/runbot.py

This file was deleted.

1 change: 1 addition & 0 deletions src/core/logging/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from core.logging.logger import log_errors # noqa
10 changes: 10 additions & 0 deletions src/core/logging/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def log_errors(f):
"""Decorator to log errors raised by the decorated function."""
def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
error_message = f'Произошла ошибка: {e}'
print(error_message)
raise e
return inner

0 comments on commit 5d45b81

Please sign in to comment.