Apifying Responsys for Sending Communications (email, SMS, push) to your Customers


Intro

The idea is apify Responsys REST services and create a couple of high level rest endpoints that can be consumed from anywhere after exposing them in a container. In our use case we are utilising Application Container Cloud Service and Node.js for coding and exposing the rest endpoints respectively.

NOTE: Create an ACCS container with at least Basic Authentication. See this entry blog for more info.

Ingredients

  • A Responsys instance in with you create email and other type of templates, a couple of campaigns and things like that.
  • An ACCS container

The source code

In bold you should put your environment data.

// jmu, july 2018

var express = require('express');

var app = express();

var https = require('https');

var querystring = require('querystring');

const user = "yourresponsysuser"

const password = "yourpassword"

const responsys_token_endpoint = 'login2.responsys.net'

var credentials = { "auth_type": "password",

"user_name": user,

"password": password}

//

var token_options = {

host: responsys_token_endpoint,

port: 443,

path: '/rest/api/v1.3/auth/token',

method: 'POST',

headers: {

"Content-Type": "application/x-www-form-urlencoded"

}

};

var token = ""

// destinatario=xxx, campania=yyyy

app.get('/sendMail', function (req, res) {

let data ='';

var destinatario = req.param("destinatario", "defaultemail")

console.log(destinatario)

var elcuerpo = {

"recipientData" : [{

"recipient" : {

"customerId" : null,

"emailAddress" : destinatario,

"listName" : {

"folderName" : "AGE",

"objectName" : "AGE_List"

},

"recipientId" : null,

"mobileNumber" : null,

"emailFormat" : "HTML_FORMAT"

}

}]

}

var x = https.request(token_options,function(res){

res.on('data', (chunk) => {

data += chunk;

});

res.on('end',function(){

token = JSON.parse(data).authToken;

console.log(token);

responsys_endpoint= JSON.parse(data).endPoint.replace('https://','');

console.log(responsys_endpoint);

var campania=req.param("campania", "CASrecibo")

var restpath ='/rest/api/v1.3/campaigns/'+ campania +'/email'

console.log(restpath);

var email_campaign = {

host: responsys_endpoint,

port: 443,

path: restpath,

method: 'POST',

headers: {

"Authorization": token,

"Content-Type": "application/json"

}

};

var data1=''

var y = https.request(email_campaign,function(res){

res.on('data', (chunk) => {

data1 += chunk;

});

res.on('end',function(){

console.log(data1)

});

});

y.write(JSON.stringify(elcuerpo));

y.end();

});

});

x.write(querystring.stringify(credentials));

x.end();

res.send('');

});

// device=xxx, campania=yyyy

app.get('/sendPush', function (req, res) {

let data ='';

var device = req.param("device", "A-DEVICE-ID")

console.log(device)

var elcuerpo = {

"recipientData": [{

"customerId": null,

"emailAddress": null,

"recipientId": null,

"mobileNumber": null,

"emailSHA256Hash": null,

"emailMD5Hash": null,

"deviceId": device,

"apiKey": "anapikey",

"listType": "PUSH"

}]}

var x = https.request(token_options,function(res){

res.on('data', (chunk) => {

data += chunk;

});

res.on('end',function(){

token = JSON.parse(data).authToken;

console.log(token);

responsys_endpoint= JSON.parse(data).endPoint.replace('https://','');

console.log(responsys_endpoint);

var campania=req.param("campania", "CASpush")

var restpath ='/rest/api/v1.3/campaigns/'+ campania +'/push'

console.log(restpath);

var email_campaign = {

host: responsys_endpoint,

port: 443,

path: restpath,

method: 'POST',

headers: {

"Authorization": token,

"Content-Type": "application/json"

}

};

var data1=''

var y = https.request(email_campaign,function(res){

res.on('data', (chunk) => {

data1 += chunk;

});

res.on('end',function(){

console.log(data1)

});

});

y.write(JSON.stringify(elcuerpo));

y.end();

});

});

x.write(querystring.stringify(credentials));

x.end();

res.send('');

});

app.listen(8080, function () {

console.log('Escuchando!');

});
Enjoy 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.