node.js passing in customer email to custom app

  • 1 November 2016
  • 1 reply
  • 58 views


I am trying to pass in data from a newly created freshdesk ticket.


The ticket number is passed in correctly, but not the email from the requester.




So I am using a Dispatchr webhook rule that does a GET to our company's API.


The webhook looks like this:




GET http://<custom-app-URL-redacted>?email={{ticket.from_email}}&ticketId={{ticket.id}}




If I 'hard code' the email in the app the 'GET request' succeeds.  I'm having an issue with getting the email from the ticket to be passed on in the webhook.




The node.js code follows:


(Line 3 is where I am having issues.)




p.s. there is an open ticket with freshdesk support for this, but I haven't had any helpful insight yet.


var request = require('request');
var util = require('util');
var email = '/requester_email';
var authToken = process.env.TOKEN;
var url = util.format("https://<api endpoint redacted>/users/find?email=%s&authToken=%s", email, authToken);
var ticketId = '/ticket_id';
var express = require('express');
var app = express();
var http = require("http");


function saveTicket(ticketId, email) {
console.log(saveTicket);
request({
url: url,
method: 'GET',
json: true
},


function (err, res, body) {
var id, isVerified, username;

console.log(res);

if (!err && res) {

console.log(JSON.stringify(body));

if (body.flags) {
isVerified = body.flags.isVerified;
}
id = body.id;

username = body.username;

var freshDeskUrl = util.format('https://<company name redacted>.freshdesk.com/api/v2/tickets/%s/notes', ticketId);
var freshDeskToken = new Buffer('<credentials:redacted>').toString('base64');

request({
url: freshDeskUrl,
method: 'POST',
headers: {
'Authorization': util.format('Basic %s', freshDeskToken),
'Content-Type': 'application/json'
},
json: {
'body': util.format("User information <br/> User id: %s, <br/> Email Address: %s, <br/> Username: %s, <br/> isVerified: %s", id, email, username, isVerified),
'private': true,
'notify_emails':["<email redacted>"]
}
}, function (err, res, body) {
if (err) console.log(err);
console.log(res.statusCode);
});
}
});
};

app.get('/', function(req, res) {
var email = req.query.email;
var ticket_id = req.query.ticketId;
saveTicket(ticket_id, email);
res.end('Hello World! I am running');
});

app.listen(8080, function () {
console.log('App listening on port 8080!');
});


 





This topic has been closed for comments

1 reply


I got it all figured out. I had some things out of scope.  Fixed code should look like this:


 


var request = require('request');
var util = require('util');
var authToken = process.env.TOKEN;
var express = require('express');
var app = express();
var http = require("http");


function saveTicket(ticketId, email) {
var url = util.format("https://<redacted>/users/find?email=%s&authToken=%s", email, authToken);

request({
url: url,
method: 'GET',
json: true
},


function (err, res, body) {
var id, isVerified, username;

console.log(res);

if (!err && res) {

console.log(JSON.stringify(body));

if (body.flags) {
isVerified = body.flags.isVerified;
}
id = body.id;
username = body.username;

var freshDeskUrl = util.format('https://<redacted>.freshdesk.com/api/v2/tickets/%s/notes', ticketId);
var freshDeskToken = new Buffer('<redacted:credentials>').toString('base64');

request({
url: freshDeskUrl,
method: 'POST',
headers: {
'Authorization': util.format('Basic %s', freshDeskToken),
'Content-Type': 'application/json'
},
json: {
'body': util.format("User information <br/> User id: %s, <br/> Email Address: %s, <br/> Username: %s, <br/> isVerified: %s", id, email, username, isVerified),
'private': true,
'notify_emails':["<redacted>"]
}
}, function (err, res, body) {
if (err) console.log(err);
console.log(res.statusCode);
});
}
});
};

app.get('/', function(req, res) {
var email = req.query.email;
var ticket_id = req.query.ticketId;
saveTicket(ticket_id, email);
res.end('Hello World! I am running');
});

app.listen(8080, function () {
console.log('App listening on port 8080!');
});