-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
175 lines (137 loc) · 5.85 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import uvicorn
import logging
from sarufi import Sarufi
from dotenv import load_dotenv
from pymessenger.bot import Bot
from fastapi import FastAPI,Response, Request,BackgroundTasks
from mangum import Mangum
# Load .env file
load_dotenv('.env')
# Initialize Flask App
app = FastAPI()
handler = Mangum(app)
# Make sure all required environment variables are set
if os.getenv("PAGE_ACCESS_TOKEN") is None:
raise ValueError("PAGE_ACCESS_TOKEN not set")
if os.getenv("VERIFY_TOKEN") is None:
raise ValueError("VERIFY_TOKEN not set")
if os.getenv("SARUFI_API_KEY") is None:
raise ValueError("SARUFI_API_KEY not set")
if os.getenv("SARUFI_BOT_ID") is None:
raise ValueError("SARUFI_BOT_ID not set")
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN")
PORT= os.getenv("PORT", 8000)
# facebook messenger object
facebook=Bot(os.getenv("PAGE_ACCESS_TOKEN") ,api_version=16.0)
# sarufi object
sarufi_bot=Sarufi(os.getenv("SARUFI_API_KEY"))
bot=sarufi_bot.get_bot(os.getenv("SARUFI_BOT_ID"))
# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
def execute_actions(actions: dict, sender_id: str):
try:
if actions.get("actions"):
media_actions = [] # store media actions
text_button_actions = [] # text/button actions
for action in actions.get("actions"):
if action.get("send_images") or action.get("send_videos") or action.get("send_documents") or action.get("send_audios"):
media_actions.append(action)
else:
text_button_actions.append(action)
facebook.send_action(sender_id, "typing_on")
# Send media actions first
for media_action in media_actions:
if media_action.get("send_images"):
images = media_action.get("send_images")
elements = [{"title": image.get("caption"),"image_url": image.get("link")} for image in images]
facebook.send_generic_message(sender_id, elements=elements)
elif media_action.get("send_videos"):
videos = media_action.get("send_videos")
for video in videos:
link = video.get("link")
facebook.send_video_url(sender_id, video_url=link)
elif media_action.get("send_documents"):
documents = media_action.get("send_documents")
for document in documents:
link = document.get("link")
facebook.send_file_url(sender_id, file_url=link)
elif media_action.get("send_audios"):
audios = media_action.get("send_audios")
for audio in audios:
link = audio.get("link")
facebook.send_audio_url(sender_id, audio_url=link)
# Send text/button actions
for text_button_action in text_button_actions:
if text_button_action.get("send_message"):
message = text_button_action.get("send_message")
if isinstance(message, list):
message = "\n".join(message)
facebook.send_text_message(message=message, recipient_id=sender_id)
elif text_button_action.get("send_reply_button"):
message = text_button_action["send_reply_button"]["body"]["text"]
buttons = text_button_action["send_reply_button"]["action"]['buttons']
buttons = [{"type": "postback", "title": button["reply"]["title"], "payload": button["reply"]["id"]} for button in buttons]
facebook.send_button_message(text=message, buttons=buttons, recipient_id=sender_id)
elif text_button_action.get("send_button"):
actions = text_button_action.get("send_button")
message = actions.get("body")
buttons = actions["action"]["sections"][0]["rows"]
buttons = [{"content_type": "text", "title": button["title"], "payload": button["id"]} for button in buttons]
message_template = {
"text": message,
"quick_replies": buttons
}
facebook.send_message(recipient_id=sender_id, message=message_template)
else:
logging.info("The message type is not supported by now")
except Exception as error:
logging.error(f"{error} in execute_actions")
def respond(sender_id: str, message: str, message_type: str = "text"):
"""
Send message to user
"""
try:
response =bot.respond(message=message,
chat_id=sender_id,
message_type=message_type,
channel="whatsapp")
return execute_actions(response,sender_id)
except Exception as error:
logging.error(f"{error} in respond")
@app.get("/")
async def webhook_verification(request: Request):
"""
Handle webhook verification from Facebook Messenger
"""
if request.query_params.get("hub.verify_token") == VERIFY_TOKEN:
content=request.query_params.get("hub.challenge")
logging.info("Verified webhook")
return Response(content=content, media_type="text/plain", status_code=200)
logging.error("Webhook Verification failed")
return "Invalid verification token"
@app.post("/")
async def webhook_handler(request: Request,tasks:BackgroundTasks):
"""
Handle webhook events from Facebook Messenger
"""
data = await request.json()
logging.info("Received webhook data: %s", data)
data_received = data['entry'][0]
if data_received.get("messaging"):
data=data_received['messaging'][0]
sender_id = data['sender']['id']
# mark as seen
facebook.send_action(sender_id, "mark_seen")
if data.get("message"):
message=data["message"].get("text")
tasks.add_task(respond,sender_id=sender_id,message=message)
elif data.get("postback"):
message_id=data["postback"]["payload"]
tasks.add_task(respond,
sender_id=sender_id,
message=message_id,
message_type="interactive")
return Response(content="ok",status_code=200)
if __name__ == "__main__":
uvicorn.run("main:app",port=PORT)