Building a Twitter Bot
Meet Jason Thorton. To the people who know Jason, he is a successful entrepreneur in San Francisco who tweets 4-5 times a day. But Jason has a secret, he’s not really a human, he’s the product of my simple algorithm in PHP.
Jason tweets A LOT about the word “love” - that’s because Jason actually steals tweets from the public timeline that contain the word “love” and posts them as his own.
Jason also @ replies to people who use the word “love” in their tweets, and asks them random questions or says something arbitrary.
It took me about 3 hours to code Jason, imagine what a real engineer could do with real AI algorithms? Now realize that it’s already a reality. Sites like Twitter are full of side projects, company initiatives, spam bots, and AI robots. When the free flow of information becomes open, the amount of disinformation increases. There’s a real need for someone to come in and vet the people we ‘meet’ on social sites — it will be interesting to see how this market grows in the next year.
The code. Remember, I’m not an engineer by any means. Please excuse the crappy code.
set_time_limit(3600);
$termy = array(
'love',
'hate',
'cant stand',
'wish',
'would',
'sick',
'cried');
$l = rand(0, (count($cr)-1));
$x = rand(0,1);
print "x=".$x;
if($x==1){
posttweet($termy{$l});
} else {
trackandfollow($termy{$l});
}
//get query tweets and follow
function trackandfollow($trm) {
$filename = $trm.".txt";
$handle = fopen($filename, "r+");
$id = fread($handle, filesize($filename));
fclose($handle);
//$trmfull = $trm."+-from%3ARants_and_Raves+-from%3Akish_rss+-from%3TechmemeFH+-from%3Awebnex+-from%3Ajasonthorton";
$jsonurl = "http://search.twitter.com/search.json?lang=en&q=".$trm."&rpp=1";
print $jsonurl;
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
$i = 0;
foreach ( $json_output->results as $results )
{
if ($i == 0) { $last_id = $results->id; }
$i++;
follow($results->from_user);
if($last_id != $id) {
reply($results->from_user,$trm);
}
}
$handle = fopen($filename, "w+");
fwrite($handle, $last_id);
fclose($handle);
}
function reply($username,$trm) {
$cr = array(
'LOL. :)',
'Interesting.',
'Whoa. Thats nuts.',
'Do you live in the Bay Area too?',
'Where do you live?',
'Nice to meet you!',
'Welcome to my Twitter!',
'Welcome to my follow list! :)',
'Hope youre having a great day! :)',
'Youre awesome!',
'Ha! Do you have any kids?',
'I just started Tweeting, how long have you been on Twitter?',
'HA! That\'s awesome!',
'We\re a new site, we just started out. Hope you can give us a try :)',
'No way! That\s crazy...',
'Love readin\' your tweets.',
'Don\'t ever give up! Keep pressin\' on!',
'Just keep goin\', keep pressin!',
'Love your attitude, keep it up!',
'Is Twitter the next big social platform?',
'Do you think Twitter is going to end up being the next IM?',
'Thanks for doin\' what you do!',
'HAHA! That\'s great. ;)',
'Nice to meet here on Twitter!',
'It\'s a lovely day somewhere! ;)',
'Are you new to Twitter?',
'Welcome to Twitter! :)',
'I know exactly how you feel...',
'Huh... not so much',
'I love it too, what did you think?',
'Yup, I use it everyday',
'Did you like it?',
'Havin fun?',
'So much to do...',
'ROFL, yeah.',
'Haha, me too.',
'Nice to tweet you!',
'Cant wait to follow your tweets! Thanks for connecting :)',
'Thanks for following!',
'Love readin your tweets. Keep em coming :)',
'Just keep fighting the good fight!',
);
$l = rand(0, (count($cr)-1));
//$trend = implode(", ",trend(1));
$postargs = 'status='.urlencode('@'.$username.' '.$cr{$l});
print $postargs;
$url = "http://jasonthorton:*********@twitter.com/statuses/update.json";
if (!$curld = curl_init()) {
echo "Could not initialize cURL session.\n";
exit;
}
echo $url;
if (!$curld = curl_init()) {
echo "Could not initialize cURL session.\n";
exit;
}
curl_setopt($curld, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_HEADER, false);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output=curl_exec($curld);
echo "Received data: \n\n$output\n\n";
$pile=json_decode($output,true);
print_r($pile);
return $pile;
}
function print_r_html ($arr) {
?>
print_r($arr);
?>
}
function trend($num) {
$jsonurl = "http://search.twitter.com/trends/current.json";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json,true);
//print_r_html($json_output);
$i = 0;
$results = $json_output['trends'];
for($i=0;$i<=$num;$i++)
{
if ($i <= $num) {
foreach ($results as $key => $value)
$trends[$i] = $results[$key][$i]['name'];
}
}
return $trends;
}
function follow($username)
{
$url = “http://jasonthorton:*********@twitter.com/friendships/create/$username.json”;
if (!$curld = curl_init()) {
echo “Could not initialize cURL session.\n”;
exit;
}
echo $url;
if (!$curld = curl_init()) {
echo “Could not initialize cURL session.\n”;
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_HEADER, false);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output=curl_exec($curld);
echo “Received data: \n\n$output\n\n”;
$pile=json_decode($output,true);
return $pile;
}
function posttweet($trm) {
$jsonurl = “http://search.twitter.com/search.json?lang=en&q=”.$trm.”&rpp=1″;
print $jsonurl;
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ( $json_output->results as $results )
{
$postargs = preg_replace(’#@[^\s]+\s#’, ‘ ‘, $results->text);
$postargs = ’status=’.$postargs;
print $postargs;
$url = “http://jasonthorton:********@twitter.com/statuses/update.json”;
if (!$curld = curl_init()) {
echo “Could not initialize cURL session.\n”;
exit;
}
echo $url;
if (!$curld = curl_init()) {
echo “Could not initialize cURL session.\n”;
exit;
}
curl_setopt($curld, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_HEADER, false);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output=curl_exec($curld);
echo “Received data: \n\n$output\n\n”;
$pile=json_decode($output,true);
print_r($pile);
return $pile;
}
}
?>

Jan 22nd 2010
Super cool! Now let’s see how you built that sucker.
Jan 23rd 2010
FTW
I’m on the way now to make a lady bot, hopefully they’ll meet on line later
AJ
Jan 23rd 2010
Crafty, my friend. … and you make a good point.
Jan 23rd 2010
How did you automate when to run this? Cron job? Did it run on a schedule or random times?
Jan 24th 2010
The bot doesn’t work for me, please take a look at the code again.
Can you make a zip-file for me ?
Thanx,
Michael
Jan 31st 2010
Hi, this is awesome post, just what i was looking for, but -
i think that there is a paste error in this function, is it possible to correct it please :
function print_r_html ($arr) {
?>
}
Many thanks