Skip to content

Instantly share code, notes, and snippets.

@samueleresca
Created October 13, 2016 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samueleresca/0c55b6dae1ad5b7a94195b523622cdfc to your computer and use it in GitHub Desktop.
Save samueleresca/0c55b6dae1ad5b7a94195b523622cdfc to your computer and use it in GitHub Desktop.
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