Spaces:
Sleeping
Sleeping
import json | |
from kb import getkb, get_response | |
from dotenv import load_dotenv | |
from whatsapp_client import WhatsAppWrapper | |
load_dotenv() | |
data = """{ | |
"object": "whatsapp_business_account", | |
"entry": [ | |
{ | |
"id": "270451259495375", | |
"changes": [ | |
{ | |
"value": { | |
"messaging_product": "whatsapp", | |
"metadata": { | |
"display_phone_number": "15556024077", | |
"phone_number_id": "297527496785471" | |
}, | |
"contacts": [ | |
{ | |
"profile": { | |
"name": "Eric Landry" | |
}, | |
"wa_id": "15127404620" | |
} | |
], | |
"messages": [ | |
{ | |
"from": "15127404620", | |
"id": "wamid.HBgLMTUxMjc0MDQ2MjAVAgASGBQzQTEyQTM0OURGQUI4QzZDNTg2MAA=", | |
"timestamp": "1716933996", | |
"text": { | |
"body": "how do i get tested for prostate cancer?" | |
}, | |
"type": "text" | |
} | |
] | |
}, | |
"field": "messages" | |
} | |
] | |
} | |
] | |
}""" | |
data = json.loads(data) | |
value = data['entry'][0]['changes'][0]['value'] | |
def main(): | |
vectorstore = getkb() | |
if 'messages' in value and 'from' in value['messages'][0] and 'text' in value['messages'][0]: | |
message = value['messages'][0] | |
name = value['contacts'][0]['profile']['name'] | |
phone_num = message['from'] | |
mesg_body = message['text']['body'] | |
context = '\n'.join([doc[0].page_content for doc in vectorstore.similarity_search_with_score(mesg_body, k=6)]) | |
response = get_response(mesg_body, context, name) | |
print(f"RESPONSE: {response}") | |
client = WhatsAppWrapper() | |
client.send_text_message("hello, right back at ya ..." + response, phone_num) | |
if __name__ == "__main__": | |
main() |