Skip to content

Instantly share code, notes, and snippets.

@mataspetrikas
Created August 28, 2018 09:27
Show Gist options
  • Save mataspetrikas/351dce0917a853c8ea679301af66f21e to your computer and use it in GitHub Desktop.
Save mataspetrikas/351dce0917a853c8ea679301af66f21e to your computer and use it in GitHub Desktop.
A Node.js script for importing existing email tickets to Zendesk
// npm install request --save
var request = require('request');
var fs = require('fs');
// update these with your auth values
var username = 'xxx@abc.io';
var userpassword = 'xxxx';
var workspace = 'oursupport';
var importtags = [ "importedticket", "inbox-info"];
function apiPostZendesk(path, data, callback){
var options = {
url: 'https://'+ workspace + '.zendesk.com/api/v2' + path,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
auth: {
'user': username,
'pass': userpassword
}
};
function apiCallback(error, response, body) {
callback(null, JSON.parse(response.body));
}
request(options, apiCallback);
}
function addUser(name, email, callback) {
var userObject = {"user": {"email": email, name: name || email, "tags": ["earlybeta", "imported"]}}
console.log(userObject)
apiPostZendesk("/users/create_or_update.json", userObject, function(error, response){
if (!error && !response.error) {
console.log("User created", JSON.stringify(response));
callback(null, response.user);
} else {
console.error("Couldn't create user", error, response);
callback(error, null);
}
});
}
function addTicket(user, subject, commentBody, date, callback) {
var ticketObject = {
"ticket": {
"requester_id": user.id,
"subject": subject,
"tags": importtags,
"created_at": date.toISOString(),
"comments": [
{ "author_id": user.id, "value": commentBody, "created_at": date.toISOString() }
]
}
};
apiPostZendesk("/imports/tickets.json", ticketObject, function(error, response){
if (!error && response.ticket) {
console.log("Ticket created", response.ticket.id, response.ticket.requester_id);
callback(null, response.ticket);
} else {
console.error("Couldn't create ticket", error);
callback(error, null);
}
});
}
function stringToDate(dateString) {
// assuming that the dateString is in format of "03.06.17 13:37"
const parts = dateString.split(".");
return new Date([parts[1], parts[0], parts[2]].join("/"));
}
function stringToNameEmail(nameString) {
if (nameString.indexOf("<") == -1) {
return [undefined, nameString];
} else {
// assuming the nameString is in format of "Cosad Inx <cosad.inx@icloud.com>"
return nameString.replace(">", "").split(" <");
}
}
function importEmails() {
var email = jsonFile[indexEmail];
console.log(email);
indexEmail = indexEmail + 1;
if (indexEmail >= jsonFile.length) {
clearInterval(importInterval);
}
}
function importEmail(emailId) {
var emailData = jsonFile[emailId];
var name = stringToNameEmail(emailData.sender)[0];
var email = stringToNameEmail(emailData.sender)[1];
// var email = "matas.petrikas+1test@gmail.com";
var subject = emailData.subject;
var date = stringToDate(emailData.date);
var body = emailData.content;
addUser(name, email, function(error, user){
if (user.id) {
addTicket(user, subject, body, date, function(error, ticket){
if (ticket.id && emailId < jsonFile.length - 1) {
console.log("Ticket added for row", emailId, ticket.id);
importEmail(emailId + 1);
} else {
console.log("Ticket adding failed", emailId, error);
}
});
}
});
}
// Get content from file
// format is an array of following objects:
/* {
"subject": "Re: Android Feedback",
"sender": "Thomas <thomas@gmail.com>",
"recipient": "Us <info@us.de>",
"date": "24.07.2018 21:27",
"content": "Subject:\nRe: Android Feedback\nFrom:\n\"Thomas \" <thomas@gmail.com>\nDate:\n24.07.18, 21:27\nTo:\n\"US\" Impressum Datenschutz\n>"
},
*/
var file = fs.readFileSync("emails.json");
var jsonFile = JSON.parse(file);
console.log("json file contains emails:", jsonFile.length)
// starting with the email, if breaks - set it accordingly
var indexEmail = 0;
importEmail(indexEmail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment