Auto Retweet Bot for Twitter

July 24, 2010 | PHP | , , , , ,

This simple tutorial will show you how to create a simple (ro)bot that will re-tweet (RT) any particular tweets related to your search results in Twitter. In this example, my bot will re-tweet any tweet that contain “wahihi” word, so I name it Kingdom of Wahihi.

Before we start, we need following requirements:

  • Twitter accountYes, the bot needs its username and password.
  • PHP interpreterYou can put this script in your own machine or any web hosting services that supports PHP.
  • Cron JobCron is an application to execute a job (application/program) in schedule. Available on mostly any web hosting services.

As replacement for Cron Job, you can manually execute the script by visiting the page (open the script using web browser).

Step one, create the bot. I’ts a PHP script, if you’re an alien and never heard of this programming language before, I recommend you to skip this page and visit my other stories shown in the sidebar.

Let’s start with declaration of username and password of your Twitter account:

<?php
$user = 'kingdomofwahihi';
$pass = 'password';
?>

Then grab the keyword:

<?php
$search = "http://search.twitter.com/search.atom?q=wahihi";
$xml_source = file_get_contents($search);
$x = simplexml_load_string($xml_source);
?>

Above script will fetch search results of “wahihi” from Twitter’s search. The output from Twitter is in Atom (XML) format, so it’s easier for us to read the data usingย simplexml_load_string() function.

Next, extract the data and retweet them:

<?php
foreach($x->entry as $item){
   // part one
   $author_name = $item->author->name;
   list($name, $mbuh) = explode (" ",$author_name);
   $author = trim($name);
   $msg = 'RT @'.$author. ': ' .$item->title;

   // part two
   $out = "POST http://twitter.com/statuses/update.json HTTP/1.1rn"
   ."Host: twitter.comrn"
   ."Authorization: Basic ".base64_encode ($user.':'.$pass)."rn"
   ."Content-type: application/x-www-form-urlencodedrn"
   ."Content-length: ".strlen ("status=$msg")."rn"
   ."Connection: Closernrn"
   ."status=$msg";

   // part three
   $fp = fsockopen ('twitter.com', 80);
   fwrite ($fp, $out);
   fclose ($fp);
}
?>

I divide above lines of code into three parts because they have different task. Part one, the bot will grab author’s name/username and what he/she tweeted then join them into one variable. And add a RT sign as a re-tweet mark.

Part two, we are preparing the variable that we will sent to Twitter’s server, it contains raw HTTP header. I know this bot is gross, but is robust ๐Ÿ˜›

Part three, open socket to Twitter’s server on port 80 then send the raw HTTP header we have prepared on part two. Now check Twitter to see if your bot is successful.

Leave any comment if you have any questions, but question about how to execute the script won’t be answered :p

Update, September 3rd:
This script is no longer working since Twitter changed its authentication method. You can try this script with OAuth authentication, written by Nazieb.

22 comments

This post has 22 comments, great!

  1. lokesh 28/07/2010 at 19:33

    Undo option don’t show when i retweet using the above script.

  2. budi 02/08/2010 at 02:05

    hi! one question, why would anyone want to apply this?
    oh nevermind, you’re just spreading knowledge. I should just ask people who already applied it somewhere.

  3. admin 02/08/2010 at 10:59

    Somebody might find this script useful when they love to re-tweet any tweets that related to their interests.

  4. budi 02/08/2010 at 18:57

    Yeah yeah that is just make sense!
    Thank you! ๐Ÿ˜€

  5. Twitter-bot, iseng-iseng ah xD « #include <prahesakusuma.h> 09/08/2010 at 01:25

    […] goes to : http://sandalian.com/php/auto-retweet-bot-for-twitter.html (saya telan mentah-mentah ini sourcenya […]

  6. hesahesa 09/08/2010 at 01:28

    hey there!
    i use your post (this post) as a reference to my blogpost
    http://hesahesa.wordpress.com/2010/08/09/twitter-bot-iseng-iseng-ah-xd/
    just want to notify you. . .
    thx ๐Ÿ˜€

  7. admin 10/08/2010 at 02:21

    no problem mas, my pleasure. ๐Ÿ™‚

  8. ridu 10/08/2010 at 21:35

    wkwkw.. nice job!!! aku udah coba dan jalan haaha..

    btw command buat cron apa ya? huhu.. gak tau nih cara make cron.. ๐Ÿ™

    bales di sini or via twitter juga oke ๐Ÿ˜›

  9. hasant 15/08/2010 at 11:37

    walah dari sini ternyata, bot sebangsa markum

    have fun!
    :d

  10. antobilang 18/08/2010 at 05:01

    rekues kingdomofngalkamdulillah!

  11. admin 18/08/2010 at 06:02

    jeneng bot’e kepanjangan kuwi kang :p

  12. bunderanhi 29/08/2010 at 22:36

    katanya akhir bulan ini udah ga bisa pake “Authorization: Basic “.base64_encode” ya ndal?

    trus kalo untuk variasi RTnya si bot piye?

  13. admin 03/09/2010 at 15:22

    I have updated the post, please read the last paragraph.

  14. Sastro 04/09/2010 at 05:59

    Bro, bikin dong versi baru yang pake Oauth

  15. links for 2010-10-02 « dupola's Collections 02/10/2010 at 23:04

    […] ยป Auto Retweet Bot for Twitter – Sandalian.com (tags: TwitterBot) […]

  16. Buzzknow 10/11/2010 at 01:25

    to bad … now old api has deprecated, u need to update with last Oauth ..

    regards

  17. BobaJob 11/11/2010 at 17:52

    WHAT A PRICKHEAD sandalian WHY DON’T YOU PULL
    THIS PAGE OR POST AT THE TOP THAT THE SCRIPT
    DON’T WORK ANYMORE…STUPID CUNT!!!!!!!!

  18. admin 12/11/2010 at 02:39

    Dear moron, why didn’t you read entire post first? :p

  19. ArdianZzZ 12/11/2010 at 12:59

    Weh, dah gak fungsi toh

  20. Denisha 31/05/2011 at 18:08

    You?re on top of the game. Thanks for sarhing.

  21. Mature Woman Dorset 20/09/2011 at 20:09

    worth reading – twitter bots are cool

  22. akuadit 30/12/2011 at 13:03

    wah udah gak jalan toh…
    hiks.. hiks… hiks…

    nice share..

    *googling lagi…

 

Leave a comment

Allowed tags are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>