Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created October 8, 2019 17:22
Show Gist options
  • Select an option

  • Save spasiu/e093de7df295df487f4265592c155ca2 to your computer and use it in GitHub Desktop.

Select an option

Save spasiu/e093de7df295df487f4265592c155ca2 to your computer and use it in GitHub Desktop.
append data to new Zendesk tickets
/**
* What is the problem we're trying to solve?
* Sometimes a business needs additional information to be part of the record in Support.
* Solution: use a service to handle events sent from Zendesk via a target, parse some info from the event and insert a comment into the ticket with some additional context.
* The real meat and potatoes, which you have to developm is what happens between receiving the event and inserting the comment.
*/
// imports
const bodyParser = require('body-parser');
const express = require('express');
const axios = require('axios');
// constants
const SUBDOMAIN = '[zendesk subdomain]';
const USERNAME = '[zendesk admin username]';
const PASSWORD = '[zendesk admin password]';
const AUTHORID = '[zendesk admin author ID]';
const PORT = 8000;
const getTicket = ticketId => axios({
method: 'get',
url: `https://${SUBDOMAIN}.zendesk.com/api/v2/tickets/${ticketId}.json`,
auth: {
username: USERNAME,
password: PASSWORD
}
});
const getComments = ticketId => axios({
method: 'get',
url: `https://${SUBDOMAIN}.zendesk.com/api/v2/tickets/${ticketId}/comments.json`,
auth: {
username: USERNAME,
password: PASSWORD
}
});
const getUser = userId => axios({
method: 'get',
url: `https://${SUBDOMAIN}.zendesk.com/api/v2/users/${userId}.json`,
auth: {
username: USERNAME,
password: PASSWORD
}
});
const sendComment = (ticketId, body) => axios({
method: 'put',
url: `https://${SUBDOMAIN}.zendesk.com/api/v2/tickets/${ticketId}.json`,
auth: {
username: USERNAME,
password: PASSWORD
},
data: {
ticket: {
comment: {
author_id: AUTHORID,
public: false,
body
}
}
}
});
express()
.use(bodyParser.json())
.post('/resources/:id/events', async function(req, res) {
const params = req.body.metadata;
try {
const user = {} // (await getUser(params['ticket.requester.id'])).data;
const ticket = {} // (await getTicket(params['ticket.id'])).data;
const comments = {} // (await getComments(params['ticket.id'])).data;
console.log(JSON.stringify({ user, ticket, comments, params }, null, 4));
} catch (err) {
console.log('ERROR', err, {
userId: params['ticket.requester.id'],
ticketId: params['ticket.id'],
params
});
}
res.end();
})
.use('/', function(req, res) {
// event data comes from Zendesk in the form of a hash map in JSON format
const props = new Map(req.body);
const message = `**This is a note containing context from some 3rd party system.**
Here's some stuff we know about the user that no other system is aware of:
- they have a dog name "Rx"
- they have seven kids
- they ate an old cookie they found in the couch recently recently.
`;
sendComment(props.get('id'), message)
.then(data => console.log('DATA ->', data))
.catch(error => console.log('ERROR ->', error.message));
// not waiting for a success to acknowledge webhook
res.end();
})
.listen(PORT);
{
"name": "new-ticket-handler",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"express": "^4.17.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment