From bc970b4ffe4e38b388376b902870f3fc4cf8a247 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 6 Jul 2017 18:52:19 +0000 Subject: [PATCH] huston, we've got texts --- lib/apis.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/apis.js b/lib/apis.js index a81318f..e63673f 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -341,6 +341,10 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { , configurable: false , writable: false , value: function getSiteConfigProp(section) { + // deprecated + if ('com.daplie.tel' === section) { + section = 'tel@daplie.com'; + } return PromiseA.resolve((siteConfig || {})[section]); } }); @@ -494,7 +498,8 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { // Twilio Parameters are often 26 long var bodyParserTwilio = require('body-parser').urlencoded({ limit: '4kb', parameterLimit: 100, extended: false }); - var bodyParserMailgun = require('body-parser').urlencoded({ limit: '100kb', parameterLimit: 500, extended: false }); + // Mailgun has something like 50 parameters + var bodyParserMailgun = require('body-parser').urlencoded({ limit: '1024kb', parameterLimit: 500, extended: false }); function bodyMultiParserMailgun (req, res, next) { var multiparty = require('multiparty'); var form = new multiparty.Form(); @@ -520,13 +525,36 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { }); } + function daplieTel() { + return twilioTel().then(function (twilio) { + function sms(opts) { + // opts = { to, from, body } + return new apiDeps.Promise(function (resolve, reject) { + twilio.sendSms(opts, function (err, resp) { + if (err) { + reject(err); + return; + } + + resolve(resp); + }); + }); + } + + return { + sms: sms + , mms: function () { throw new Error('MMS Not Implemented'); } + }; + }); + } + var caps = { // // Capabilities for APIs // 'mailer@daplie.com': mailgunMail // whichever mailer , 'mailgun@daplie.com': mailgunMail // specifically mailgun - , 'tel@daplie.com': twilioTel // whichever telephony service + , 'tel@daplie.com': daplieTel // whichever telephony service , 'twilio@daplie.com': twilioTel // specifically twilio , 'com.daplie.tel.twilio': twilioTel // deprecated alias @@ -535,15 +563,33 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { // //, 'mailgun.urlencoded@daplie.com': function (req, res, next) { ... } , 'mailgun.parsers@daplie.com': function (req, res, next) { - console.log('mailgun parser req.headers'); - console.log(req.headers); + var chunks = []; + + req.on('data', function (chunk) { + chunks.push(chunk); + }); + req.on('end', function () { + }); function verify() { var body = req.body; var mailconf = siteConfig['mailgun.org']; + if (!body.timestamp) { + console.log('mailgun parser req.headers'); + console.log(req.headers); + chunks.forEach(function (datum) { + console.log('Length:', datum.length); + //console.log(datum.toString('utf8')); + }); + console.log('weird body'); + console.log(body); + } + if (!validateMailgun(mailconf.apiKey, body.timestamp, body.token, body.signature)) { console.error('Request came, but not from Mailgun'); + console.error(req.url); + console.error(req.headers); res.send({ error: { message: 'Invalid signature. Are you even Mailgun?' } }); return; } @@ -552,12 +598,15 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { } if (/urlencoded/.test(req.headers['content-type'])) { + console.log('urlencoded'); bodyParserMailgun(req, res, verify); } else if (/multipart/.test(req.headers['content-type'])) { + console.log('multipart'); bodyMultiParserMailgun(req, res, verify); } else { + console.log('no parser'); next(); } }