-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessaging.js
More file actions
86 lines (66 loc) · 2.44 KB
/
messaging.js
File metadata and controls
86 lines (66 loc) · 2.44 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
75
76
77
78
79
80
81
82
83
84
85
86
require("dotenv").config();
const ngrok_domain = process.env.NGROK_DOMAIN;
const bodyParser = require("body-parser");
const express = require("express");
const { MessagingResponse } = require("twilio").twiml;
const VoiceResponse = require("twilio").twiml.VoiceResponse;
const { connectToDB } = require("./databaseProvider");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/sms", async (req, res) => {
const twiml = new MessagingResponse();
const userMessage = req.body.Body;
//TODO: We have the user message for a peer review of the chosen employee, write it to the database.
const document = {
target: "Tim",
response: userMessage,
};
const client = await connectToDB();
const collection = await client.collection("surveys");
collection.insertOne(document, function (err, res) {
if (err) throw err;
console.log("1 document inserted");
client.close();
});
twiml.message(
"Thank you for letting me know. Remember, you can always reach out to these resources if you are ever struggling and need someone to talk to. https://www.betterhelp.com/"
);
res.type("text/xml").send(twiml.toString());
});
// Returns TwiML which prompts the caller to record a message
app.post("/record", (request, response) => {
// Use the Twilio Node.js SDK to build an XML response
const twiml = new VoiceResponse();
twiml.say(
{ voice: "Polly.Amy" },
"Hello, we wanted to check in on you to see how you are doing. Is there anything you want to talk about?"
);
twiml.record({
maxLength: 30,
});
// End the call with <Hangup>
twiml.hangup();
// Render the response as XML in reply to the webhook request
response.type("text/xml");
response.send(twiml.toString());
});
// Returns TwiML which prompts the caller to record a message
app.post("/transcript", async (request, response) => {
console.log("Transcript received");
const transcription =
"I have been really stressed out lately and it is affecting my mental health. I am not sure what to do about it.";
const document = {
target: "Farhan",
voice_response: transcription,
};
const client = await connectToDB();
const collection = await client.collection("voice_transcripts");
collection.insertOne(document, function (err, res) {
if (err) throw err;
console.log("1 document inserted");
client.close();
});
});
app.listen(3000, () => {
console.log("Express server listening on port 3000");
});