from fastapi import FastAPI, Request, Query import logging from whatsapp_client import WhatsAppWrapper import json from kb import getkb, get_response logger = logging.getLogger() app = FastAPI() vectorstore = getkb() @app.get('/') def ok(): return 'all is good!' @app.get("/webook") async def verify( verify_token: str = Query(alias="hub.verify_token"), mode: str = Query(alias="hub.mode"), challenge: str = Query(alias="hub.challenge"), ): print(f"verify_token: {verify_token}") print(f"mode: {mode}") print(f"challenge: {challenge}") if verify_token == "jericlandry": print('good challenge') return int(challenge) else: print('bad challenge!!!') return "bad challenge" @app.post("/webook") async def callback(request: Request): body = await request.body() data = json.loads(body) value = data['entry'][0]['changes'][0]['value'] if 'messages' in value and 'from' in value['messages'][0] and 'text' in value['messages'][0]: message = value['messages'][0] phone_num = message['from'] mesg_body = message['text']['body'] name = value['contacts'][0]['profile']['name'] print("SEARCH: " + str(mesg_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) client = WhatsAppWrapper() client.send_text_message(response, phone_num) return {"status": "success"}