Skip to content

Instantly share code, notes, and snippets.

@jmahabal
Created August 30, 2017 19:44
Show Gist options
  • Save jmahabal/b855cdd98a6387989fc0c153a218021f to your computer and use it in GitHub Desktop.
Save jmahabal/b855cdd98a6387989fc0c153a218021f to your computer and use it in GitHub Desktop.
/* Setting things up. */
var path = require('path'),
express = require('express'),
app = express(),
Twit = require('twit'),
config = {
/* Be sure to update the .env file with your API keys. */
twitter: {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
}
},
T = new Twit(config.twitter);
// For static files
app.use(express.static('public'));
/* You can use uptimerobot.com or a similar site to hit your /BOT_ENDPOINT to wake up your app and make your Twitter bot tweet. */
app.all("/" + process.env.BOT_ENDPOINT, function (request, response) {
console.log("Waking up the application.");
});
var listener = app.listen(process.env.PORT, function () {
console.log('Your bot is running on port ' + listener.address().port);
});
var cron = require('node-cron');
var rpn = require('request-promise-native');
// Helper functions
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
cron.schedule('0 0 */6 * * *', function(){ // Every six hours
rpn.get('https://en.wikipedia.org/w/api.php?action=query&list=random&rnlimit=1&rnnamespace=0&format=json')
.then(response => {
let title = JSON.parse(response).query.random[0].title;
let wikipedia_url = "https://en.wikipedia.org/wiki/" + title.replaceAll(" ", "_");
T.post('statuses/update', { status: title + "\n" + wikipedia_url });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment