Login Form



Creating extended Twitter bot with PHP PDF Print E-mail

If you try to search in the Web info about creating Twitter bot then you will find a lot if articles and blog messages about this. But most of them are just examples of code that posts twitt (message) to Twitter.

This is not enough to create full function Twitter bot.

Good Twitter bot  should: post twitts, subscribe for new users by given subject, somehow make another users to subscribe for needed account.  Actually, last task is most important - grow count of subscribers for some account.

There is example of such bot - Extended Twitter bot with PHP.

I used twitterlibphp to work with Twitter API. It is nice PHP class by Justin Poliey.

I think most useful task of Twitter bot is to increase count of subscribers for some twitter account.

To achieve this i used my own resume after using of twitter. Usually, when some account becomes my subscriber i get notification email and i am going to look what is that account. If that is something i am interested in i subscribe for that account too.

Based on this resume, i created bot that have list of functions:

  • Search for tweets by some keywords and add found twitter account to DB table
  • Get accounts from DB table that i am not subscribed and never was subscribed.
  • Filter accounts with special condition
  • Subscribe for selected accounts
  • Update my twitter status with messages from DB (that is what any twitter bot does )
  • Get list of my subscribers and accounts that i am subscribed on.
  • Select and remove accounts that i subscribed but they didn't subscribed me after some time (it is not good to be subscribed for too many accounts, so remove "bad" accounts).

Filtering of accounts to subscribe

It is easy to search for accounts by keywords. But not all accounts are good for subscription . There are a lot of bots in the Twitter. I read somewhere that 80% of accounts are bots!

It is needed to filter bots accounts and process only "real" accounts - accounts that are used by human .

To solve this task i also used my experience in twitter. I found that real accounts have few subscriptions  (<50) and bots usually have a lot of subscriptions (>100) .

So, there is simple criteria. If account have more then 50 subscriptions then this is bot and account must be ignored. This is not 100% true. But i most cases this works.

Functioning of the bot.

Another important thing for twitter bot (and any other bot) is balance in doing of API requests. It is not good to do request by request. In this case twitter can easy know that this are requests from a bot.

My bot works with scenario that is saved in settings.

Scenario looks like:


Cron task starts the bot each minute. The bot looks in DB for next step in scenario and execute task. Pause means to do nothing.

In next my articles i will describe the code of the bot.


Last Updated on Sunday, 07 March 2010 14:28


#4 Administrator 2011-08-24 06:35
Quoting Francisco Velarde:
Can you help me to code one to response to some keywords?

What exactly do you need the bot does?
#3 Francisco Velarde 2011-08-23 19:26
Can you help me to code one to response to some keywords?
#2 Administrator 2010-09-12 05:09
Quoting Kev:
Are you going to publish the code for this?

I'd really like to do this.


Yes. I want to publish it.
But that bot will need to be modified. Because twitter removed Basic authentication from API. Now user can be authentificated only with oAuth.
I think i will publish that code at next week.
#1 2010-09-11 21:34
Are you going to publish the code for this?

I'd really like to do this.