djano python

This commit is contained in:
Kseninia Mikhaylova 2024-06-04 13:44:22 +03:00
parent c4dac25528
commit c143650bdd
1 changed files with 48 additions and 17 deletions

View File

@ -2,7 +2,8 @@ import json
from django.conf import settings
from telegram import Update
from telegram.ext import Application, CommandHandler
from telegram.ext import Application, CommandHandler, MessageHandler, filters
from telegram.constants import ParseMode, ChatType
from asgiref.sync import async_to_sync
@ -17,10 +18,51 @@ import logging
logger = logging.getLogger("root")
async def start(update, context):
logger.info(update)
await update.message.reply_html(text="123")
class ItemViewSet(viewsets.ViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
@async_to_sync
async def create(self, request):
req = json.loads(request.body)
logger.info(f"que len before put {ptb_application.update_queue.qsize()}")
update_item = Update.de_json(data=req, bot=ptb_application.bot)
await ptb_application.update_queue.put(update_item)
logger.info(f"que len after put {ptb_application.update_queue.qsize()}")
# await self.ptb_application.bot.send_message(
# chat_id=req["message"]["from"]["id"],
# text=f'Вы прислали текст `{req["message"]["text"]}`',
# parse_mode=ParseMode.MARKDOWN_V2,
# reply_to_message_id=req["message"]["message_id"],
# )
# if req["message"]["chat"]["type"] != ChatType.PRIVATE:
# return Response()
# if req["message"]["text"] == "/add":
# await self.ptb_application.bot.send_message(
# chat_id=req["message"]["from"]["id"],
# text=f'Вы хотите создать новую инвентаризацию?',
# parse_mode=ParseMode.MARKDOWN_V2,
# reply_to_message_id=req["message"]["message_id"],
# )
return Response({"test": "create"})
async def init_tg():
ptb_application = (
Application.builder().token(settings.TGBOT["token"]).updater(None).build()
Application.builder().token(settings.TGBOT["token"]).concurrent_updates(True).updater(None).build()
)
ptb_application.add_handler(CommandHandler("start", start))
ptb_application.add_handler(
MessageHandler(filters.ChatType.PRIVATE, callback=start)
)
logger.info(
{
@ -29,19 +71,8 @@ async def init_tg():
"handlers": ptb_application.handlers,
}
)
await ptb_application.initialize()
await ptb_application.start()
return ptb_application
class ItemViewSet(viewsets.ViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
def __init__(self, **kwargs) -> None:
self.ptb_application = init_tg()
super().__init__(**kwargs)
@async_to_sync
async def create(self, request):
req = json.loads(request.body)
await self.ptb_application.bot.send_message(chat_id=req["message"]["from"]["id"], text='123')
return Response({"test": "create"})
ptb_application = async_to_sync(init_tg)()