Skip to content

Instantly share code, notes, and snippets.

@lesterjude
Created March 7, 2015 10:22
Show Gist options
  • Save lesterjude/a18de303d4350076b7af to your computer and use it in GitHub Desktop.
Save lesterjude/a18de303d4350076b7af to your computer and use it in GitHub Desktop.
Zendesk Create Ticket for Callback
var credentials = {
"token": "jjx1OIWIKGrQdpJcLmHFEdPcrmuzx2ZW0BLgHVxa",
"base_url": "https://drumbi.zendesk.com/",
"username": "farsheed@drumbi.com",
"api_path": "api/v2",
"session_id": localSession.beatsession_guid
}
/*var credentials = {
"token": "jjx1OIWIKGrQdpJcLmHFEdPcrmuzx2ZW0BLgHVxa",
"base_url": "https://drumbi.zendesk.com/",
"username": "farsheed@drumbi.com",
"api_path": "api/v2",
"session_id": localSession.beatsession_guid
}
*/
var dashboard_url = "https://dashboard.drumbi.com/v1/notification/" + localSession.beatsession_guid;
drumbi.getAllVariables(function(err, sessionData) {
if(sessionData.ticket_id){
updateTicket(sessionData);
}
else{
var json = {
caller_email: sessionData.email,
agent_firstname: sessionData.agent_firstname,
caller_name: sessionData.caller_name,
caller_number: sessionData.caller_number || sessionData.callback_number,
subject: sessionData.topic,
credentials: credentials,
page: decodeURIComponent(sessionData.url),
referrer: decodeURIComponent(sessionData.referrer),
session_id: localSession.beatsession_guid,
ip: sessionData.ip,
platform: sessionData.platform,
browser: sessionData.browser,
url: decodeURIComponent(sessionData.url),
tickets: []
};
drumbi.set({'session_info': JSON.stringify(json),
caller_name: sessionData.caller_name,
email: sessionData.email
}, function() {
customerSearch(json);
})
}
})
function updateTicket(sessionData){
var name = sessionData.agent_firstname;
var number = sessionData.caller_number || sessionData.callback_number;
var body = 'Callback Initiated by ' + name + '.\n'
+ 'Phone Call to: ' + number + '\n'
+ 'Session ID: ' + localSession.beatsession_guid + '\n'
+ 'Drumbi Details: ' + dashboard_url;
zendesk.updateTicket({
ticket: {
id: sessionData.ticket_id,
comment: {
body: body,
public: false,
author_id: sessionData.requester_id
}
},
caller: {
name: sessionData.caller_name,
email: sessionData.email,
number: sessionData.caller_number
},
credentials: sessionData.session_info.credentials
}, function(err, result) {});
}
function customerSearch(sessionData) {
var callerInfo = {
"name": "No Name",
"email": "",
"caller_number" : ""
}
if(sessionData.caller_name)
callerInfo.name = sessionData.caller_name;
if(sessionData.caller_email)
callerInfo.email = sessionData.caller_email;
callerInfo.caller_number = sessionData.caller_number;
zendesk.customerSearch({
"caller": callerInfo,
"credentials": credentials
}, function(err, response) {
if(response && response.data && !response.data.error){
callerInfo.name = response.data.caller_info.name;
callerInfo.email = response.data.caller_info.email;
//callerInfo.caller_number = response.data.caller_info.phone || callerInfo.caller_number;
}
sessionData.caller_number = callerInfo.caller_number;
sessionData.caller_email = callerInfo.email;
sessionData.caller_name = callerInfo.name;
createTicket(sessionData);
});
}
function createTicket(sessionData) {
var name = sessionData.agent_firstname;
var number = sessionData.caller_number || sessionData.callback_number;
var body = 'Callback Initiated by ' + name + '.\n'
+ 'Phone Call to: ' + number + '\n'
+ 'Session ID: ' + localSession.beatsession_guid + '\n'
+ 'Drumbi Details: ' + dashboard_url;
//get email
zendesk.createTicket({
ticket: {
subject: sessionData.subject + ' > New',
status: 'open',
priority: 'normal',
comment: {
body: body,
public: false
},
via_id: 46
},
caller: {
name: sessionData.caller_name,
email: sessionData.caller_email,
number: sessionData.caller_number
},
credentials: credentials
}, function(err, result) {
drumbi.set({
notification_url: sessionData.credentials.base_url + 'agent/#/tickets/'+ result.data.ticket.id,
zendesk: sessionData.credentials.base_url + 'agent/#/tickets/'+ result.data.ticket.id,
zenid: result.data.ticket.id,
topic: sessionData.subject,
ticket_id: result.data.ticket.id,
requester_id: result.data.ticket.requester_id
}, function(err, result) {
//drumbi.connectCaller();
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment