46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
from rest_framework.views import APIView
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from rest_framework import status
|
|
|
|
from django.http import HttpResponse
|
|
from core.models import QueuedMessage, Message
|
|
import requests
|
|
from requests.exceptions import RequestException
|
|
import orjson
|
|
from django.conf import settings
|
|
from core.messaging import natural
|
|
import aiohttp
|
|
from asgiref.sync import sync_to_async
|
|
from core.clients import signalapi
|
|
|
|
|
|
|
|
async def send_message(db_obj):
|
|
recipient_uuid = db_obj.session.identifier.identifier
|
|
text = db_obj.text
|
|
|
|
send = lambda x: signalapi.send_message_raw(recipient_uuid, x) # returns ts
|
|
start_t = lambda: signalapi.start_typing(recipient_uuid)
|
|
stop_t = lambda: signalapi.stop_typing(recipient_uuid)
|
|
|
|
tss = await natural.natural_send_message(
|
|
text,
|
|
send,
|
|
start_t,
|
|
stop_t,
|
|
) # list of ts
|
|
#result = await send_message_raw(recipient_uuid, text)
|
|
await sync_to_async(db_obj.delete)()
|
|
result = [x for x in tss if x] # all trueish ts
|
|
if result: # if at least one message was sent
|
|
ts1 = result.pop() # pick a time
|
|
await sync_to_async(Message.objects.create)(
|
|
user=db_obj.session.user,
|
|
session=db_obj.session,
|
|
custom_author="BOT",
|
|
text=text,
|
|
ts=ts1, # use that time in db
|
|
)
|
|
|