Created
October 13, 2016 19:24
-
-
Save samueleresca/0c55b6dae1ad5b7a94195b523622cdfc to your computer and use it in GitHub Desktop.
This file contains 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
exports.extractUrl = function _extractUrl(message) { | |
if (message.type !== "message") return; | |
if (typeof message.attachments !== "undefined" | |
&& message.attachments.length > 0) { | |
return message.attachments[0].contentUrl; | |
} | |
if (typeof message.text !== "") { | |
return _findUrl(message.text); | |
} | |
return ""; | |
}; | |
function _findUrl(text) { | |
var source = (text || '').toString(); | |
var matchArray; | |
// Regular expression to find FTP, HTTP(S) and email URLs. | |
var regexToken = /(((http|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)/g; | |
// Iterate through any URLs in the text. | |
if ((matchArray = regexToken.exec(source)) !== null) { | |
var token = matchArray[0]; | |
return token; | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment