Skip to content

Commit

Permalink
Use the request_welcome message type to send the user a welcome message
Browse files Browse the repository at this point in the history
  • Loading branch information
sabaimran committed Dec 22, 2023
1 parent 47c30df commit 062fef1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/flint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
KHOJ_WHATSAPP_PROD = "whatsapp:+18488004242"
KHOJ_WHATSAPP_DEBUG = "whatsapp:+14155238886"
KHOJ_INTRO_MESSAGE = f"""
By the way, I am Khoj, your dedicated personal AI 👋🏽. I can help you with:
Nice to meet you! I am Khoj, your dedicated personal AI 👋🏽. I can help you with:
- 📜 Free-form journaling and note-taking
- 🧠 Answering general knowledge questions
- 💡 Ideating over new ideas
Expand Down
21 changes: 11 additions & 10 deletions src/flint/routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ async def handle_whatsapp_message(body):
khojuser__phone_number=formatted_number
)
user_exists = await user.aexists()
intro_message = False
intro_message = value["messages"][0]["type"] == "request_welcome"

if not user_exists:
user = await User.objects.acreate(username=formatted_number)
user.khojuser.phone_number = formatted_number
await user.asave()
intro_message = True
else:
user = await user.aget()

Expand Down Expand Up @@ -405,6 +405,15 @@ async def response_to_user_whatsapp(message: str, user: User, from_number: str,
}
url = "https://graph.facebook.com/v17.0/" + phone_number_id + "/messages"

# Send Intro Message
if intro_message:
data = make_whatsapp_payload(KHOJ_INTRO_MESSAGE, from_number)
response = requests.post(url, json=data, headers=headers)
asyncio.create_task(
save_conversation(user=user, message="", response=KHOJ_INTRO_MESSAGE, user_message_type="system")
)
return Response(status_code=200)

# Get Conversation History
logger.info(f"Retrieving conversation history for {uuid}")

Expand Down Expand Up @@ -453,12 +462,4 @@ async def response_to_user_whatsapp(message: str, user: User, from_number: str,
response = requests.post(url, json=data, headers=headers)
response.raise_for_status()

# Send Intro Message
if intro_message:
data = make_whatsapp_payload(KHOJ_INTRO_MESSAGE, from_number)
response = requests.post(url, json=data, headers=headers)
asyncio.create_task(
save_conversation(user=user, message="", response=KHOJ_INTRO_MESSAGE, user_message_type="system")
)

return Response(status_code=200)

0 comments on commit 062fef1

Please sign in to comment.