Last active
May 22, 2017 08:39
-
-
Save glennblock/882c5534d813a668169857d1da865d13 to your computer and use it in GitHub Desktop.
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
module.exports = function(context, cb) { | |
var slack = require("slack-notify")(context.secrets.SLACK_URL); | |
var htmlToText = require("html-to-text"); | |
var body = context.body; | |
var util = require('util'); | |
if(body !== null && body.data !== null && body.data.item !== null) { | |
var item = body.data.item; | |
if (isAuth0ExtendMessage(item, context.secrets.SUBJECT)) { | |
var parts = item.conversation_parts.conversation_parts[0]; | |
//extract text from the HTML | |
var text = htmlToText.fromString(parts.body, { | |
wordwrap: 130 | |
}); | |
var conversationUrl = item.links.conversation_web; | |
// trim the text and add "... More" | |
if (text.length > 215) { | |
text = text.substring(0, 214) + `... <${conversationUrl}|More>` | |
} | |
var message = composeMessage(body, item, parts, conversationUrl, text, context.secrets.CHANNEL); | |
slack.send(message); | |
} | |
} | |
cb(null, null); | |
}; | |
function isAuth0ExtendMessage(item, subject) { | |
return (item.conversation_message !== null && item.conversation_message.subject == subject); | |
} | |
function createMessage(color, msgText, channel) { | |
var message = { | |
channel: channel, | |
icon_url: "https://fst.slack-edge.com/2fac/plugins/intercom/assets/service_36.png", | |
text: "*Intercom*", | |
attachments: [ | |
{ | |
color: color, | |
text: msgText | |
} | |
] | |
} | |
return message; | |
} | |
function composeMessage(body, item, parts, conversationUrl, text, channel) { | |
var from = parts.author.name; | |
var to; | |
var color; | |
if (parts.author.type === 'user') { | |
to = item.assignee.name; | |
var fromUrl = `https://app.intercom.io/apps/${body.app_id}/users/${parts.author.id}`; | |
color = "#4277f4"; | |
msgText = `<${fromUrl}|${from}> replied to <${conversationUrl}|a conversation> with ${to}\n\n${text}`; | |
} | |
else { | |
to = item.user.name; | |
color = "#f4b541"; | |
msgText = `${from} replied to <${conversationUrl}|a conversation> with ${to}\n\n${text}`; | |
} | |
var message = createMessage(color, msgText, channel); | |
return message; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment