Last active
December 19, 2017 18:30
-
-
Save bobjflong/fef9a800e790fa7da502 to your computer and use it in GitHub Desktop.
Conversations Tags Demo
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
| 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); |
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
| { | |
| "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" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
omg thanks so much, man