Skip to content

Instantly share code, notes, and snippets.

@neuling
Created May 31, 2017 08:56
Show Gist options
  • Save neuling/0db55b13a9df96d7e961379286fde96c to your computer and use it in GitHub Desktop.
Save neuling/0db55b13a9df96d7e961379286fde96c to your computer and use it in GitHub Desktop.
askjollife.js
const Twit = require('twit');
const client = new Twit({
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
});
Array.prototype.random = function() { return this[Math.floor(this.length * Math.random())]; };
const stream = client.stream('statuses/filter', { track: ['@askjollife'] });
const answers = {
yes: ['Yes!', 'yes!', 'Yessss!', 'yep!', 'Positive!'],
no: ['No!', 'NO!', 'hmmm ... NO!', 'Negative!']
};
const onSent = (error, tweet, response) => {
console.log(error ? `Error: ${error}` : `Answered: ${tweet.text}`);
};
stream.on('tweet', (tweet) => {
if (tweet.user.screen_name === 'askjollife' || tweet.text.indexOf('@askjollife') !== 0) { return null; }
const isFather = ['jollife', 'neuling2k', 'i_am_faps'].indexOf(tweet.user.screen_name) !== -1;
const answer = isFather ? answers.yes.random() : answers.no.random();
const answerTimeout = 2000 + (Math.random() * 5000);
setTimeout(() => {
client.post('statuses/update', { status: `@${tweet.user.screen_name} ${answer}`, in_reply_to_status_id: tweet.id }, onSent);
}, answerTimeout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment