MONSTERATTACK!! Attempting a fun Twitterbot

I spent last night sitting on my couch trying to create a fun little twitter bot but I'm not completely done yet. The basic idea is someone tweets "summon #monsterattack @twitteruser" and the bot selects it's target. Right now it just selects it's first target so your tweet has to be at the top of the list to be selected.

The code is a modified version of what the Hak5 Guys were working on a couple weeks ago. I've taken that same basic code and tweaked it a little bit and instead of something happening on a screen it's parsing the string and sending a message from @monsterattack to the victim.

The next step is to set up a cron job on my server to access the page every minute or two but I'm running into a couple issues. I'll be sure to update whenever it is completely up and running.

Hak5 Episode 424 - PHP Twitter Tamagotchi and ROFLcon
Original Hak5 Code

The code may be a little rough, any suggestions are welcome:


<?php
/* ---------------------------------------- */
// Change these parameters with your Twitter
// user name and Twitter password.
/* ---------------------------------------- */
$twitter_username ='username';
$twitter_psw ='password';
/* ---------------------------------------- */

/* Don't change the code belove
/* ---------------------------------------- */
//require('twitterAPI.php');
/* ---------------------------------------- */
require_once('magpierss/rss_fetch.inc');
require('twitterAPI.php');

$previous_items = "1"; //There can only be one
define('MAGPIE_CACHE_ON', 0); //Tell Magpie not to cache
define('MAGPIE_CACHE_AGE', 0); //Mo cache mo problems

$lasturl = "http://search.twitter.com/search.atom?q=+from:monsterattack"; //what are we looking for

$url = "http://search.twitter.com/search.atom?q=summon+%23monsterattack"; //what are we looking for
$num_items = 1;
$rss = fetch_rss($url);
$items = array_slice($rss->items, 0, $num_items);

$lastrss = fetch_rss($lasturl);
$lastitems = array_slice($lastrss->lastitems, 0, $num_items);

// Open last logfile as array so we can check for duplicates //$previous_items = file('rsslogs.txt');
echo "...";

foreach ($items as $item) {
$href = $item['link'];
$title = $item['title']; //Really we just care about title but meh
$desc = $item['description']; //just incase we define link and description
$item_checksum = $title; // Use an MD5 hash of the link as unique identifier
// Actually we're not using an MD5 now and I forget why
foreach ($lastitems as $lastitem){
$lasttitle = $lastitem['title'];
}

if ($item_checksum != $previous_items) //if this is a new command
{

$name = strstr($title, "@");

$name = $name." ";

$name = substr($name, 0, strpos($name, " "));

$twitter_message = $name." ROARRRRRRRRRR #MONSTERATTACK";

echo $twitter_message;

if(strlen($twitter_message)<1){ $error=1; } else { $twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); }

$previous_items = $item_checksum;
} }

?>