Anatomy of a Twitter Bot

April 22nd, 2008

Update: If you’re here in search of sample code for a Twitter repost bot, I would strongly recommend going to the Anatomy of a Better Twitter Bot post, which has a much-improved iteration of the LOTD bot code.

Update: Since Fred Twittered this post, it has reached a somewhat larger audience than expected and I’ll add an addtional note: there will be some interesting new stuff happening with @lotd. I’ve been chatting back and forth with Daryn Nakhuda, a gentleman and a real programmer, about possibilities, and when you put together his ideas, my ideas, and his ability to write sane, reliable code, cool things should result. Stay tuned.

While the logic of “hide your shame” should dictate that I never, ever reveal the code that runs the Twitter Lyric of the Day Club, there’s been enough interest that I’m just going to suck it up and publish it. For any programmers who end up at this post: yes, the code is a little eccentric, and yes, there are a number of ways that it could fall down and hurt itself. Believe me, I know. That said, here’s the rundown:

Requirements
To run a variation of this @lotd script, you must be able to understand Perl well enough to make some basic modifications to the script, and must be able to set up a simple database table (phpMyAdmin is your friend). In addition, you need a server or account at a Web hosting service that:

Overview
It’s a very simple setup: there’s a single perl script running on my server that gets the replies posted to @lotd via the Twitter API, loads them into a database table, and then republishes the posts in the lotd account (again via the API). It’s scheduled to run once every 15 minutes, around the clock. The script uses the XML::Simple and DBI modules, but doesn’t have any other dependencies.

To run your own bot using this script, you’ll need to:

  1. Create the Twitter account that will be the repost bot.
  2. Create a database table as described in the next section.
  3. Update the script below:
    • The DB connect information for your database. (Lines 21, 43, 75)
    • The Twitter username/password information for your Twitterbot account. (Lines 6, 84)
    • The regex to remove @YourTwitterBot from replies before it reposts them. (Line 18)
  4. Upload the finished script to your server.
  5. Set up a cron job to periodically run your script (@lotd runs every 15 minutes).

…and that’s pretty much it. As I said in my first email to Fred about this, “the hardest part of making something like the @lotd bot is just having the idea — once you’ve got the idea, Twitter makes it easy to build what you’ve got in your head.”

Good luck, have fun, and let me know about anything interesting that you make!

Update: In response to a couple of questions, I guess I didn’t make it explicit—this stuff is released under the Woodie Guthrie license: “This song is Copyrighted in U.S., under Seal of Copyright # 154085, for a period of 28 years, and anybody caught singin it without our permission, will be mighty good friends of ourn, cause we don’t give a dern. Publish it. Write it. Sing it. Swing to it. Yodel it. We wrote it, that’s all we wanted to do.”

Get the Database Table Structure (MySQL CREATE)
Click the link above, copy and paste, and change the table or field names as seems appropriate.

Get the Example Script
Click the link above, copy and paste, and make the updates noted in the post above. If you change the database table or field names, make sure that you also update the script appropriately.

