-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (58 loc) · 1.84 KB
/
server.js
File metadata and controls
74 lines (58 loc) · 1.84 KB
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
import express from "express";
import twilio from "twilio";
import dotenv from "dotenv";
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const PORT = process.env.PORT || 3000;
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
app.get("/", (req, res) => {
res.send("Server is running fine!");
});
app.post("/phone-call", async (req, res) => {
client.calls
.create({
url: "https://027d-154-80-54-106.ngrok-free.app/voice-response",
to: req.body.to,
from: process.env.TWILIO_PHONE_NUMBER,
})
.then((call) => console.log(`Call made: ${call.sid}`))
.catch((err) => res.status(500).send(err));
});
app.post("/voice-response", (req, res) => {
const twiml = new twilio.twiml.VoiceResponse();
const gather = twiml.gather({
numDigits: 1,
timeout: 5,
action: "https://027d-154-80-54-106.ngrok-free.app/handle-key",
method: "POST",
});
gather.say(
"Hello! This is Ali Zulfiqar. Did you place an order recently? Please press 1 for Yes, 2 for No."
);
twiml.say("We didn't receive any input. Goodbye!");
res.type("text/xml");
res.send(twiml.toString());
});
app.post("/handle-key", (req, res) => {
const digit = req.body.Digits;
const twiml = new twilio.twiml.VoiceResponse();
if (digit === "1") {
// Update DB for confirmation
twiml.say("Thank you. Your order is confirmed!");
} else if (digit === "2") {
// Update DB for rejection
twiml.say("Thank you. We have noted that you did not place the order.");
} else {
twiml.say("Invalid input. Goodbye!");
}
res.type("text/xml");
res.send(twiml.toString());
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});