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;
} }
?>
About
Musician, web developer, podcaster, tech enthusiast and soon to be father. I love solving problems, especially if I'm using PHP or another web technology to find the solution. I have over 10 years of web experience using everything from HTML, PHP, Javascript, Flash and even some Java, and I'm always willing to learn something new.
Activity Stream
Mon, 09/06/2010 - 20:02
Tyler checked in at Ivor Wynne Stadium8:02pm#Mon, 09/06/2010 - 03:16
Tyler tweeted "How can I tell if my brain is melting? Ow! It burns. I miss my bed" 3:16am#Sun, 09/05/2010 - 00:09
Tyler tweeted "It's gonna be a long night" 12:09am#
Tyler tweeted "Where the fuck do I start? Top down or bottoms up?" 12:06am#
Tyler checked in at 7 Eleven4:45pm#Tue, 08/31/2010 - 12:25
Tyler tweeted "Server migration is almost complete." 12:25pm#Mon, 08/30/2010 - 22:44
Tyler tweeted "Check out Arcade Fire's new interactive HTML5 music experience, “The Wilderness Downtown” http://t.co/fKRhmTh" 10:44pm#
Tyler checked in at A&W10:15pm#
Tyler tweeted "need lemonade." 6:27pm#Sat, 08/28/2010 - 16:38
Tyler checked in at Starbucks4:38pm#
Find me Here
Archive
- January, 2009 (2)
- February, 2009 (7)
- March, 2010 (1)
- June, 2010 (1)




