Skip to content

Instantly share code, notes, and snippets.

@bobjflong
Last active December 19, 2017 18:30
Show Gist options
  • Select an option

  • Save bobjflong/fef9a800e790fa7da502 to your computer and use it in GitHub Desktop.

Select an option

Save bobjflong/fef9a800e790fa7da502 to your computer and use it in GitHub Desktop.
Conversations Tags Demo
var PromiseThrottle = require('promise-throttle');
var wait = require('waitress');
var Intercom = require('intercom-client');
var intercomClient = new Intercom.Client("app id", "api key").usePromises();
var redis = require('redis');
var client = redis.createClient()
var _ = require('underscore');
var fetchConversation = function (id) {
return function () {
return intercomClient.conversations.find({ id: id });
}
};
var throttle = new PromiseThrottle({
requestsPerSecond: 5,
promiseImplementation: Promise
});
client.lrange('conversations', 0, -1, function(err, d) {
var done = wait(d.length, function(err, result) {
var conversationTags = _.reduce(result, function (memo, response) {
var tags = response.body.tags.tags;
return memo.concat(
_.map(tags, function (tag) { return tag.name })
);
}, []);
console.log(summarize(conversationTags));
});
for(var i=0; i < d.length; i++) {
throttle.add(fetchConversation(d[i])).then(function (r) {
done(null, r);
});
}
});
var summarize = function (tags) {
return _.countBy(tags, function (tag) {
return tag;
});
};
var express = require('express')
, app = express()
, bodyParser = require('body-parser')
, jsonParser = bodyParser.json();
app.post('/', jsonParser, function (req, res) {
client.lpush('conversations', req.body.data.item.id, function() {
console.log("Added: " + req.body.data.item.id);
res.send('ok');
});
});
var server = app.listen(1234);
{
"name": "conversation_tags_api_example",
"version": "1.0.0",
"description": "An example of using the Intercom Conversations API and tags to gather basic summary statistics",
"main": "index.js",
"dependencies": {
"promise-throttle": "*",
"intercom-client": "*",
"waitress": "*",
"redis": "*",
"underscore": "*"
},
"devDependencies": {
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Bob Long",
"license": "Apache 2"
}
@crlsfilho
Copy link

omg thanks so much, man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment