So I thought I'd finally write a little blog post about a Twitter bot I made a while ago. A few people emailed me asking for the source code, so I had previously posted about it on webDevRefinery, but never on my own blog. Basically all the bot does is search for whenever people mention "over 9000" or "over nine thousand", and replies with "WHAT, NINE THOUSAND?!". Pretty simple, but I wanted to learn about using the Twitter API. It seems to have inspired the creation of other Twitter bots, like AnnoyingNavi and The Spacesphere, which I think is pretty cool. 😃.

The source code is available as a Gist on Github. It is written in PHP and requires the PECL OAuth extension to be installed. I think it's a pretty good example of a simple "search and reply" Twitter bot, that could easily be extended to do more useful things.

Until next time,

— Daniel

Tags awesome

Short URL for sharing: https://d.sb/B5B. This entry was posted on 11th June 2011 and is filed under PHP, Programming. You can leave a comment if you'd like to, or subscribe to the RSS feed to keep up-to-date with all my latest blog posts!

Comments

  1. Avatar for Th33mporor Th33mporor said:

    so THIS is why Navi and Vegeta are trollin' me...... DAMN!

    1. Avatar for Daniel15 Daniel15 said:

      LOL, I'm only responsible for the DBZNappa one. The Navi one is awesome, but (unfortunately) not my creation :P

  2. Avatar for Matheus Matheus said:

    Ah... I thought you stalked lots of Twitter pages. I even replied a few times, saying that the scouter must be broken.

    1. Avatar for Daniel15 Daniel15 said:

      Haha, nah, it's all automated :P

  3. Avatar for MapsRus MapsRus said:

    Nice work! Just got caught by your bot...

    1. Avatar for mmmcookie414 mmmcookie414 said:

      Me too! I think I was the last person who got tweeted at the time of posting this.

  4. Avatar for someone someone said:

    Great. Now we're gonna get Twitter spam!

    Thanks very much for your 'experiment'!
    AnnoyingNavigation is already harassing me with stupid tweets, just because I tweeted something about Zelda.
    And guess what? I have to do something to stop receiving crap I never solicited in the first place!

    You are officially a Twitter dickhead.

    1. Avatar for Michael Michael said:

      Someone doesn't get jokes. Really get off your high horse and find the funny side in shit, he already said he didn't write navi.

      get over yourself.

  5. Avatar for SmiVan SmiVan said:

    Haha It actually replied me, and thats how I got to this site just now! :)

  6. Avatar for DavefromTWJ DavefromTWJ said:

    I rode my bicycle over 9000 miles around the United States and all I get is a bot reply? haha

  7. Avatar for ArthurBarbaro ArthurBarbaro said:

    @someone: Haha! It's funny because it's his fault now, because he posted something so easy to make, and that was probably already being used by spammers nonetheless! <laugh.gif>

  8. Avatar for Hidde Hidde said:

    Hi,

    I'm just playing around with this script and I got a error when I run this on my server.

    "PHP Fatal error: Class 'OAuth' not found in /chroot/home/xxx/domain.com/html/folder/twit... on line 19"

    Running from shell with package pcre-6.6-6.el5_6.1.x86_64 installed and updated. Any idea why I get this error?

    1. Avatar for Daniel15 Daniel15 said:

      Install the PECL OAuth extension and try again :)

  9. Avatar for Kelly Mears Kelly Mears said:

    I'm noticing that the example script matches not just exact strings, but any strings that contain all the elements specified in the $term..

    So if I'm searching for "NBA JAM" (just an example), it matches "I love playing NBA JAM", but it also matches "Gotta jam for now, NBA game on in a bit."

    How can I specify that I want the string exactly?

    Thanks for everything!

    1. Avatar for Daniel15 Daniel15 said:

      The search just uses Twitter's search API and returns the results exactly as Twitter returns them. Try putting the term in quotation marks, something like:
      $twitter->addReply('"NBA JAM"', 'Reply here');

      Let me know if that works :)

      1. Avatar for Kelly Mears Kelly Mears said:

        Seems to have worked with the quotes!

        There are a bunch of things I want to try with this script-- I'll post back here as I figure stuff out..

        Have you noticed that if you post to the timeline that is connected to the app, it seems to mess with the since_id parameter? For instance, if I retweet to one of my followers the last "batch" of users gets hit for a second round of auto-replies.

        http://twitter.com/finn_hum...
        http://twitter.com/jake_mag...
        :D

        1. Avatar for Daniel15 Daniel15 said:

          That is very weird! Posting shouldn't affect the since_id as the since_id is only used for the search, and is read from a file every time the script runs. I've posted to the DBZNappa account and it hasn't reposted any auto-replies. Very weird.

  10. Avatar for Mons Mons said:

    When i start bot

    ========= 2011-09-01 8:14:12 PM - Запущен ========= Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in /usr/home/test/data/www/twitter/twitterautoreply.php:101 Stack trace: #0 /usr/home/test/data/www/twitter/twitterautoreply.php(101): OAuth->getRequestToken('https://api.twi...') #1 /usr/home/test/data/www/twitter/twitterautoreply.php(51): TwitterAutoReply->doAuth() #2 /usr/home/test/data/www/twitter/index.php(10): TwitterAutoReply->run() #3 {main} thrown in /usr/home/test/data/www/twitter/twitterautoreply.php on line 101

    PECL OAuth extension installed

    1. Avatar for Daniel15 Daniel15 said:

      This means that your authentication data is incorrect. Check your four authentication values (consumer key, consumer secret, token and secret) are all correct and don't have any spaces before or after them.

  11. Avatar for Jesse Watson Jesse Watson said:

    Well, I'm glad you explained that it was an attempt to test a bot, I spent hours trying to talk to @DBZNappa and all I got was 9000.

    1. Avatar for Daniel15 Daniel15 said:

      Hahaha, yeah, it's all automated. It won't say anything other than the automated responses, unless I log in and post something using its account :P

    1. Avatar for Daniel15 Daniel15 said:

      Yeah, I saw a blog post by the person that made that bot. They said that they were inspired by DBZNappa. I'm happy that people are being inspired by some of my work, even if it is silly things like this!

  12. Avatar for Shenron Shenron said:

    any chance you will ever make a video explaining this a little better, i'm having a little problem comprehending a few things in the creation of this.

    1. Avatar for Daniel15 Daniel15 said:

      I don't have time to make a video at the moment, nor are my recording skills good :P

  13. Avatar for Shenron Shenron said:

    Well what about a better tutorial? You don't really explain it too thuroughly.

    1. Avatar for Daniel15 Daniel15 said:

      Yeah I don't have time to write one :(

  14. Avatar for David David said:

    LOL just got hit myself by it and was like... what the fuck... then saw the link to this... good shit :P

  15. Avatar for tba tba said:

    I am trying to run the script, but I get the following error:

    "PHP Fatal error: Class 'OAuth' not found in /var/www/files/bot/TwitterAutoReply.php on line 22"

    What am I doing wrong?

    1. Avatar for Daniel15 Daniel15 said:

      You need to install the PECL OAuth extension. On a Linux system, you should be able to do this by running "pecl install oauth".

  16. Avatar for Zach Zach said:

    Very creative! and a bot that doesn't retweet stuff, but just replies! :)

    1. Avatar for Daniel15 Daniel15 said:

      Haha, thanks! :)

  17. Avatar for markwhitt1 markwhitt1 said:

    a bot i was wondering what it was talking about !

  18. Avatar for rgrg rgrg said:

    ITS OVER 9000!!!!!!!!!!!!!!!!!!

  19. Avatar for shadowgrin shadowgrin said:

    LMAO. Nappa made my day good sir.

  20. Avatar for OkamiGekigami OkamiGekigami said:

    I got it but I didn't say 9000, I said 1000 =P

  21. Avatar for Alex Alex said:

    As these things happen, I found myself in need of a Twitter bot that would reply "WHAT?! NINE THOUSAND?" to any mention of "9000" on Twitter.

    Thank you for writing this, it saves me from doing so. I'd be interested to hear how you managed to deduce my need some six months before I myself became aware of that need, but this can wait for another time.

  22. Avatar for ufoman ufoman said:

    This is hilarious! LOL XD

  23. Avatar for dirtyred dirtyred said:

    Well, this smells spammy...

  24. Avatar for Lionx Lionx said:

    LOL! I just got caught by this yesterday.. funny. :)

    i want to make some bots too.. lol

  25. Avatar for Matheus Matheus said:

    you could create a tutorial with pictures step by step, never stirred in programção and would like to create a bot

    1. Avatar for Daniel15 Daniel15 said:

      Unfortunately I don't really have time to write a tutorial D:

  26. Avatar for MARCOroni MARCOroni said:

    Oh... I was trolled. XD @DBZNappa this is great. =))

  27. Avatar for Moni7775 Moni7775 said:

    LOL it's automated and I wanted to reply :p
    What nine thousannddd :)

  28. Avatar for Moni7775 Moni7775 said:

    LOL it's automated and I wanted to reply :p
    What nine thousannddd :)

  29. Avatar for Bas Bas said:

    Haha, amazing bot. Make it reply to "9001" too :D

  30. Avatar for Emma Emma said:

    I must say, I got a giggle from your WHAT, NINE THOUSAND?! Bot - nice one dude!

  31. Avatar for Naveed Abbas Naveed Abbas said:

    Hi Daniel
    Hope you are doing well.
    o am new to twitter api and want to implement twitter bot. i just want how to install pecl package i have used this bot with tmhoauth but not working for me :( its just search for tweets but not reply for them because it gives error on this code
    public function setToken($token, $secret)
    {
    $this->_oauth->setToken($token, $secret);
    }
    can you please tell me some replacement code for this?
    thanks

      1. Avatar for mk1 mk1 said:

        in TwitterAutoReply.php you also need to make the following changes

        Comment out $this->_oauth->setToken($token, $secret); and add the changes so the function looks like below:

        public function setToken($token, $secret)
        {
        //$this->_oauth->setToken($token, $secret);
        $this->_oauth->config['user_token'] = $token;
        $this->_oauth->config['user_secret'] = $secret;
        }

  32. Avatar for Neil Neil said:

    Great use of a DBZ avatar. Just got caught by your bot.

  33. Avatar for J.D. J.D. said:

    Is the Nappa Bot down? It hasn't tweeted in a few days.

    1. Avatar for Daniel15 Daniel15 said:

      It was down, but should be okay now.

  34. Avatar for Nick Nick said:

    Lol i was watching syndicates livestreams and he wanted 9000 likes and everyone got spammed i legitly thought you were doing it yourself then i saw bot and i was like woow q:

  35. Avatar for Jon Jon said:

    Alright. When I run the script it will ask me to authorize the application which is fine but once I authorize the application, it will just ask me to authorize it again the next time it runs. Any thoughts on this?
    I'll post a screen shot of the issue.
    http://i.imgur.com/S5i6g.png

    1. Avatar for Daniel15 Daniel15 said:

      Are you sure you entered the OAuth details correctly?

  36. Avatar for Jon Jon said:

    Alright, I'm getting this error Now:

    ========= 2012-06-11 10:14:34 AM - Started =========
    Performing search for "based god"... Done, 15 results.
    @BasedTrillness said: oh my god based god
    PHP Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in /var/www/Twitterbot/twitterautoreply.php:82
    Stack trace:
    #0 /var/www/Twitterbot/twitterautoreply.php(82): OAuth->fetch('https://twitter...', Array, 'POST')
    #1 /var/www/Twitterbot/twitterautoreply.php(68): TwitterAutoReply->sendReply(Object(stdClass), 'SWAG http://i.i...')
    #2 /var/www/Twitterbot/example.php(12): TwitterAutoReply->run()
    #3 {main}
    thrown in /var/www/Twitterbot/twitterautoreply.php on line 82

  37. Avatar for jujuwoot jujuwoot said:

    Do you control him because if he's a bot he couldn't follow someone :P

    1. Avatar for Daniel15 Daniel15 said:

      Yeah, I do control him :P

  38. Avatar for danny danny said:

    so what was the navi thing?

  39. Avatar for Koray Koray said:

    How did you manage to work this bot all the time ?
    is cron job ?

    1. Avatar for Daniel15 Daniel15 said:

      Yeah, it's a cron job that runs every 15 minutes.

  40. Avatar for ensrsln ensrsln said:

    my robot is a suspend by twitter :S ?
    help please.

    1. Avatar for Daniel15 Daniel15 said:

      Yeah, this seems to happen sometimes. I guess I've been lucky with mine! Other people seem to have had their suspended :(

  41. Avatar for Tweakfix Tweakfix said:

    Your bot caught me today, and I even replied... lol I so love him.

    1. Avatar for Daniel15 Daniel15 said:

      Hahaha I'm glad. xD

    2. Avatar for Daniel15 Daniel15 said:

      Hahaha I'm glad. xD

  42. Avatar for @MsAnimeBug @MsAnimeBug said:

    I always get a reply from ur bot.. it's pretty awesome ^_^ tried having a convo w/it, no reply. LOL wish I could get my profile to autoreply to spammers.. I hate em! Any chance you'd know how to do that?

  43. Avatar for Swain Swain said:

    @DBZNappa has reached 10k+ followers! You guys know what that means...
    IT'S OVER NINEDOUZAND.

    1. Avatar for Daniel15 Daniel15 said:

      WHAT? NINE THOUSAND?! :P

      1. Avatar for Mannie Mannie said:

        hey, can you help me get a bot thingy for my twitter?

  44. Avatar for Eli Eli said:

    Hahaha, I love this thing. It's still goin strong.

  45. Avatar for Dario Dario said:

    Absolutely brilliant! I loved that unexpected response! LMAO!

  46. Avatar for Kevin Kevin said:

    this is my favorite thing

  47. Avatar for neoinr neoinr said:

    Slightly curious, how is it that DBZNappa has not been suspended for policy violation?

    1. Avatar for Daniel15 Daniel15 said:

      To be honest, I have absolutely no idea. I guess I'm just lucky? Hahaha

  48. Avatar for Frank d Frank d said:

    It works, kinda. I'm getting this error
    PHP Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 403, expected HTTP/1.1 20X or a redirect)' in /path/to/script/script.php:80

    Any ideas?
    Your help is appreciated.
    Thanks in advance

    1. Avatar for Daniel15 Daniel15 said:

      Interesting... Not sure why you'd be getting that. Are you sure your OAuth details are correct?

  49. Avatar for Mikey Mikey said:

    Nice stuff - thank you for the share. Do you think you will be updating your php twitter script anytime soon? Do you think you could build something like this autotwbot.com? Let me know - thanks.

  50. Avatar for Ashley Elias Ashley Elias said:

    thats fuckin cool :D

  51. Avatar for Calvin Calvin said:

    I got this error,

    #!/usr/bin/php ========= 2012-10-04 1:08:44 PM - Started ========= Performing search for "over 9000"... Done, 15 results. @doM_CraZzy said: Over 9000
    Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in C:\xampp\htdocs\TwitterAutoReply.php:82 Stack trace: #0 C:\xampp\htdocs\TwitterAutoReply.php(82): OAuth->fetch('https://twitter...', Array, 'POST') #1 C:\xampp\htdocs\TwitterAutoReply.php(68): TwitterAutoReply->sendReply(Object(stdClass), 'WHAT?! NINE THO...') #2 C:\xampp\htdocs\example.php(12): TwitterAutoReply->run() #3 {main} thrown in C:\xampp\htdocs\TwitterAutoReply.php on line 82
    =========================

    Can you help me, thanks

    1. Avatar for Daniel15 Daniel15 said:

      Make sure you set your Twitter application to be read and write (not read-only)

  52. Avatar for Calvin Miller Calvin Miller said:

    I got the following error,

    Can help me fix it ?

    thanks

    ==================================================

    #!/usr/bin/php ========= 2012-10-04 2:22:04 PM - Started ========= Please navigate to the following URL to get an authentication token: http://twitter.com/oauth/au... Once done (and you have a PIN number), press ENTER.

    Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\xampp\htdocs\TwitterAutoReply.php on line 112

    Warning: fread() expects parameter 1 to be resource, string given in C:\xampp\htdocs\TwitterAutoReply.php on line 112
    PIN Number:
    Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\xampp\htdocs\TwitterAutoReply.php on line 115

    Warning: fread() expects parameter 1 to be resource, string given in C:\xampp\htdocs\TwitterAutoReply.php on line 115

    Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)' in C:\xampp\htdocs\TwitterAutoReply.php:119 Stack trace: #0 C:\xampp\htdocs\TwitterAutoReply.php(119): OAuth->getAccessToken('https://twitter...', '', '') #1 C:\xampp\htdocs\TwitterAutoReply.php(58): TwitterAutoReply->doAuth() #2 C:\xampp\htdocs\example.php(12): TwitterAutoReply->run() #3 {main} thrown in C:\xampp\htdocs\TwitterAutoReply.php on line 119

    1. Avatar for Daniel15 Daniel15 said:

      What version of PHP are you using?

  53. Avatar for Guest Guest said:

    Why did you pick that number?

  54. Avatar for WHAT? NINE THOUSAND?! WHAT? NINE THOUSAND?! said:

    dude your bot makes me laugh so hard hahaha, congrats!

    1. Avatar for Daniel15 Daniel15 said:

      Glad you like it.

  55. Avatar for Mike Mike said:

    I wish I was a lucky as you are with not being caught by the Twitter police! The majority of the bots that I make are suspended within a week. This one and the Yoda one that has something like 780,000 tweets just blow my mind!

    1. Avatar for Daniel15 Daniel15 said:

      It's crazy right? Haha. Maybe making a bot that replies just a few times per hour would make it less likely to be suspended.

  56. Avatar for Thought-Criminal Thought-Criminal said:

    I'll give you over 9000 dollars to install this on my computer. P.S. LOVE this!

  57. Avatar for Dylan Dylan said:

    "which I think is pretty cool. :)"

    No, not cool. Very annoying actually. Get a life, dude

    1. Avatar for Death Death said:

      Prolly has one unlike yourself

  58. Avatar for vinicius vinicius said:

    Friend, tcan you show me the command line to execute the cron?

    I set the parameters to twice/hour at cpanel, but I don't know the comand line, this shoul looks like this: php -q /home/USER/public_html/bot/example.php ???

    1. Avatar for Daniel15 Daniel15 said:

      Yeah, that looks correct. Add " > /home/USER/public_html/bot/log.txt 2>&1" to the end so the output will go into a file called "log.txt". Check the log.txt after a while to see the output from the cron job.

  59. Avatar for Guest Guest said:

    @daniel I am very interested with your apps, can you teach me how to set up this twitter application? I am willing to pay for your service if your are willing to teach me how this work. :) email me @ ditzemojon@gmail.com

  60. Avatar for Doom Bringer Doom Bringer said:

    @daniel I am very interested with your apps, can you teach me how to set up this twitter applicationhttps://twitter.... I am willing to pay for your service if your are willing to teach me how this work. :) email me @ ditzemojon@gmail.com. Thanks! I'll wait for your email......

  61. Avatar for kentonecloude kentonecloude said:

    @daniel @DBZNappa I am very interested with your apps, can you teach me how to set
    up this twitter application? I am willing to pay for your service if
    your are willing to teach me how this work. :) email me @
    ditzemojon@gmail.com

  62. Avatar for Francis B. Smith Francis B. Smith said:

    Hey could you teach me how to successfully run this scrip in layman's language. I am not a programmer.

  63. Avatar for Joe Rea Joe Rea said:

    I seriously cried with laugher when this hit. I told co-workers. This was fantastic.

  64. Avatar for ElliottBelardo ElliottBelardo said:

    The twitter bot should have commands like stats and ignore.

  65. Avatar for Traceguy Traceguy said:

    Dude its awesome!!! In thought it was awesome seeing the name and phase repeated to someone talking about mine craft... I soon realized it was a bot, as In was replying to at least a hundred people a minute

  66. Avatar for aam aam said:

    hey daniel.. is this affect with the change of twitter API next month? Thanks..

    1. Avatar for Daniel15 Daniel15 said:

      Not too sure... I'll have to look at the code again and see.

      1. Avatar for Guest Guest said:

        i follow you everywhere just to keep me update to the new code.. :D

        Thanks again.

  67. Avatar for Katlego Matlala Katlego Matlala said:

    Dude thats annoying but pretty cool...

  68. Avatar for David Martinez David Martinez said:

    WHAT? NINE THOUSAND?! :D

  69. Avatar for Geovedi Geovedi said:

    Hey Sir,
    Whats the version PHP i need to Install it?
    What any Free Hosting to install it sir?

    1. Avatar for Daniel15 Daniel15 said:

      I'm not sure about free hosting, I haven't used shared hosting for a long time. You'll need SSH access and not all hosts provide it.

  70. Avatar for Pawkeshup Pawkeshup said:

    This bot just made my day.

    1. Avatar for uptocloud uptocloud said:

      hey, I know some html & css only. could u plz show me how to make the bot work (I have a godaddy account with cpanel). my email: justin-farmer@yandex.com

  71. Avatar for William Donald Wolfe William Donald Wolfe said:

    This bot of yours has brought it to my attention that you should program a bot that trolls James Iha

  72. Avatar for Jecs9 Jecs9 said:

    Hello, I tried to make an auto reply bot like this one but after the first 13 replies I got the account blocked the reason was replying to people too often, or something like that, how did you avoid that?

  73. Avatar for Norma Stitz Norma Stitz said:

    You fucker....I nearly replied to the tweet. Good work sir!

  74. Avatar for Ishi Ishi said:

    Hey Daniel, can you please make a version of this for hosting site user like me. So our bot online 24/7 even our computer is off.

    1. Avatar for Daniel15 Daniel15 said:

      You can already use it on a hosted server as long as you have SSH access. If you have cPanel or similar, you could probably set it up as a cronjob via that, but I haven't done that in ages.

      1. Avatar for Guest Guest said:

        I just want to let you know that im being spammed by your bot, he sent me 4 messages (every 5minutes) it is supposed to happen ? Do i need to block him ?

  75. Avatar for uptocloud uptocloud said:

    hey Daniel, I know some html & css only. could u plz show me how to make
    the bot work (I have a godaddy account with cpanel). my email:
    justin-farmer@yandex.com

  76. Avatar for Anthony Anthony said:

    Hi Daniel..

    Can it also work for shared hosting like Hawkhost? I don't know exactly if it has something like PECL OAuth or SSH Access.. THanks in advance..

  77. Avatar for ToverlandCraft Bot ToverlandCraft Bot said:

    how do i use this? what do i need? how do i install it?

  78. Avatar for K.K. Computers K.K. Computers said:

    hi, i want similar site like tweetpi.com can anyone give me?
    urgent requirement

  79. Avatar for Neo Neo Neo Neo said:

    We sell the Best Mosque Carpets all over UAE in the best quality and cheap rates. We have a large number of variety in carpets and a Discount of 20%.