Auto Retweet Bot for Twitter
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 = "https://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 https://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.
You may also like
22 comments
Leave a ReplyCancel reply
Archives
- April 2025
- March 2025
- February 2025
- November 2023
- January 2023
- October 2022
- August 2022
- April 2022
- March 2022
- January 2022
- July 2021
- October 2020
- August 2020
- June 2020
- January 2020
- November 2019
- July 2019
- December 2018
- October 2018
- September 2018
- August 2018
- May 2018
- March 2018
- February 2018
- December 2017
- September 2017
- June 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- May 2014
- April 2014
- March 2014
- February 2014
- November 2013
- October 2013
- September 2013
- July 2013
- June 2013
- January 2013
- December 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- June 2006
- March 2006
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | |||||
Undo option don’t show when i retweet using the above script.
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.
Somebody might find this script useful when they love to re-tweet any tweets that related to their interests.
Yeah yeah that is just make sense!
Thank you! 😀
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 😀
no problem mas, my pleasure. 🙂
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 😛
walah dari sini ternyata, bot sebangsa markum
have fun!
:d
rekues kingdomofngalkamdulillah!
jeneng bot’e kepanjangan kuwi kang :p
katanya akhir bulan ini udah ga bisa pake “Authorization: Basic “.base64_encode” ya ndal?
trus kalo untuk variasi RTnya si bot piye?
I have updated the post, please read the last paragraph.
Bro, bikin dong versi baru yang pake Oauth
to bad … now old api has deprecated, u need to update with last Oauth ..
regards
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!!!!!!!!
Dear moron, why didn’t you read entire post first? :p
Weh, dah gak fungsi toh
You?re on top of the game. Thanks for sarhing.
worth reading – twitter bots are cool
wah udah gak jalan toh…
hiks.. hiks… hiks…
nice share..
*googling lagi…