-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (41 loc) · 1.67 KB
/
Copy pathindex.js
File metadata and controls
59 lines (41 loc) · 1.67 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
var bodyParser = require('body-parser');
const express = require('express');
const cors = require('cors');
const authRoutes = require("./routes/auth.js");
const app = express();
const PORT = process.env.PORT || 5000;
require('dotenv').config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const messagingServiceSid = process.env.TWILIO_MESSAGING_SERVICE_SID;
const twilioClient = require('twilio')(accountSid, authToken);
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: true }));
// server responses on client requests
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.post('/', (req, res) => {
const { message, user: sender, type, members } = req.body;
if (type === 'message.new') {
members
.filter((member) => member.user_id != sender.id)
.forEach(({ user }) => {
if (!user.online) {
twilioClient.messages.create({
body: 'You have a new message from ${message.user.fullname} - ${message.text}',
messagingServiceSid: messagingServiceSid,
to: user.phoneNumber
})
.then(() => console.log('Message sent!'))
.catch((err) => console.log(err))
}
})
return res.status(200).send('Message sent!');
}
return res.status(200).send('Not a new message request');
});
app.use('/auth', authRoutes);
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));