from asgiref.sync import async_to_sync from django.conf import settings from telegram import ForceReply, Update from telegram.ext import Application, CommandHandler, MessageHandler, filters from telegram.constants import ParseMode, ChatType import logging from .apps import TgBotClass logger = logging.getLogger("root") class TgBotApp: _app = None async def start(self, update, context): user = update.effective_user logger.info(update) await update.message.reply_to_message( rf"Hi {user.mention_html()}!", reply_markup={} ) async def set_webhook(self, url): if not self._app: logger.error("no app") return app = self._app await app.bot.set_webhook(url, allowed_updates=Update.ALL_TYPES) async def init_tg(self): self._app = ( Application.builder() .token(settings.TGBOT["token"]) .concurrent_updates(True) .updater(None) .build() ) self._app.add_handler(CommandHandler("start", self.start)) self._app.add_handler(MessageHandler(filters.ChatType.PRIVATE, self.start)) logger.info( { "app": self._app, "bot": self._app.bot, "handlers": self._app.handlers, } ) await self._app.initialize() await self._app.start() return self._app