Skip to content

Instantly share code, notes, and snippets.

@lperrin
Created September 1, 2015 20:07
Show Gist options
  • Save lperrin/5a7d23f55943a80d6981 to your computer and use it in GitHub Desktop.
Save lperrin/5a7d23f55943a80d6981 to your computer and use it in GitHub Desktop.
function extractHandle(data) {
var sourceIsIntercomFriendly = data.contact.source === 'email' || data.contact.source === 'intercom';
if (!data.contact || !sourceIsIntercomFriendly)
return null;
if (!/@mail.intercom.io/i.test(data.contact.handle))
return data.contact.handle;
// we need to go back to the messages to get the real intercom contact
if (!data.conversation)
return null;
for (var i = 0; i < data.conversation.messages.length; i++) {
var message = data.conversation.messages[i];
for (var j = 0; i < message.recipients.length; i++) {
var recipient = message.recipients[j];
if (recipient.role === 'from' && recipient.handle === 'notifications@mail.intercom.io')
return recipient.name;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment