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: http://dan.cx/B5B. This entry was posted on Saturday, 11th June 2011 at 10:15 PM 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!

53 comments

  1. Avatar for Th33mporor said:
    so THIS is why Navi and Vegeta are trollin' me...... DAMN!
    1. Avatar for 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 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 said:
      Haha, nah, it's all automated :P
  3. Avatar for MapsRus said:
    Nice work! Just got caught by your bot...
  4. Avatar for 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 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 said:
    Haha It actually replied me, and thats how I got to this site just now! :)
  6. Avatar for 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 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 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/twitterautoreply.php 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 said:
      Install the PECL OAuth extension and try again :)
  9. Avatar for 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 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 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_human_boy
        http://twitter.com/jake_magicdog
        :D
        1. Avatar for 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 joserey said:
    That's neat! You got me there with my tweet twitter.com/#!/pinoyeXtension/status/106629938282635265
  11. Avatar for 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 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.
  12. Avatar for Jesse 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 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
  13. Avatar for Belisimo said:
    There is a similar Spartan bot that replies using the 300 dialogue

    http://www.twitter.com/KingLeonidaz_
    1. Avatar for 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!
  14. Avatar for 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 said:
      I don't have time to make a video at the moment, nor are my recording skills good :P
  15. Avatar for Shenron said:
    Well what about a better tutorial? You don't really explain it too thuroughly.
    1. Avatar for Daniel15 said:
      Yeah I don't have time to write one :(
  16. Avatar for David said:
    LOL just got hit myself by it and was like... what the fuck... then saw the link to this... good shit :P
  17. Avatar for 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 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".
  18. Avatar for Zach said:
    Very creative! and a bot that doesn't retweet stuff, but just replies! :)
    1. Avatar for Daniel15 said:
      Haha, thanks! :)
  19. Avatar for markwhitt1 said:
    a bot i was wondering what it was talking about !
  20. Avatar for rgrg said:
    ITS OVER 9000!!!!!!!!!!!!!!!!!!
  21. Avatar for shadowgrin said:
    LMAO. Nappa made my day good sir.
  22. Avatar for OkamiGekigami said:
    I got it but I didn't say 9000, I said 1000 =P
  23. Avatar for 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.
  24. Avatar for ufoman said:
    This is hilarious! LOL XD
  25. Avatar for dirtyred said:
    Well, this smells spammy...
  26. Avatar for Lionx said:
    LOL! I just got caught by this yesterday.. funny. :)

    i want to make some bots too.. lol
  27. Avatar for 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 said:
      Unfortunately I don't really have time to write a tutorial D:
  28. Avatar for MARCOroni said:
    Oh... I was trolled. XD @DBZNappa this is great. =))
  29. Avatar for Moni7775 said:
    LOL it's automated and I wanted to reply :p
    What nine thousannddd :)
  30. Avatar for Moni7775 said:
    LOL it's automated and I wanted to reply :p
    What nine thousannddd :)
  31. Avatar for Bas said:
    Haha, amazing bot. Make it reply to "9001" too :D
  32. Avatar for Emma said:
    I must say, I got a giggle from your WHAT, NINE THOUSAND?! Bot - nice one dude!
  33. Avatar for 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 Daniel15 said:
      Try the instructions in this comment: https://gist.github.com/820281#gistcomment-92314
  34. Avatar for Neil said:
    Great use of a DBZ avatar. Just got caught by your bot.
  35. Avatar for J.D. said:
    Is the Nappa Bot down? It hasn't tweeted in a few days.
    1. Avatar for Daniel15 said:
      It was down, but should be okay now.
  36. Avatar for 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:

Leave a comment

Cancel reply

(required)

(required, but will not be published)