-
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.
Fixed problem with Django and telegram-api-bot
- Loading branch information
1 parent
8afacbc
commit 5d45b81
Showing
10 changed files
with
108 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -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() |
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,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() |
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
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
from core.logging.logger import log_errors # noqa |
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,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 |