From f246cf0b93fe04efc35237259a91bba6d2190553 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Fri, 4 Aug 2017 11:45:33 -0600 Subject: [PATCH] added code to support GetResponse.com as a contacts/lists manager --- lib/apis.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 58 insertions(+) diff --git a/lib/apis.js b/lib/apis.js index 7229fb2..834c649 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -3,6 +3,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { var PromiseA = apiDeps.Promise; var mkdirpAsync = PromiseA.promisify(require('mkdirp')); + var request = PromiseA.promisify(require('request')); //var express = require('express'); var express = require('express-lazy'); var fs = PromiseA.promisifyAll(require('fs')); @@ -320,6 +321,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { var _mandrill; var _mailchimp; var _twilio; + var _get_response; myApp.use('/', function preHandler(req, res, next) { return getSiteConfig(clientUrih).then(function (siteConfig) { Object.defineProperty(req, 'getSiteMailer', { @@ -435,6 +437,51 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { } }); + Object.defineProperty(req, 'GetResponse', { + enumerable: true + , configurable: false + , get: function () { + if (_get_response) { + return _get_response; + } + _get_response = { + saveSubscriber: function (email, opts) { + var config = siteConfig['getresponse@daplie.com']; + var customFields = []; + Object.keys(config.customFields).forEach(function (name) { + if (typeof opts[name] !== 'undefined') { + customFields.push({ + customFieldId: config.customFields[name] + , value: [ String(opts[name]) ] + }); + } + }); + + return request({ + method: 'POST' + , url: 'https://api.getresponse.com/v3/contacts' + , headers: { 'X-Auth-Token': 'api-key ' + config.apiKey } + , json: true + , body: { + name: opts.name + , email: email + , ipAddress: opts.ipAddress + , campaign: { campaignId: config.campaignId } + , customFieldValues: customFields + } + }).then(function (resp) { + if (resp.statusCode === 202) { + return; + } + return PromiseA.reject(resp.body.message); + }); + } + }; + + return _get_response; + } + }); + var Twilio = require('twilio'); function twilioTel(/*opts*/) { if (_twilio) { @@ -496,6 +543,9 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { function mailgunMail(/*opts*/) { return apiDeps.Promise.resolve(req.getSiteMailer()); } + function getResponseList() { + return apiDeps.Promise.resolve(req.GetResponse); + } // Twilio Parameters are often 26 long var bodyParserTwilio = require('body-parser').urlencoded({ limit: '4kb', parameterLimit: 100, extended: false }); @@ -560,6 +610,7 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { , 'twilio@daplie.com': twilioTel // specifically twilio , 'com.daplie.tel.twilio': twilioTel // deprecated alias + , 'getresponse@daplie.com': getResponseList // // Webhook Parsers // @@ -642,6 +693,12 @@ module.exports.create = function (xconfx, apiFactories, apiDeps) { if (caps[capname]) { return caps[capname](opts, b, c); } + if (siteConfig[capname]) { + var service = siteConfig[capname].service || siteConfig[capname]; + if (caps[service]) { + return caps[service](opts, b, c); + } + } return apiDeps.Promise.reject( new Error("['" + req.clientApiUri + '/' + pkgId + "'] " + "capability '" + capname + "' not implemented") diff --git a/package.json b/package.json index 487aab0..348535a 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "nodemailer": "^1.4.0", "nodemailer-mailgun-transport": "1.x", "oauthcommon": "git+https://git.daplie.com/node/oauthcommon.git", + "request": "^2.81.0", "serve-static": "1.x", "sqlite3-cluster": "git+https://git.daplie.com/coolaj86/sqlite3-cluster.git#v2", "stripe": "^4.22.0",