miscTech, socialsoftware, web | Comments | Trackback

  • nice but why we are making a simple service like twitter that hard and diffecult
  • I did not know about twitter bot, before, well thank you for your information.
  • I used to like twitter and now they are just too crowded with people wanted to sell things :/
  • Thanks for the resources. I can use Perl so this is really cool!
  • How can those twitter bots get past the captcha?
  • Sweet.. Thanks for sharing the example script!
  • What a useful post here. Very informative for me..TQ friends...

    Cheers,
    gadgettechblog.com
  • Aneeklive
    I also kind of expect to see Twitter building similar tools at some point, since the ~1,500 API calls/day limit means that third parties can't do group tools that scale very well. Any large group or tool that uses direct messages will burn through those API calls pretty quick. Interesting possibility for Twitter, actually: people can build their own groups using the API, or pay a monthly and use Twitter-native group tools so that (a) no coding is required, and (b) the group can scale since you're not API rate limited.

    Pregnant woman
  • fiberblend
    Thats still the scary old pre-Daryn version that I'm inflicting on an unsuspecting Internet. :)

    I just updated the post to let folks know that you and I are going to be moving @lotd forward -- can I ping you tomorrow to set up some scheming time?

    Fiberblend
  • Aneeklive
    Someone should make a service that creates and manages Twitter bots through a simple web site. I'm sort of surprised it doesn't already exist... then again, maybe it does and I just don't know about it. TwitterFeed is pretty close.

    Katherine Heigl Photos
  • Haroldq
    I agree with Aneeklive. Twitter is a great tool, however without the apps, I believe it would be like any other social networking tool. The apps is what makes it "hip."

    Dream Beds
  • cableandflex
    This is just about the most intelligent comment on Twitter I have ever read. Someone out there really understands twitter in the VC community, I am happy and amazed. This is going to be a massive set of businesses - almost like an open-source listserv. I wonder if Twitter has some juicy account names set up in reserve for itself... it could be like a domain-name spree... and what company wouldn't want this kind of access to its fans? (other than bad companies of course)

    Wiring a plug
  • cableandflex
    twitter is great! simple and easy to use application make things a success.

    i like the applications people are creating around twitter. i would like to have twitter app as private tool to discuss the daily work inside company. email / forums / blog / wiki are not the perfect solution for this....but twitter like app would be great.

    do let me know if you know of any such thing.
    Fused connection unit
  • Twitter is a great marketing viral tool. Easy to use and very good marketing wise for both users and marketers.
  • Twitter is way over cooked, Too much hype face book bebo myspace they will be around for many years to come, When they first com eout it was all big stuff and now its twitter this twitter that big deal.
  • Twitter is too saturated with marketers wanting to sell. It is just an autofollow train where no one gets anywhere and it doesn't serve it's purpose.
  • Twiiter sucks!
  • Amazing work..and to think people were under-estimating its value..:O
  • Amazing work there..Kudos for sharing this out.
    Role Play
  • Thanks! I've just made a twitter bot: http://download11.com/twitter
  • This is really helpful. thanks.
  • Someone should make a service that creates and manages Twitter bots through a simple web site. I'm sort of surprised it doesn't already exist... then again, maybe it does and I just don't know about it. TwitterFeed is pretty close.
  • Jon
    I'm thinking of making a bot for a friend and the information was very helpful. Thanks a lot. GOnna use MT for hosting since that seems easiest. I'll put your referral code so hopefully they hook you up a little bit.
  • Excellent to hear, Greg -- though, d'oh: I should have told you to put 250labs.com as a referral, so I got some free hosting out of it. :)
  • Hi Whit,

    I got your Twitter bot to work! I moved from GoDaddy to MediaTemple. MT seems to be really great. I then uploaded your script and created a new database. All I had to do was config the script and wham-o - it worked!

    Lessons learned: use a real host if you're going to do real web work.

    Thanks for your help,

    Greg
  • Greg -

    I'm hosting this stuff on a MediaTemple gridserver account. While it's not as flexible in many ways as having a dedicated server, I've been very happy with them over the past year or so.

    You might try getting in touch with Scott Schnaars -- he set up a bot using this code as a model and has been working through some issues with getting it running on a GoDaddy hosted account.
  • Hi,

    Which host are you using? I'm using GoDaddy, but I'm having some problems.

    Thanks,

    Greg
  • Thanks for publishing your code. I adapted it into Classic ASP and am now using it to power the @nojf group for people going down to the New Orleans Jazz and Heritage Festival this week.

    Spread the word! :)
  • Thanks for sharing the code, I plan to clean it up a bit and put it to good use.
  • sweeeeet. Thanks for sharing.
blog comments powered by Disqus

Search

Search seamonkeyrodeo

More Quick Links

Categories

The Past Year

Creative Commons License