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.
lokesh
28/07/2010 — 19:33
Undo option don’t show when i retweet using the above script.
budi
02/08/2010 — 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.
admin
02/08/2010 — 10:59
Somebody might find this script useful when they love to re-tweet any tweets that related to their interests.
budi
02/08/2010 — 18:57
Yeah yeah that is just make sense!
Thank you! ๐
hesahesa
09/08/2010 — 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 ๐
admin
10/08/2010 — 02:21
no problem mas, my pleasure. ๐
ridu
10/08/2010 — 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 ๐
hasant
15/08/2010 — 11:37
walah dari sini ternyata, bot sebangsa markum
have fun!
:d
antobilang
18/08/2010 — 05:01
rekues kingdomofngalkamdulillah!
admin
18/08/2010 — 06:02
jeneng bot’e kepanjangan kuwi kang :p
bunderanhi
29/08/2010 — 22:36
katanya akhir bulan ini udah ga bisa pake “Authorization: Basic “.base64_encode” ya ndal?
trus kalo untuk variasi RTnya si bot piye?
admin
03/09/2010 — 15:22
I have updated the post, please read the last paragraph.
Sastro
04/09/2010 — 05:59
Bro, bikin dong versi baru yang pake Oauth
Buzzknow
10/11/2010 — 01:25
to bad … now old api has deprecated, u need to update with last Oauth ..
regards
BobaJob
11/11/2010 — 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!!!!!!!!
admin
12/11/2010 — 02:39
Dear moron, why didn’t you read entire post first? :p
ArdianZzZ
12/11/2010 — 12:59
Weh, dah gak fungsi toh
Denisha
31/05/2011 — 18:08
You?re on top of the game. Thanks for sarhing.
Mature Woman Dorset
20/09/2011 — 20:09
worth reading – twitter bots are cool
akuadit
30/12/2011 — 13:03
wah udah gak jalan toh…
hiks.. hiks… hiks…
nice share..
*googling lagi…