Last active
January 4, 2018 00:49
-
-
Save josephdburdick/66bed95279a0c70b80ca9203bb15d5f7 to your computer and use it in GitHub Desktop.
SMS Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'isomorphic-fetch'; | |
import gql from 'graphql-tag'; | |
import { ApolloClient } from 'apollo-client'; | |
import { InMemoryCache } from 'apollo-cache-inmemory'; | |
import { HttpLink } from 'apollo-link-http'; | |
import url from 'url'; | |
import { normalizeRequest } from './sms-middleware.util'; | |
import log from '../../../common/log'; | |
/** | |
* SMS Middleware | |
* @param {Object} req Incoming Request | |
* @return Twilio Response XML | |
*/ | |
// Prepare new ApolloClient instance | |
const { protocol, hostname, port, pathname } = url.parse(__BACKEND_URL__); | |
const apiUrl = `${protocol}//${hostname}:${process.env.PORT || port}${pathname}`; | |
const client = new ApolloClient({ | |
link: new HttpLink({ uri: apiUrl }), | |
cache: new InMemoryCache() | |
}); | |
export default async (req, res, next) => { | |
try { | |
// Normalize incoming data to match schema | |
req.body = normalizeRequest(req); | |
// Create incoming text message mutation | |
const mutation = gql` | |
mutation handleIncomingTextMessage($input: IncomingTextMessageRequestInput) { | |
handleIncomingTextMessage(input: $input) { | |
status | |
} | |
} | |
`; | |
// Send mutation | |
const response = await client.mutate({ | |
mutation, | |
variables: { | |
input: { | |
...req.body | |
} | |
} | |
}); | |
// Return response XML to Twilio | |
return ` | |
<Response> | |
<Message> | |
${response} | |
</Message> | |
</Response> | |
`; | |
} catch (e) { | |
log(e.message); | |
next(e); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input TextMessageMediaAttachment { | |
contentType: String! | |
url: String! | |
} | |
input IncomingTextMessageRequestInput { | |
sid: String! | |
to: String! | |
from: String! | |
body: String! | |
numMedia: Int! | |
status: String | |
fromCity: String | |
fromState: String | |
fromZip: String | |
fromCountry: String | |
toCity: String | |
toState: String | |
toZip: String | |
toCountry: String | |
accountSid: String | |
mediaAttachmments: [TextMessageMediaAttachment] | |
} | |
type TextMessagePayloadResponse { | |
sid: String! | |
to: String! | |
from: String! | |
body: String! | |
status: String! | |
numMedia: Int! | |
errorCode: Int | |
errorMessage: String | |
dateCreated: Date | |
dateUpdated: Date | |
dateSent: Date | |
numSegments: Int | |
price: Float | |
priceUnit: String | |
} | |
extend type Query { | |
getTextMessage(sid: String!): TextMessagePayloadResponse | |
getTextMessages(limit: Int, pageSize: Int): [TextMessagePayloadResponse] | |
} | |
extend type Mutation { | |
handleIncomingTextMessage( | |
input: IncomingTextMessageRequestInput | |
): TextMessagePayloadResponse | |
sendTextMessage( | |
to: String! | |
from: String | |
body: String! | |
mediaUrl: String | |
): TextMessagePayloadResponse | |
} | |
extend type Subscription { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment