A Drink Bot – Kyna McIntosh

EDIT: Currently running at https://twitter.com/SonofaDrinkBot

So I created a bot that detects whenever someone tweets ‘i need a drink,’ and, using their name and message, concocts a mixed drink, and sends an image of it to the user.

I was suspended within the first 3 minutes for spam (I was tweeting once every 15 seconds or so).

Here’s the page now : https://twitter.com/A_Drink_Bot

And what it looked like before I got banned :

The first ban was just a warning ban, and I randomized my tweets a bit and only posted once every two or three minutes after I ‘unsuspended’ myself.

Because the drinks were somewhat randomized, some of them sounded pretty good and others sounded like you would go blind from drinking them…
Here’s the flickr gallery containing all of the drinks my bot made during its short, short life : http://www.flickr.com/photos/90787080@N06/sets/72157632174450103/

I was permanently suspended within a half hour.

CODE!

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.media.*;
import java.io.File.*;

static String OAuthConsumerKey    = "*";
static String OAuthConsumerSecret = "*";
static String AccessToken         = "*";
static String AccessTokenSecret   = "*";

Twitter myTwitter;

String  myQueryPhrase = "i need a drink";  
long    previousIdOfTweetContainingQuery = 0;

class Ingredient {
  String name;
  String img;

  Ingredient(String nameOf, String imgPath) {
    name = nameOf;
    img = imgPath;
  }
}

class Drink {
  String name;
  String alcohol;
  String mixer;
  String garnish;
  String alcohol2;

  Drink(String n, String a, String m, String g, String a2) {
    name = n;
    alcohol = a;
    mixer = m;
    garnish = g;
    alcohol2 = a2;
  }
}

ArrayList alcohols;
ArrayList mixers;
ArrayList garnish;

void setup() {

  size(525, 350);
  background(255);
  frameRate(10);

  imageMode(CENTER);

  alcohols = new ArrayList();
  mixers = new ArrayList();
  garnish = new ArrayList();

  //gotta make list of alcohol and imgs
  String tempAlcohols[] = loadStrings("alcohols/alcohols.txt");
  for (int i=0; i < tempAlcohols.length; i++) {
    String holdA[] = split(tempAlcohols[i], '_');
    println(holdA[0]);
    println(holdA[1]);
    alcohols.add(new Ingredient(holdA[0], holdA[1]));
  }

  //gotta make list of mixers and imgs
  String tempMixers[] = loadStrings("mixers/mixers.txt");
  for (int j=0; j < tempMixers.length; j++) {
    String holdM[] = split(tempMixers[j], '_');
    mixers.add(new Ingredient(holdM[0], holdM[1]));
  }

  //gotta make list of garnish and imgs
  String tempGarnish[] = loadStrings("garnish/garnish.txt");
  for (int k=0; k = a) {
    two = true;
    int newRand2 = (int)map(rand2, 0, 10*(user.length()), 0, alcohols.size());
    Ingredient alc2 = alcohols.get(newRand2);
    PImage alc2img = loadImage("alcohols/" + alc2.img);
    alc2img.resize(250, 250);
    image(alc2img, 150, 200);
    text(alc2.name, 275, 200);
  }
}
void makeDrink(String user, String message) {
  fill(0);
  textSize(18);
  boolean two = false; 

  //generate alcohol
  int a = (int)(user.charAt(0));
  int newA = (int)map(a, 0, 127, 0, alcohols.size());
  Ingredient alc = alcohols.get(newA);
  PImage alcimg = loadImage("alcohols/" + alc.img);
  alcimg.resize(250, 250);
  image(alcimg, 150, 200);
  text(alc.name, 275, 150);  

  //add extra alcohol?
  int rand2 = (int)(random(0, 10*(user.length())));
  if (rand2 >= a) {
    two = true;
    int newRand2 = (int)map(rand2, 0, 10*(user.length()), 0, alcohols.size());
    Ingredient alc2 = alcohols.get(newRand2);
    PImage alc2img = loadImage("alcohols/" + alc2.img);
    alc2img.resize(250, 250);
    image(alc2img, 150, 200);
    text(alc2.name, 275, 200);
  }

  //generate mixer
  int rand1 = (int)(random(0, (message.length())));
  int b = (int)(message.charAt(rand1));
  int newB = (int)map(b, 0, 127, 0, mixers.size());
  Ingredient mix = mixers.get(newB);
  PImage miximg = loadImage("mixers/" + mix.img);
  miximg.resize(250, 250);
  image(miximg, 150, 200);
  if (two == true) text(mix.name, 275, 250);
  else text(mix.name, 275, 200);

  //add garnish?
  int rand3 = (int)(random(0, rand2));
  int newRand3 = (int)map(rand3, 0, rand2, 0, garnish.size());
  if (newRand3 <= a) {
    Ingredient gar = garnish.get(newRand3);
    PImage garimg = loadImage("garnish/" + gar.img);
    garimg.resize(250, 250);
    image(garimg, 150, 200);
    if (two == true) text(gar.name, 275, 300);
    else text(gar.name, 275, 250);
  }

  PImage glass = loadImage("glass.png");
  glass.resize(250, 250);
  image(glass, 150, 200);

  textSize(36);

  text("The "+ user, 50, 50, 450, 150); 
  //put a name on dat shit

  //save dat shit
  save("/processing-2.0b3/"+user+".png");
}
void draw() {
  background(255);

  ConfigurationBuilder cb = new ConfigurationBuilder();
  cb.setOAuthConsumerKey(OAuthConsumerKey);
  cb.setOAuthConsumerSecret(OAuthConsumerSecret);
  cb.setOAuthAccessToken(AccessToken);
  cb.setOAuthAccessTokenSecret(AccessTokenSecret);

  Twitter myTwitter = new TwitterFactory(cb.build()).getInstance();

  try {
    println ("Searching.... Current time = " + hour() + ":" + minute() + ":" + second()); 
    Query query = new Query(myQueryPhrase);
    //maybe because i changed the way it was arranged??
    QueryResult result = myTwitter.search(query);

    ArrayList tweetsContainingQuery = (ArrayList) result.getTweets();
    if (tweetsContainingQuery.size() > 0) {
      Status mostRecentTweetContainingQuery = (Status) tweetsContainingQuery.get(0);
      long mostRecentTweetContainingQueryId = mostRecentTweetContainingQuery.getId();

      if (previousIdOfTweetContainingQuery == 0) {
        previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;
      }
      if (mostRecentTweetContainingQueryId != previousIdOfTweetContainingQuery) {
        // yay! someone has just tweeted our favorite word!
        previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;

        // print out the new tweet, for fun. 
        User used = mostRecentTweetContainingQuery.getUser();
        String user = used.getName();
        String username = used.getScreenName();
        String msg = mostRecentTweetContainingQuery.getText();
        Date d = mostRecentTweetContainingQuery.getCreatedAt();

        makeDrink(username, msg);

        //new error
        File src = new File(username+".png");

        File real = new File(src.getAbsolutePath());

        String stat = ("@"+username+" I heard you needed a drink, so I made you one. ");

        StatusUpdate status = new StatusUpdate(stat);
        status.setMedia(real);

        status.setInReplyToStatusId(mostRecentTweetContainingQueryId);
        myTwitter.updateStatus(status);

        println("Tweet by " + user + " at " + d + ": " + msg);
      }
    }
  }

  catch (TwitterException te) {
    println("Error connecting to Twitter: " + te);
  };

  delay (12000); // REALLY IMPORTANT, PEOPLE. You're limited to 350 requests per hour, or about once per 11 seconds.
}

Post a comment