Skip to content

Instantly share code, notes, and snippets.

@thameera
Last active January 4, 2016 18:12
Show Gist options
  • Save thameera/5af64146128ead5d9d77 to your computer and use it in GitHub Desktop.
Save thameera/5af64146128ead5d9d77 to your computer and use it in GitHub Desktop.
A webtask ( https://webtask.io/ ) that takes in the text of a tweet and adds the URL in that tweet to Pocket queue if there's any. eg: This can called from an IFTTT task that triggers each time when a tweet mentioning the word 'auth0' is found.
"use latest";
/*
* This webtask takes in the text of a tweet that mentions the
* word 'auth0' and if there's a link in that tweet adds it to
* the Pocket queue.
* Pocket consumer key and access token should be set as secrets
* to the webtask.
*/
const request = require('request');
const extractUrl = (text) => {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = urlRegex.exec(text);
if (!urls) {
return null;
}
return urls[0];
};
module.exports = (ctx, done) => {
const url = extractUrl(decodeURIComponent(ctx.data.text));
// Ignore tweets with no URLs
if (!url) {
done();
}
request
.post('https://getpocket.com/v3/add')
.form({
url,
consumer_key: ctx.data.POCKET_CONSUMER_KEY,
access_token: ctx.data.POCKET_ACCESS_TOKEN
})
.on('response', (response) => {
if (response.statusCode === 200) {
console.log(`Successfully saved URL: ${url}`);
done();
} else {
console.log(`Error saving URL. Status code: ${response.statusCode}`);
done(response);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment