forked from josancamon19/meta-glasses-gemini
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
103 lines (85 loc) · 3.74 KB
/
main.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import PlainTextResponse
from functionality.calendar import create_google_calendar_event
from functionality.nutrition import get_cals_from_image
from functionality.image import logic_for_prompt_before_image, retrieve_calories_from_image
from functionality.notion_ import *
from functionality.search import *
from functionality.automation import *
from functionality.audio import retrieve_transcript_from_audio
from utils.whatsapp import send_whatsapp_threaded
from utils.gemini import *
app = FastAPI()
ok = {'status': 'Ok'}
app.add_middleware(
CORSMiddleware,
allow_origins=["https://www.messenger.com", "https://www.facebook.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/')
def home():
return 'API v1.0 OK'
@app.get('/webhook', response_class=PlainTextResponse)
def webhook_verification(request: Request):
if request.query_params.get('hub.mode') == 'subscribe' and request.query_params.get(
'hub.verify_token') == os.getenv('WHATSAPP_WEBHOOK_VERIFICATION_TOKEN'):
return request.query_params.get('hub.challenge')
else:
raise HTTPException(status_code=400, detail="Bad Request")
@app.post('/webhook')
def receive_whatsapp_message(request: Request, data: dict):
# TODO: webhook authentication handling
message = data['entry'][0]['changes'][0]['value'].get('messages', [{}])[0]
threading.Thread(target=logic, args=(message,)).start()
return ok
def logic(message: dict):
if not message:
return ok
# message['id'] # message id to redis set, if in set, skip
# e.g. wamid.HBgMNTczNTA0MzQyMjYyFQIAEhgUM0EzNzk5MzY2MzBGQ0Q4NzJDQzgA
if message['type'] == 'image':
return logic_for_prompt_before_image(message)
if message['type'] == 'audio':
return retrieve_transcript_from_audio(message)
# if message['type'] == 'audio':
# path = download_file(message['audio'])
# text = retrieve_transcript_from_audio(path) # TBI
# else:
text = message['text']['body']
if text.lower().strip() == 'cals': # special keyword for calories tracking
return retrieve_calories_from_image()
# Get last two words of message if "food log" match.
isfoodlog:str = ' '.join(text.lower().strip().split()[-2:])
if (isfoodlog in['food log','foodlog','food log.','my diet','my diet.']):
return get_cals_from_image()
operation_type = retrieve_message_type_from_message(text)
print('operation_type', operation_type, len(operation_type))
if operation_type == 'calendar':
args = determine_calendar_event_inputs(text)
args['color_id'] = 9 if args['type'] == 'reminder' and args['duration'] == 0.5 else 0
del args['type'] # Faking the "reminders" in google cal, using a similar color, and default time
create_google_calendar_event(**args)
send_whatsapp_threaded('Event created successfully!')
return ok
elif operation_type == 'notion':
arguments = determine_notion_page_inputs(text)
add_new_page(**arguments)
send_whatsapp_threaded('Notion page created successfully!')
return ok
elif operation_type == 'search':
response = google_search_pipeline(text)
send_whatsapp_threaded(response)
return ok
elif operation_type == 'image':
return logic_for_prompt_before_image(message)
elif operation_type == 'automation':
response = automation_command(text)
send_whatsapp_threaded(response)
return ok
else:
response = simple_prompt_request(text + '. Respond in 10 to 15 words.')
send_whatsapp_threaded(response)
return ok