-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (98 loc) · 2.62 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { config } from 'dotenv';
if (process.env.NODE_ENV !== 'production') {
config();
}
import twilio, { TwimlResponse } from 'twilio';
import express from 'express';
import { json, urlencoded } from 'body-parser';
const {
TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER, PORT
} = process.env;
const client = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
const app = express();
app.use(urlencoded({ extended: true }));
app.use(json());
// enable CORS
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
// TODO just use mongo?
class MessageList {
messages = {};
id = 1;
save(data) {
// save some data about sent message in-memory
const message = {
id: this.id,
to: data.to || data.To,
from: data.from || data.From,
body: data.body || data.Body,
sent: data.dateCreated || new Date().toJSON()
}
// increment id so the front end has a uid
this.id ++;
const key = (message.to === TWILIO_PHONE_NUMBER) ?
message.from : message.to;
if (this.messages[key]) {
this.messages[key].push(message);
} else {
this.messages[key] = [ message ];
}
}
list(number) {
number = number[0] === '+' ? number : `+${number}`;
return this.messages[number] || [];
}
listAll() {
return this.messages;
}
}
const messageList = new MessageList();
// + is interpretted as space by browsers so omit this character
// we will add it automatically
app.post('/send/:number', (req, res) => {
// TODO also validate number sent
if (!req.body.message) {
return res
.status(400)
.send('ERROR: No message body found')
}
const toNumber = `+${req.params.number}`;
const msg = client.messages.create({
to: toNumber,
from: TWILIO_PHONE_NUMBER,
body: req.body.message
}, (err, message) => {
if (!err) {
messageList.save(message);
res
.status(201)
.send(message);
} else {
console.log('ERROR: Twilio message failed to send.');
console.log(err.message);
res
.status(400)
.send(err);
}
});
});
app.post('/reply', (req, res) => {
messageList.save(req.body);
res.status(201).send('OK');
});
app.get('/messages/', (req, res) => {
res.status(200).send(
messageList.listAll());
});
app.get('/messages/:number', (req, res) => {
res.status(200).send(
messageList.list(`+${req.params.number}`));
});
const port = PORT || 8080;
app.listen(
port,
() => console.log(`server running at http://localhost:${port}`)
);