Skip to content

Commit

Permalink
Fix issue with notes being unimplemented. Use background tasks for sc…
Browse files Browse the repository at this point in the history
…heduling whatsapp response

- Need to use backgroundtasks because the webhook expects a response within a few seconds. If not delivered, it will keep resending until it's acked. This is problematic for queries taht take a longer response time. Hence, use bg tasks to schedule the work after returning a response to the API
  • Loading branch information
sabaimran committed Jan 24, 2024
1 parent 26d9391 commit 64dce37
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/flint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}

UNIMPLEMENTED_COMMANDS = {
"/notes": "/notes",
"/speak": "/speak",
}


Expand Down
10 changes: 4 additions & 6 deletions src/flint/routers/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Standard Packages
import asyncio
import logging
import os
import requests
Expand All @@ -9,7 +8,7 @@
from typing import Optional

# External Packages
from fastapi import APIRouter, status, Request
from fastapi import APIRouter, status, Request, BackgroundTasks
from fastapi.responses import Response
from fastapi.params import Form
from fastapi import Body
Expand All @@ -27,9 +26,6 @@
KHOJ_FAILED_AUDIO_TRANSCRIPTION_MESSAGE,
)

# Keep Django module import here to avoid import ordering errors


# Initialize Router
api = APIRouter()
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -84,9 +80,11 @@ async def whatsapp_chat(
@api.post("/whatsapp_chat", status_code=status.HTTP_200_OK)
async def whatsapp_chat_post(
request: Request,
background_tasks: BackgroundTasks,
body=Body(...),
):
asyncio.create_task(handle_message(body=body))
background_tasks.add_task(handle_message, body)
return


def verified_body(body):
Expand Down

0 comments on commit 64dce37

Please sign in to comment.