Created
June 22, 2019 17:32
-
-
Save deleteman/8ce40065de1643218f2be695d8d87b27 to your computer and use it in GitHub Desktop.
Full code for integrating olark and buttercms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WRITE_TOKEN = 'your-buttercms-write-api-key' | |
const express = require("express") | |
const request = require("request") | |
const bodyParser = require("body-parser") | |
const slug = require("slug") | |
const app = express(); | |
app.use(bodyParser.urlencoded({extended: true})) | |
/** | |
* Turns a list of JSON objects like this: | |
* { | |
body: 'hey there!', | |
timestamp: '1561209998.139551', | |
kind: 'MessageToVisitor', | |
nickname: 'Fernando', | |
operatorId: '1046479' | |
} | |
* | |
* Into something easier to read, like this: | |
* < (1561214253.621349): hey there! | |
< (1561214255.636326): I have a question for you! | |
< (1561214261.976311): if you can answer it please | |
< (1561214266.057416): that woudl be awesome! | |
> (1561214274.637839): hey there, sure I can | |
> (1561214277.006478): tell me about it | |
< (1561214296.327043): what's your name? | |
> (1561214298.993349): fernando | |
* | |
*/ | |
function getConvoTranscript(items) { | |
return items.map( i => { | |
let transcript = [] | |
if(i.kind == 'MessageToVisitor') transcript.push("> ") | |
else transcript.push("< ") | |
transcript.push("(" + i.timestamp + "): ") | |
transcript.push(i.body) | |
return transcript.join(" ") | |
}).join("\n") | |
} | |
app.post('/olark-hook', async function (req, res) { | |
/** | |
* 1. get olark payload | |
* 2. check for tags containing "faq" tag | |
* 3. create a new FAQ item in butter | |
* - Title: Olark Question: ID | |
* - Question: empty | |
* - Answer: empty | |
* - Transcript: full convo transcript | |
* | |
* Future improvements: | |
* 1. Keep track of received convo ids, and their items | |
* - If already received convo ID is sent again, only process new items | |
*/ | |
let body = JSON.parse(req.body.data) | |
if(body.tags.map( t => t.toLowerCase() ).indexOf("faq") != -1) { | |
let pTitle = "Olark question: " + body.id | |
let newPage = { | |
title: pTitle, | |
slug: slug(pTitle), | |
"page-type": "faq", | |
fields: { | |
"en": { | |
question: "", | |
answer: "", | |
transcript: getConvoTranscript(body.items) | |
} | |
} | |
} | |
let postURL = 'https://api.buttercms.com/v2/pages/' | |
request.post({ //perform the page creation | |
url: postURL, | |
headers: { | |
"Authorization": "Token " + WRITE_TOKEN | |
}, | |
json: true, | |
body: newPage, | |
}, (err, resp) => { //We're done! | |
if(err) { | |
console.log("There was an error: ", err) | |
return res.json(err) | |
} | |
console.log("Done, new page successfully created in ButterCMS!") | |
console.log(resp) | |
res.json({ integrationUrl: "http://your-host.com/olark-hook"}) | |
}) | |
} | |
}) | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment