Spaces:
Sleeping
Sleeping
File size: 1,539 Bytes
220afd8 7d02c05 220afd8 1f3c6b2 a120bed 7d02c05 338aa9d 5b6ca23 bca2aec a46ecf2 94fc04c e417777 a46ecf2 94fc04c c5c301b 94fc04c a46ecf2 7e0240e 448b36e 1f3c6b2 ef1b7ba 30c52d9 c4e7ed8 5b6ca23 2c84cbb 211d12e 5b6ca23 a120bed 33134c0 a120bed 211d12e a120bed 211d12e 448b36e 82726c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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"}
|