bio photo

Email Twitter Facebook LinkedIn Github

Update - updated code for tweet bot below.

I have decided to take down mean tweets on twitter one by one - https://www.twitter.com/MeanAlert. When somebody tweets “I hate talking to drivers on Uber”, they get an auto-tweet back.

I was a bit annoyed when I searched I hate Uber on Twitter, lots of people were hating on drivers,

People are sharing the same physical space yet tweet out their frustration, for me this culture of not having uncomfortable conversations is a downward trajectory.

  • The use of the word “Hate” is used too freely.
  • Some problems are just not problems, bitching about a chauffer you’ve summoned at the press of a button is not a problem.

  • Look around, your life is amazing, don’t be a jack-ass to somebody not as well off as you.

So I built the bot

This was the first tweet that went out,

I was surprised when I got retweeted, I’d like to think I made a point and changed somebody.

Not everybody took it well, infact just the second tweet was replied to by a person who made an uncomfortable (and if I can say so), poorly articulated, comeback.

I lol’d. I’d like to think I made a point there too.

This was my 3rd Tweet,

I lol’d, this person looked really tough, but I was happy hetook it in stride and didn’t say anything back.

Under the hood

So let’s take a look at the code:

var Twitter = require('twitter');// Step 1
var Keys = require('./keys.js')(); // Step 1
var http = require("http");
var fs = require('fs'); // Using the filesystem module
var httpServer = http.createServer(requestHandler);

var request = require('request'); //library



////////////////// Step 1 //////////////////////////////////
var client = new Twitter({
    consumer_key: Keys.consumer_key,
    consumer_secret: Keys.consumer_secret,
    access_token_key: Keys.access_token_key,
    access_token_secret: Keys.access_token_secret
});

// var gistRawURL = 'https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e';
// request(gistRawURL, function (error, response, body) {
//   	if (!error && response.statusCode == 200) {
//     	var arr = body.split('\n');
//     	// pick random string from arr here...
// 	}
// });

// return;

client.get('search/tweets', {q: 'i hate uber'}, function(error, tweets, response){
   //console.log(tweets);
   console.log(tweets);

	// for (var i = 0; i < tweets.statuses.length; i++) {
		var tweet = tweets.statuses[0];
		// tweet.user.screen_name;
		// tweet.text;
		// tweet.id
	// }

	client.post('statuses/update', {
		status: '@'+ tweet.user.screen_name + ' pretty sure the driver had it worse', in_reply_to_status_id: tweet.in_reply_to_status_id

	}, function(error, tweet, response){
	  if (!error) {
	    console.log(tweet);
	  } else {
	  	console.log(error);
	  }
	});

});

function requestHandler(req, res) {
	// Read index.html
	fs.readFile(__dirname + '/index.html', 
		// Callback function for reading
		function (err, data) {
			// if there is an error
			if (err) {
				res.writeHead(500);
				return res.end('Error loading whatever youre loading');
			}
			// Otherwise, send the data, the contents of the file
			res.writeHead(200);
			res.end(data);
  		}
  	);
}

httpServer.listen(8080, console.log("listening to 8080"));

Some Problems that need solving:

  • crontab -e not working

I was manually starting/stopping script (did 10 tweets)

  • Some responses weren’t accurate, especially when the script was run late at night (when people aren’t taking or talking about Uber)
  • Response tweet (harcoded) was not dynamic. Solution I was working on involved scraping tweets from a gist.github.com file, I never got there:
// var gistRawURL = 'https://gist.githubusercontent.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e';
// request(gistRawURL, function (error, response, body) {
//   	if (!error && response.statusCode == 200) {
//     	var arr = body.split('\n');
//     	// pick random string from arr here...
// 	}
// });

// return;

Extensibility

If I make my bot dynamic it can extend from this uber driver experiment and taking on more mean tweets.

** Updates to code for extensibility **

With the help of Sam Lavigne here are some updates to the code for extensibility. I’ve made tweets dynamic (they’re pulled from an editable Gist file and not hardcoded anymore).

Here’s what the code looks like now:

var gistRawURL = 'https://gist.githubusercontent.com/osehgol/0e751856cc975f17aa87/raw';
request(gistRawURL, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var arr = body.split('\n');
        console.log(arr);
        //ensure no empty value in array is tweeted - in case of unecessary carriage return
        arr = arr.filter(function(val){
            return val != "";
        });
        //randomizes tweet after Googling "javascript random element from array"
        var item = " " + arr[Math.floor(Math.random()*arr.length)];
        makeTweet(item);
        // pick random string from arr here...
    }
});

//code below illustrates how to manipulate arrays efficiently with JavaScript.
//arr.map(function(val{
//      return val + " is a number"
//}));

//this is the same code as above, typed out in full-form
//var newArray =[];
//for (var i = 0; i < arr.length; i++){
//      if(arr[i] != "";
//          newArray.push(arr[i]);
//}