From 0b2b8e4b40f0d315b968beebc0dd71f8a03c1e59 Mon Sep 17 00:00:00 2001 From: Paritosh Gupta Date: Thu, 29 Dec 2016 15:53:19 +0530 Subject: [PATCH 1/4] Added CreateTragetedMessage Function --- README.md | 14 ++++++++++++++ index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/README.md b/README.md index 47439e1..4127b93 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,20 @@ client.getTags(getTagsOptions , function(error, response) { }); ``` +#### Send Targeted Message +To view the syntax for device_filter and to pass extra options (please refer to the Pushwoosh [doc](http://docs.pushwoosh.com/docs/createtargetedmessage) for the available options). + +```javascript +var Pushwoosh = require('pushwoosh-client'), + client= new Pushwoosh("AuthToken"), + options = { + device_filter: 'A("00000-00000") * T("gender", EQ, "F")' + }; + client.sendMessage('Hello world', options, function(error, response) { + ... + }); +``` + ### Tests npm test diff --git a/index.js b/index.js index e6b00bb..e98ef0e 100644 --- a/index.js +++ b/index.js @@ -246,4 +246,52 @@ PushwooshClient.prototype.parseResponse = function(response, body, callback) { callback(new Error('Unknown response code / error')); }; +PushwooshClient.prototype.sendTargetedMessage = function (msg, options, callback) { + + var client = this; + if (!msg || typeof msg !== 'string') { + return callback(new Error('Message has to be provided')); + } + + if(options){ + if(options.hasOwnProperty('application')) return callback(new Error('application cannot be used in a Targeted Message')); + else if(options.hasOwnProperty('applications_group')) return callback(new Error('applications_group cannot be used in a Targeted Message')); + else if(options.hasOwnProperty('platforms')) return callback(new Error('platforms cannot be used in a Targeted Message')); + else if(options.hasOwnProperty('devices')) return callback(new Error('devices cannot be used in a Targeted Message')); + else if(options.hasOwnProperty('filter')) return callback(new Error('filter cannot be used in a Targeted Message')); + else if(options.hasOwnProperty('conditions')) return callback(new Error('conditions cannot be used in a Targeted Message')); + } + + if (!options || !options.hasOwnProperty('devices_filter') || typeof options.devices_filter !== 'string') { + return callback(new Error('Devices Filter has to be provided')); + } + else{ + options.devices_filter = JSON.stringify(options.devices_filter); + } + + var defaultOptions = { + send_date: 'now', + content: msg + }; + + + var notification = extend(defaultOptions, options); + + var body = { + request: { + auth: client.appCode, // first parameter is appCode where we recieve the authToken in this case + } + }; + + var body.request = extend(body.request, notification); + + + client.sendRequest('createTargetedMessage', body, function (error, response, body) { + if (error) { + return callback(error); + } + client.parseResponse(response, body, callback); + }); +}; + module.exports = PushwooshClient; From 5efadaf3c407ade0332c64aa63e3ce7c1f336b77 Mon Sep 17 00:00:00 2001 From: Paritosh Gupta Date: Thu, 29 Dec 2016 16:21:12 +0530 Subject: [PATCH 2/4] Fix variable re-declaration --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index e98ef0e..7655ee0 100644 --- a/index.js +++ b/index.js @@ -274,7 +274,6 @@ PushwooshClient.prototype.sendTargetedMessage = function (msg, options, callback content: msg }; - var notification = extend(defaultOptions, options); var body = { @@ -283,7 +282,7 @@ PushwooshClient.prototype.sendTargetedMessage = function (msg, options, callback } }; - var body.request = extend(body.request, notification); + body.request = extend(body.request, notification); client.sendRequest('createTargetedMessage', body, function (error, response, body) { From 532ac54f5f87015626818223997543f210b97ca5 Mon Sep 17 00:00:00 2001 From: Paritosh Gupta Date: Thu, 29 Dec 2016 18:56:21 +0530 Subject: [PATCH 3/4] Added handling for pushwoosh client --- README.md | 4 +++- index.js | 25 +++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4127b93..3935256 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,9 @@ To view the syntax for device_filter and to pass extra options (please refer to ```javascript var Pushwoosh = require('pushwoosh-client'), - client= new Pushwoosh("AuthToken"), + client= new Pushwoosh("AuthToken", { + isTargetedMessage: true + }), options = { device_filter: 'A("00000-00000") * T("gender", EQ, "F")' }; diff --git a/index.js b/index.js index 7655ee0..598b455 100644 --- a/index.js +++ b/index.js @@ -5,13 +5,26 @@ var request = require('request'), apiVersion = '1.3'; function PushwooshClient(appCode, authToken, options) { + if(!options){ // Only authToken And options are sent in case of Targeted Message + console.log("options missing"); + console.log("authToken: ",authToken); + this.options = authToken; + this.authToken = appCode; + appCode = null; + } + else{ + this.appCode = appCode; + this.authToken = authToken; + } if (!appCode || !authToken) { + if(!this.options || !this.options.hasOwnProperty('isTargetedMessage')) { throw new Error('Application/ApplicationsGroup ID and Authentication Token from Pushwoosh must be provided'); + } + else if(!authToken){ + throw new Error('Authentication Token from Pushwoosh must be provided'); + } } - this.appCode = appCode; - this.authToken = authToken; - // parse the options options = options || {}; @@ -265,9 +278,6 @@ PushwooshClient.prototype.sendTargetedMessage = function (msg, options, callback if (!options || !options.hasOwnProperty('devices_filter') || typeof options.devices_filter !== 'string') { return callback(new Error('Devices Filter has to be provided')); } - else{ - options.devices_filter = JSON.stringify(options.devices_filter); - } var defaultOptions = { send_date: 'now', @@ -278,13 +288,12 @@ PushwooshClient.prototype.sendTargetedMessage = function (msg, options, callback var body = { request: { - auth: client.appCode, // first parameter is appCode where we recieve the authToken in this case + auth: client.authToken, } }; body.request = extend(body.request, notification); - client.sendRequest('createTargetedMessage', body, function (error, response, body) { if (error) { return callback(error); From 9c1030ae911561bad26c7760bbfab5811f70189f Mon Sep 17 00:00:00 2001 From: Paritosh Gupta Date: Fri, 30 Dec 2016 10:23:44 +0530 Subject: [PATCH 4/4] Successfully Ran All Test Cases --- index.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 598b455..069875f 100644 --- a/index.js +++ b/index.js @@ -5,24 +5,25 @@ var request = require('request'), apiVersion = '1.3'; function PushwooshClient(appCode, authToken, options) { - if(!options){ // Only authToken And options are sent in case of Targeted Message - console.log("options missing"); - console.log("authToken: ",authToken); - this.options = authToken; - this.authToken = appCode; - appCode = null; - } - else{ - this.appCode = appCode; - this.authToken = authToken; + var isTargetedMessage = false; + if(!options && authToken && authToken.hasOwnProperty('isTargetedMessage')){ + isTargetedMessage = true; + if(!appCode) { + throw new Error('Authentication Token from Pushwoosh must be provided'); + } } if (!appCode || !authToken) { - if(!this.options || !this.options.hasOwnProperty('isTargetedMessage')) { throw new Error('Application/ApplicationsGroup ID and Authentication Token from Pushwoosh must be provided'); - } - else if(!authToken){ - throw new Error('Authentication Token from Pushwoosh must be provided'); - } + } + + if(!isTargetedMessage) { + this.appCode = appCode; + this.authToken = authToken; + } + else{ + this.options = authToken; + this.authToken = appCode; + this.appCode = null; } // parse the options