-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
104 lines (82 loc) · 2.28 KB
/
index.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
require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.port || 3000;
const twilioClient = require('twilio')(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN,
);
const dialogflow = require('@google-cloud/dialogflow');
const sessionClient = new dialogflow.SessionsClient();
// twilio sends application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }))
app.use(express.json());
app.post('/whatsapp', async function(req, res) {
const from = req.body.From;
const to = req.body.To;
const body = req.body.Body;
// session for current user
const projectId = await sessionClient.getProjectId();
const sessionPath = sessionClient.projectAgentSessionPath(projectId, from);
// query dialogflow
const response = await sessionClient.detectIntent({
session: sessionPath,
queryInput: {
text: {
text: body,
languageCode: 'en-US',
}
}
});
// handle emi due date action
if(response[0].queryResult.action == 'emi.due-date') {
let dueDate = new Date();
dueDate.setTime(dueDate.getTime() + 5*24*60*60*1000);
let dueAmount = "$200";
await twilioClient.messages.create({
from: to,
to: from,
body: `Your next emi of ${dueAmount} is on ${dueDate.toDateString()}.`
});
res.status(200).end();
return
}
// relay message sent by dialogflow directly to user
// message can be in two formats
// 1) Normal String response
// 2) Json Object with url and text properies
// url: publically accessible url of image
// text: additional text
const messages = response[0].queryResult.fulfillmentMessages;
for (const message of messages) {
// normal text message
if(message.text) {
await twilioClient.messages.create({
from: to,
to: from,
body: message.text.text[0],
});
}
// response payload
if(message.payload) {
let url = '';
let text = '';
if(message.payload.fields.media_url) {
url = message.payload.fields.media_url.stringValue;
}
if(messages.payload.fields.text) {
text = message.payload.fields.text.stringValue
}
await twilioClient.messages.create({
from: to,
to: from,
body: text,
mediaUrl: url,
});
}
}
res.status(200).end();
});
app.listen(PORT, () => {
console.log(`Listening on ${PORT}`);
});