BOD-14 Backend server work + sending of messages WIP for front end
This commit is contained in:
9
server/graphql-client/graphql-client.js
Normal file
9
server/graphql-client/graphql-client.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const GraphQLClient = require("graphql-request").GraphQLClient;
|
||||
require("dotenv").config();
|
||||
|
||||
//TODO May need to use a different client that includes caching of resources.
|
||||
exports.client = new GraphQLClient(process.env.GRAPHQL_ENDPOINT, {
|
||||
headers: {
|
||||
"x-hasura-admin-secret": process.env.HASURA_ADMIN_SECRET
|
||||
}
|
||||
});
|
||||
23
server/graphql-client/queries.js
Normal file
23
server/graphql-client/queries.js
Normal file
@@ -0,0 +1,23 @@
|
||||
exports.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID = `
|
||||
query FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID(
|
||||
$mssid: String!
|
||||
$phone: String!
|
||||
) {
|
||||
bodyshops(where: { messagingservicesid: { _eq: $mssid } }) {
|
||||
id
|
||||
conversations(where: { phone_num: { _eq: $phone } }) {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
exports.INSERT_MESSAGE = `
|
||||
mutation INSERT_MESSAGE($msg: [messages_insert_input!]!) {
|
||||
insert_messages(objects: $msg) {
|
||||
returning {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
82
server/sms/receive.js
Normal file
82
server/sms/receive.js
Normal file
@@ -0,0 +1,82 @@
|
||||
require("dotenv").config();
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const queries = require("../graphql-client/queries");
|
||||
const phone = require("phone");
|
||||
|
||||
exports.receive = (req, res) => {
|
||||
//Perform request validation
|
||||
if (!req.body || !req.body.MessagingServiceSid || !req.body.SmsMessageSid) {
|
||||
res.sendStatus(400);
|
||||
} else {
|
||||
client
|
||||
.request(queries.FIND_BODYSHOP_BY_MESSAGING_SERVICE_SID, {
|
||||
mssid: req.body.MessagingServiceSid,
|
||||
phone: phone(req.body.From)[0]
|
||||
})
|
||||
.then(response => {
|
||||
//TODO Add logic for handling MMS.
|
||||
let newMessage = { msid: req.body.SmsMessageSid, text: req.body.Body };
|
||||
if (response.bodyshops[0]) {
|
||||
//Found a bodyshop - should always happen.
|
||||
if (response.bodyshops[0].conversations.length === 0) {
|
||||
//No conversation Found, create one.
|
||||
console.log("[SMS Receive] No conversation found. Creating one.");
|
||||
newMessage.conversation = {
|
||||
data: {
|
||||
bodyshopid: response.bodyshops[0].id,
|
||||
phone_num: phone(req.body.From)[0]
|
||||
}
|
||||
};
|
||||
} else if (response.bodyshops[0].conversations.length === 1) {
|
||||
//Just add it to the conversation
|
||||
console.log("[SMS Receive] Conversation found. Added ID.");
|
||||
newMessage.conversationid =
|
||||
response.bodyshops[0].conversations[0].id;
|
||||
} else {
|
||||
//We should never get here.
|
||||
console.log(
|
||||
"Massive Error: Duplicate Phone Numbers for MSSID: " +
|
||||
req.body.MessagingServiceSid
|
||||
);
|
||||
}
|
||||
|
||||
client
|
||||
.request(queries.INSERT_MESSAGE, { msg: newMessage })
|
||||
.then(r2 => {
|
||||
res.sendStatus(200);
|
||||
})
|
||||
.catch(e2 => {
|
||||
console.log("e2", e2);
|
||||
res.json(e2);
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(e1 => {
|
||||
console.log("e1", e1);
|
||||
res.json(e1);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// const sampleMessage: {
|
||||
// "ToCountry": "CA",
|
||||
// "ToState": "BC",
|
||||
// "SmsMessageSid": "SMad7bddaf3454c0904999d6018b1e8f49",
|
||||
// "NumMedia": "0",
|
||||
// "ToCity": "Vancouver",
|
||||
// "FromZip": "",
|
||||
// "SmsSid": "SMad7bddaf3454c0904999d6018b1e8f49",
|
||||
// "FromState": "BC",
|
||||
// "SmsStatus": "received",
|
||||
// "FromCity": "VANCOUVER",
|
||||
// "Body": "Hi",
|
||||
// "FromCountry": "CA",
|
||||
// "To": "+16043301606",
|
||||
// "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
|
||||
// "ToZip": "",
|
||||
// "NumSegments": "1",
|
||||
// "MessageSid": "SMad7bddaf3454c0904999d6018b1e8f49",
|
||||
// "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
|
||||
// "From": "+16049992002",
|
||||
// "ApiVersion": "2010-04-01"
|
||||
// }
|
||||
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)." });
|
||||
}
|
||||
};
|
||||
23
server/sms/status.js
Normal file
23
server/sms/status.js
Normal file
@@ -0,0 +1,23 @@
|
||||
require("dotenv").config();
|
||||
const client = require("../graphql-client/graphql-client").client;
|
||||
const queries = require("../graphql-client/queries");
|
||||
const phone = require("phone");
|
||||
|
||||
exports.status = (req, res) => {
|
||||
//Perform request validation
|
||||
console.log("Inbound Status Update: ", JSON.stringify(req.body));
|
||||
res.sendStatus(200);
|
||||
};
|
||||
|
||||
// Inbound Sample
|
||||
// {
|
||||
// "SmsSid": "SM5205ea340e06437799d9345e7283457c",
|
||||
// "SmsStatus": "queued",
|
||||
// "MessageStatus": "queued",
|
||||
// "To": "+16049992002",
|
||||
// "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
|
||||
// "MessageSid": "SM5205ea340e06437799d9345e7283457c",
|
||||
// "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
|
||||
// "From": "+16043301606",
|
||||
// "ApiVersion": "2010-04-01"
|
||||
// }
|
||||
Reference in New Issue
Block a user