-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
167 lines (147 loc) · 5.99 KB
/
app.js
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
const openAiQuery = require("./utils/openAiQuery");
require("dotenv").config(); // load environment variables from .env file
// Access token for your app
// (copy token from DevX getting started page
// and save it as environment variable into the .env file)
const token = process.env.WHATSAPP_TOKEN;
// Imports dependencies and set up http server
const request = require("request"),
express = require("express"),
body_parser = require("body-parser"),
axios = require("axios").default,
app = express().use(body_parser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log("webhook is listening"));
// Health check endpoint
app.get("/", (req, res) => {
res.status(200).send("Hello World!");
})
// Accepts POST requests at /webhook endpoint
app.post("/webhook", async (req, res) => {
// Parse the request body from the POST
let body = req.body;
// Check the Incoming webhook message
console.log(JSON.stringify(req.body, null, 2));
// info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
if (req.body.object) {
if (
req.body.entry &&
req.body.entry[0].changes &&
req.body.entry[0].changes[0] &&
req.body.entry[0].changes[0].value.messages &&
req.body.entry[0].changes[0].value.messages[0]
) {
let phone_number_id =
req.body.entry[0].changes[0].value.metadata.phone_number_id;
let from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
let from_name = req.body.entry[0].changes[0].value.contacts[0].profile.name
let type = req.body.entry[0].changes[0].value.messages[0].type;
if(type === "text") {
let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
let body = "";
if(msg_body === "Hi" || msg_body === "hi" || msg_body === "Hello" || msg_body === "hello") {
body = `Hi, I am your personal assistant. I can help you with your sales call amd sync it to your CRM. Please send me the transcript of the sales call.
Example format:
Company name: Central Perk
Company PoC: Chandler
PoC Role: Creative Head
Sale person name: Abhinav
Role: Business Head
Transcript: What did you guys speak about?
`
} else {
const insights = await openAiQuery(msg_body, 'text');
body = insights ? `
Successfully pushed to CRM. Send *Hi* to start again. Looking forward to help you again. Have a kickass day!
` : "Something went wrong";
}
axios({
method: "POST", // Required, HTTP method, a string, e.g. POST, GET
url:
"https://graph.facebook.com/v12.0/" +
phone_number_id +
"/messages?access_token=" +
token,
data: {
messaging_product: "whatsapp",
to: from,
text: { body: body },
},
headers: { "Content-Type": "application/json" },
});
} else if (type === "audio") {
let mediaId = req.body.entry[0].changes[0].value.messages[0].audio.id;
const transcriptResponse = await axios.post(`http://localhost:8000/get/transcript`, {
media_id: mediaId,
},{
headers: {
"Authorization": "Bearer YWRtaW4="
}
})
const transcript = transcriptResponse.data.transcript;
const insights = await openAiQuery(transcript, 'text');
const body = insights ? `
Successfully pushed to CRM. Send *Hi* to start again. Looking forward to help you again. Have a kickass day!
` : "Something went wrong";
axios({
method: "POST", // Required, HTTP method, a string, e.g. POST, GET
url:
"https://graph.facebook.com/v12.0/" +
phone_number_id +
"/messages?access_token=" +
token,
data: {
messaging_product: "whatsapp",
to: from,
text: { body: body },
},
headers: { "Content-Type": "application/json" },
});
}
else {
axios({
method: "POST", // Required, HTTP method, a string, e.g. POST, GET
url:
"https://graph.facebook.com/v12.0/" +
phone_number_id +
"/messages?access_token=" +
token,
data: {
messaging_product: "whatsapp",
to: from,
text: { body: "Unsupported Format" },
},
headers: { "Content-Type": "application/json" },
});
}
}
res.sendStatus(200);
} else {
// Return a '404 Not Found' if event is not from a WhatsApp API
res.sendStatus(404);
}
});
// Accepts GET requests at the /webhook endpoint. You need this URL to setup webhook initially.
// info on verification request payload: https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
app.get("/webhook", (req, res) => {
/**
* UPDATE YOUR VERIFY TOKEN
*This will be the Verify Token value when you set up webhook
**/
const verify_token = process.env.VERIFY_TOKEN;
// Parse params from the webhook verification request
let mode = req.query["hub.mode"];
let token = req.query["hub.verify_token"];
let challenge = req.query["hub.challenge"];
// Check if a token and mode were sent
if (mode && token) {
// Check the mode and token sent are correct
if (mode === "subscribe" && token === verify_token) {
// Respond with 200 OK and challenge token from the request
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});