Auto Follow Keywords On Twitter.
In this article I will introduce you a small console application written in C# and .NET 3.5 that enables you to automatically follow Twitter users based on keywords they use in their tweets.
If you don't know Twitter, you may look at their website
or read the Wikipedia
article about Twitter.
The motivation for developing this tool was to enable some of our company's Twitter accounts to automatically follow other Twitter users based on the keywords they post.
E.g. we maintain a Twitter account
for our CMS "Zeta Producer Desktop". Now I wanted to do
a search for "cms" on Twitter
and follow all users (or the first n ones) that were displayed in the search result.
Even though a similar service exists with Twollow
, they charge for their services. Since I wanted a free version (although I do have way less features than they have implemented), I started developing Zeta Twitter.
The application is a .NET 3.5 console application that is intended to be used as a scheduled task in the Windows Task Scheduler. You would e.g. configure the task to run every 30 minutes.
Example screenshot of the output of the ZetaTwitter.exe console application.
I am using the free library tweet#
to communicate with the
You can add multiple Twitter accounts and for each account multiple keywords to check. The whole configuration is done within a separate configuration file called "ZetaTwitterConfiguration.xml". This file must be stored in the same folder as the executable
Example configuration file.
Currently all attributes are mandatory. Adjust the file in the downloaded archive with the XML/text editor of your choice.
This chapter outlines the highlights of the code. Again the warning that none of the code is rocket science.
The configuration is being stored in an XML file (as stated above) and being read into two classes by simple
method calls. I do read it manually, not with any XML to class mapping.
The classes are:
- Configuration - The root class which has an array of
Account objects each for each
<account> node in the configuration file.
- Account - The representation of one account. Has an array of
string objects for the keywords specified in the configuration file.
The processing is done the Program
class. Using the
property (which is implemented with a mutex) to quit itself if it is already running. So it is a single-instance console application.
Reason for that is that depending on the configuration file size, one run may take rather long and I wanted to avoid that the Windows Task Scheduler fire multiple instances. Twitter only allows a limited number of requests per IP address per day, so you better
play nice here.
The Main method consists of two nested loops, the outer iterates through all
objects of the configuration, the inner through all keywords of the currently processed account.
Using tweet# is interesting: They heavily rely on .NET 3.5
to implement most of the functionality. The
basic steps are outlined in the online documentation
Example for step 1:
- Create a request of what you want to get and specify your account login credentials.
- Retrieve the reply from Twitter and further process the reply.
var twitter =
.AuthenticateAs( account.UserName, account.Password )
.Containing( keyword )
response = twitter.Request();
Here in this example, I authenticate an account with user name and password, and then query for tweets with the given keyword. The result is then returned a string containing the
Example for step 2:
var searchResult = response.AsSearchResult();
The response is transformed into a search result collection. Here you have the ability to e.g. iterate through the results and further process them:
foreach ( var status in searchResult.Statuses )
var userName = status.FromUserScreenName;
// ... do something with the user name ...
Depending on the kind of query, different object types are being returned. Again, please see the
tweet# reference Wiki
for full options.
Currently all output is written to console window only. Ususally next steps would be to use a logging framework like
instead to log to various locations like e.g. e-mail or log files to get notified when something goes wrong.
Current state of the tool
The tool was a quick development of approximately 2 hours. It currently does exactly what I wanted it to do. Proably It does not even have any of the features that you want it to have.
Here you have at least two options:
- Download the source code and enhance it the way you want it.
- Tell me (preferable in the
Discussions section) which features are missing that I should add.
Of course option 2. would be helpful to me when enhancing.
As usual, I will enhance, extend and correct the tool over the next weeks and months. Keep the feedback coming!
- 2009-11-06 - Updated to latest tweet# library.
- 2009-06-26 - First version.