-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (75 loc) · 2.34 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
require("dotenv").config();
const app = require("express")();
const bodyParser = require("body-parser");
const nedb = require("nedb");
const Vonage = require("@vonage/server-sdk");
const moment = require("moment");
// tell application to use body-parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// initialise nedb
const msgDB = new nedb({ filename: "messages.db", autoload: true });
// initialise vonage
const vonage = new Vonage(
{
apiKey: process.env.VONAGE_KEY,
apiSecret: process.env.VONAGE_SECRET,
applicationId: process.env.VONAGE_FACEBOOK_ID,
privateKey: "./private.key",
},
{ apiHost: "https://messages-sandbox.nexmo.com" }
);
function sendMessage(sender, recipient, text) {
const to = { type: "messenger", id: recipient };
const from = { type: "messenger", id: sender };
const message = { content: { type: "text", text: text } };
vonage.channel.send(to, from, message, function (error, success) {
if (error) {
console.error(error);
}
console.log("sendSuccess :>> ", success);
});
}
// setup inbound and status endpoints
app.post("/inbound", function (request, response) {
if (request.body.message.content.text.toLowerCase().trim() === "recap") {
// get all messages in DB and reply to user
msgDB.find({ "from.id": request.body.from.id }, function (error, records) {
if (error) {
console.error(error);
}
const messages = records
.map(
(record) =>
`${moment(record.timestamp).format("DDMMMYY HH:mm:ss")}: ${
record.message.content.text
}`
)
.join("\n\n");
sendMessage(request.body.to.id, request.body.from.id, messages);
});
} else {
msgDB.insert(request.body, function (error, record) {
if (error) {
sendMessage(
request.body.to.id,
request.body.from.id,
"Something went wrong. Please try again later."
);
return console.error(error);
}
sendMessage(
record.to.id,
record.from.id,
`Message "${record.message.content.text}" received. Thank you!`
);
});
}
response.send("ok");
});
app.post("/status", function (request, response) {
console.log("status :>> ", request.body);
response.send("ok");
});
// port number should match ngrok's
app.listen(3000);