The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
Twitter.com 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 Twitter
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 "ZetaTwitter.exe".
Example configuration file.
Currently all attributes are mandatory. Adjust the file in the downloaded
archive with the XML/text editor of your choice.
nicely asked to explain my code a little bit, this chapter outlines the
highlights. 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
XmlDocument 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
each for each
<account> node in the configuration file.
Account - The representation of one account. Has an array of
objects for the keywords specified in the configuration file.
The processing is done the
Program class. Using the
(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
Account 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
are outlined in the online documentation of tweet#:
- Create a request of what you want to get and specify your account login
- Retrieve the reply from Twitter and further process the reply.
Example for step 1:
var twitter =
.AuthenticateAs( account.UserName, account.Password )
.Containing( keyword )
response = twitter.Request();
Here in this example, I authenticate an account with username and password,
and then query for tweets with the given keyword. The result is then returned a
string containing the JSON
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;
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
LOG4NET 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
- Download the source code and enhance it the way you want it.
- Tell me (down here in the comments 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!
Updated to latest tweet# library.
Added section that explains the internal code structure and some basic ideas behind the code.
Added to Microsoft Codeplex and to Google code
Since at least four people (see their 1-vote comments below) seem to strongly dislike me and/or my article, let me summarize the motivation for this article:
- I searched Google for Twitter keyword auto-follow tools.
- I searched again.
- I found one tool, Twollow.com, which is usable only when paing money.
- I decided to write my own, as-fast-as-possible. So I used a third-party-library tweet#.
- I wanted to give others that want such a tool the opportunity to not start from scratch but to use my (thin) layer on top of tweet# as a foundation.
- I published the code as an article here at CodeProject.com.
This said, I do know:
- Neither the article nor the code is rocket science.
- For me, this article is similar to all other articles I wrote, but somehow I must be mistaken.
- I do love sharing and using code.
- I also do love my company (really!) so I put hyperlinks to products of us in my profile section that appears under every article I write, not just this one.
Thanks for reading!