Created
September 1, 2015 20:07
-
-
Save lperrin/5a7d23f55943a80d6981 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
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 === '[email protected]') | |
return recipient.name; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment