Skip to content

Instantly share code, notes, and snippets.

@bastien
Created March 1, 2024 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastien/f3a87baf18fcb53dcbebcd1804779288 to your computer and use it in GitHub Desktop.
Save bastien/f3a87baf18fcb53dcbebcd1804779288 to your computer and use it in GitHub Desktop.
Test script that creates a help-center article with an attachment and updates its content
EMAIL={admin-email}
PASSWORD={admin-password}
SUBDOMAIN={your-subdomain}
SECTIONID={some-id}
PERMISSIONGROUPID={permssion-group-id}
FILENAME={relative-path-to-file-to-upload}
LOCALE={locale}
#!/usr/bin/node
// Dependencies:
// node v20.11.1
// npm install --save form-data
//
// .env file format:
//
// EMAIL={admin-email}
// PASSWORD={admin-password}
// SUBDOMAIN={your-subdomain}
// SECTIONID={some-id}
// PERMISSIONGROUPID={permssion-group-id}
// FILENAME={relative-path-to-file-to-upload}
// LOCALE={locale}
//
// node --env-file=.env article_attachment_update.js
const https = require('https')
const formData = require('form-data');
const fs = require('fs')
const email = process.env.EMAIL
const password = process.env.PASSWORD
const subdomain = process.env.SUBDOMAIN
const sectionId = process.env.SECTIONID
const permissionGroupId = process.env.PERMISSIONGROUPID
const filename = process.env.FILENAME
const locale = process.env.LOCALE
const headers = {
'Authorization': 'Basic ' + Buffer.from(email + ':' + password).toString('base64'),
'content-type': 'application/json'
}
function createAttachment() {
return new Promise((resolve, reject) => {
let form = new formData()
const file = fs.readFileSync(__dirname + '/' + filename)
form.append('file', file, filename)
form.append('inline', 'true')
const attachmentHeaders = { ...headers, ...form.getHeaders()}
const attachmentCreateOptions = {
hostname: subdomain + '.zendesk.com',
method: 'POST',
path: '/api/v2/help_center/articles/attachments.json',
headers: attachmentHeaders
}
const attachmentCreateRequest = https.request(attachmentCreateOptions, (res) => {
let data = []
console.log('statusCode:', res.statusCode)
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = JSON.parse(Buffer.concat(data).toString())
resolve(response)
})
res.on('error', (error) => {
reject(error);
})
})
form.pipe(attachmentCreateRequest)
attachmentCreateRequest.end()
})
}
function createArticle(attachmentId) {
return new Promise((resolve, reject) => {
const articleCreateOptions = {
hostname: subdomain + '.zendesk.com',
method: 'POST',
path: '/api/v2/help_center/sections/' + sectionId + '/articles',
headers: headers
}
const articleCreateRequest = https.request(articleCreateOptions, (res) => {
let data = []
console.log('statusCode:', res.statusCode)
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = JSON.parse(Buffer.concat(data).toString())
resolve(response)
})
res.on('error', (error) => {
reject(error);
})
})
const body = '<p><img src="/hc/article_attachments/'+attachmentId+'"></p>'
const data = {
article: {
body: body,
title: 'Attachment Update Test',
locale: locale,
user_segment_id: null,
permission_group_id: permissionGroupId
}
}
articleCreateRequest.write(JSON.stringify(data))
articleCreateRequest.end()
})
}
function deleteAttachment(id) {
return new Promise((resolve, reject) => {
const attachmentDeleteOptions = {
hostname: subdomain + '.zendesk.com',
method: 'DELETE',
path: '/api/v2/help_center/articles/attachments/' + id,
headers: headers
}
const attachmentDeleteRequest = https.request(attachmentDeleteOptions, (res) => {
let data = []
console.log('statusCode:', res.statusCode)
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = Buffer.concat(data).toString()
resolve(response)
})
res.on('error', (error) => {
reject(error);
})
})
attachmentDeleteRequest.end()
})
}
function listAttachments(articleId) {
return new Promise((resolve, reject) => {
const attachmentListOptions = {
hostname: subdomain + '.zendesk.com',
method: 'GET',
path: '/api/v2/help_center/articles/' + articleId + '/attachments',
headers: headers
}
const attachmentListRequest = https.request(attachmentListOptions, (res) => {
let data = []
console.log('statusCode:', res.statusCode)
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = JSON.parse(Buffer.concat(data).toString())
resolve(response)
})
res.on('error', (error) => {
reject(error);
})
})
attachmentListRequest.end()
})
}
function updateTranslation(article) {
return new Promise((resolve, reject) => {
const translationUpdateOptions = {
hostname: subdomain + '.zendesk.com',
method: 'PUT',
path: '/api/v2/help_center/articles/' + article.id + '/translations/' + locale,
headers: headers
}
const translationUpdateRequest = https.request(translationUpdateOptions, (res) => {
let data = []
console.log('statusCode:', res.statusCode)
res.on('data', (chunk) => {
data.push(chunk);
});
res.on('end', () => {
const response = JSON.parse(Buffer.concat(data).toString())
resolve(response)
})
res.on('error', (error) => {
reject(error);
})
})
const body = article.body + '<p>UPDATED</p>'
const data = {
translation: {
body: body
}
}
translationUpdateRequest.write(JSON.stringify(data))
translationUpdateRequest.end()
})
}
async function testAttachmentUpdate () {
let attachment = await createAttachment()
let article = await createArticle(attachment.article_attachment.id)
let attachments = await listAttachments(article.article.id)
let attachmentIds = attachments.article_attachments.map((attachment) => attachment.id)
console.log(attachment)
console.log(article)
console.log(attachments)
/* for the sake of the test we delete the attachment, then update the
* translation still with the same attachment reference inside
* and check that the article again is associated to a new attachment
*/
await deleteAttachment(attachment.article_attachment.id)
let translation = await updateTranslation(article.article)
attachments = await listAttachments(article.article.id)
let postDeleteAttachmentIds = attachments.article_attachments.map((attachment) => attachment.id)
console.log(translation)
console.log(attachments)
let result = postDeleteAttachmentIds.length > 0 &&
postDeleteAttachmentIds.length == attachmentIds.length
console.log(postDeleteAttachmentIds)
console.log(attachmentIds)
console.log('All attachment present? - ' + result)
}
testAttachmentUpdate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment