BOD-14 Backend server work + sending of messages WIP for front end
This commit is contained in:
41
server/sms/send.js
Normal file
41
server/sms/send.js
Normal file
@@ -0,0 +1,41 @@
|
||||
require("dotenv").config();
|
||||
const twilio = require("twilio");
|
||||
const phone = require("phone");
|
||||
|
||||
const client = twilio(
|
||||
process.env.TWILIO_AUTH_TOKEN,
|
||||
process.env.TWILIO_AUTH_KEY
|
||||
);
|
||||
|
||||
exports.send = (req, res) => {
|
||||
console.log("Sending an SMS!");
|
||||
const { to, messagingServiceSid, body, conversationid } = req.body;
|
||||
|
||||
if (!!to && !!messagingServiceSid && !!body && !!conversationid) {
|
||||
client.messages
|
||||
.create({
|
||||
body: body,
|
||||
messagingServiceSid: messagingServiceSid,
|
||||
to: phone(to)[0]
|
||||
})
|
||||
.then(message => {
|
||||
let newMessage = {
|
||||
msid: message.sid,
|
||||
text: body,
|
||||
conversationid,
|
||||
isoutbound: true
|
||||
};
|
||||
client
|
||||
.request(queries.INSERT_MESSAGE, { msg: newMessage })
|
||||
.then(r2 => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch(e2 => {
|
||||
res.json({ success: false, message: e2 });
|
||||
});
|
||||
})
|
||||
.catch(error => res.json({ success: false, message: error }));
|
||||
} else {
|
||||
res.json({ success: false, message: "Missing required parameter(s)." });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user