Archive for September 2009

Build your own retweet / hash bot with #LinqToTwitter

You love twitter? You love Linq too? Then you are gonna adore LinqToTwitter.

LINQ to Twitter is a LINQ Provider for the Twitter micro-blogging service. It uses standard LINQ syntax
for queries and includes method calls for changes via theTwitter API.

LinqToTwitter is one of the most inspiring applications out there made with C# by @JoeMayo. You can download it from here : http://linqtotwitter.codeplex.com/. It is open source and several projects are using it already.

Today I want to show how simple it is to develop your own twitter based application using LinqToTwitter, as for an example we will build a simple retweet / hash bot, like the ones we find on twitter for example hashandroid, hashphp, hashcss and more.

You can use for example such bots to retweet every tweet mentioning your domain name, your own name, your trademark or even your favorite movie. A valuable tool for business too.

Download LinqToTwitter, then start a new Visual Studio solution, we will use C# as the language.

First we need to add a reference to the LinqToTwitter DLL and System.Configuration (Solution Explorer –> References –> Add a Reference).

The code is so simple actually, I will explain as long as we go through the code, you can find the complete source code in the attached file from here.

To be sure our bot won’t retweet tweets that are already processed we will read the ID of the tweet from the configuration file like follows

// Start fetching tweets from the last one we fetched before, to not retweet duplicate tweets
// We do this by searching for tweeting having an ID >= to lastTweetID


saved in the App.config
var lastTweetID = getLastTweetID();


private static string getLastTweetID()
{
return ConfigurationManager.AppSettings["lastTweetID"];
}



Once the last tweet ID retrieved, we fetch all the tweets that have a specified string and are emitted after our lastTweetID:




List<AtomEntry> lstTweets = SearchTwitter(twitterCtx, "martani_net"
, Convert.ToUInt64(lastTweetID));



The function SearchTwitter returns the list of tweets satisfying the criteria,we pass the term to search for and the last tweetID.




private static List<AtomEntry> SearchTwitter(TwitterContext twitterCtx, string searchWrd, ulong lastTweetID)
{
var queryResults =
from search in twitterCtx.Search
where search.Type == SearchType.Search &&
search.Query == searchWrd &&
search.PageSize == 10 &&
search.SinceID == lastTweetID
select search;

foreach (var search in queryResults)
{
return search.Entries.ToList();
}
return null;
}



Now that we have the list of tweets we save the most recent tweet ID so that the next time we fetch only new tweets:




var lastTweet = lstTweets.First();
lastTweetID = lastTweet.ID.Substring(lastTweet.ID.LastIndexOf(':') + 1);

// Save the lastest tweet ID in the App.config file.
saveLastTweetID(lastTweetID);



This is the function that saves the lastTweetID in the app.config file (actually this didn’t work for me!! any help?)




private static void saveLastTweetID(string lastTweetID)
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["lastTweetID"].Value = lastTweetID;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}



Everything is ready now, we have just to retweet the new tweets with a little change in their form :




foreach (var entry in lstTweets)
{
//Console.WriteLine(entry.ID);
string via = " (via @" + GetShortName(entry.Author.Name) + ")";
string contentWithoutHTML = DeleteHTML(entry.Content);
string newTweet = contentWithoutHTML.Substring(0,
Math.Min(contentWithoutHTML.Length, 140 - via.Length))
+ via;

// skip tweets that we already retweeted before
if (AlreadyTwittered(contentWithoutHTML))
continue;

twitterCtx.UpdateStatus(newTweet);
//Console.WriteLine(newTweet);
}



Here we are fetching the user name and storing it in the via variable. we use the GetShortName function to get only the user name and not it’s real name. For example entry.Author.Name returns “martani_net (Martani Fakhrou)” so our function returns only “martani_net




private static string GetShortName(string longName)
{
return longName.Substring(0, longName.IndexOf(' '));
}



Then we get the content of the tweet without any HTML, we use a simple regular expression to delete any html specific tags:




private static string DeleteHTML(string text)
{
Regex reg = new Regex("<[^>]*>");
return reg.Replace(text, "");
}



Then we compose the new tweet which is the content without HTML + the “via (username)” footer. Here we have to be aware that our tweet doesn’t exceed 140 chars which mean the true length of the content can’t exceed 140 – the length of the footer (via @something)



Still one trick to take care of, our retweeted tweets will be fetched also, which means we have to take them away, for this we use the function AlreadyTwittered as follows :




private static bool AlreadyTwittered(string p)
{
// if the tweet ends with ")" and have the string " (via" then
// we might have retweeted it already
// this is a poor cretaria, just for examples here.

if (p.EndsWith(")") && p.IndexOf(" (via") != -1)
return true;
else
return false
;
}



Well this is all, we can now send our new tweet to twitter with the following statement




twitterCtx.UpdateStatus(newTweet);



Of course you have to handle also how this program will execute periodically, each 10 minutes for example.



If you intend to use AOuth then you have to setup your application on twitter to get the secret and API key, otherwise you can use the old authentication system, and yeah LinqToTwitter handle all this for you :).



Download



the source code from here.



I have a lot of tricks to do with LinqToTwitter, and from those, a bot available publically to make users able to set their hashtags or specific words to build their own bot with just few clicks, but I can’t make it to sell an ASP.NET hosting and make my projects real :), any help will be appreciated of course.



Twinq test :



Untitled

Posted in , , , |

Run you own web server using PHP / ASP.NET on IIS7 [Part #1]

These tutorials aims basically to target PHP and beginner ASP.NET developers to show them how to configure, run and make their IIS7 web server serving websites on Internet from their home machines. It's also intended to fill the gap between PHP developers and the non open source products out there, especially the IIS server which a lot of them are not aware of. Also ASP.NET developers will benefit from these tutorials too, because configuring the server affect any web platform running on that server.

Part #1 will be a quick view of how to make IIS7 run your website locally and how to access it from internet. Most of the time, when developing web application, we encounter a lot of problems like timeout requests, malformed HTTP headers and of course execution time and such that we can't test once we develop on a local server. So configuring our machine to be a webhost will be the first step that we will take.

Also, you can use your own web server for testing purposes, developers usually send a copy of their web applications to friends to test it, which is just a bad choice in all sorts of considerations, the best is to access one version of your website running on your own machine like a real website with a special domain name.

IIS7? The "what" and the "why"

IIS stands for Internet Information Service and it's Microsoft’s web server running on windows platforms. IIS7 is the latest version and the most secure, fast, reliable and robust; it ships with Windows Vista, Windows 7 and Windows 2008 Server by default with some limitations according to your windows edition.

I can just say: it is more than great; you want to find out more about it here : http://www.iis.net/ or http://en.wikipedia.org/wiki/Internet_Information_Services

Installing and running IIS7

IIS7 is installed by default on Windows 2008 and some Windows 7 / Vista versions, check the Administrative tools in the control panel to see if there is the IIS manager or not, In case it's not installed, just few clicks will bring it up, follow the tips here : http://learn.iis.net/page.aspx/28/installing-iis-70-on-windows-vista/

To run IIS7 : Start > Control Panel > Administrate Tools > IIS Manager, notice that you must have administrative privileges to do so.

1

This is the IIS manager, where you can configure all the aspects of the server, if you used IIS6 or 5 before, you will find this a little different from the old ones. As you can see there are dozens of settings from Modules, to CGI and port bindings to a lot of other stuff that we will walk through in the next part.

2

To make sure everything was configured correctly, go to http://127.0.0.1 or http://localhost/ on your browser.

4

Making IIS7 available on the web

The easiest way to do so is to find your IP address, use http://whatismyipaddress.com/ for example, and navigate to /">http://<you-ip>/. Chances that you won't get access to your server are very high, first because you may be using a router which blocks entering requests, or your firewall is blocking every request coming from internet.

Configuring your router

The next step is to configure your router to translate the port 80 (http) to your web server in order to handle it, just head to your router configuration page, look for the port translation option and map the port 80 (TCP) to your machines LOCAL IP address with the port 80 also (we will talk about port binding on IIS in the next parts).

3

That's it, nothing more, now typing your own IP address will give you the IIS7 welcome homepage.

Giving your server a domain name

You may prefer accessing your server with a domain name rather than using your IP address (which is a problem as we will see in the following section). You can use your own domain name to make it point to your IP address, or you can use a free, fast, lightweight service aimed for such testing and not persistent cases.

I used http://www.no-ip.com/ for this, they have a good DNS redirection services and it's for free, just create an account, choose your sub domain and point it to you IP, for example "test.no-ip.biz" like in the following picture :

5

Now you can access you web server using that domain name which is better than typing the IP address.

Dynamic IP, The domain name is not pointing to your server anymore!

If your provider assigns a different IP each time to your router then the above method will just break because the domain name will always point to the old IP address. There are a lot of solutions for this, but the best one is to use DynDNS if your router supports this by default.

DynDNS is almost the same thing as the previous service except that it points to the IP the router indicates and updates each time it changes. Once you create your account on DynDNS (Also free, don't worry), enter these details on your router DynDNS configuration section, and you are done:

8

9

Debugging, security and everything else

I am sure that since you are a web developer, you are aware of security and privacy risks, like tracing your IP to know your location, or hacking, brute forcing your server. But if you are aware enough nothing of that will happen, and since this is just a startup server to test with some of your friends, an application like this wouldn’t be a problem after all.

At the end I would like to mention how debugging under the .NET framework and IIS works to give more security options to developers. If you have an error in your website for example, running it locally with show you more details about the error and the configuration of the server, but requesting the web application from outside will just give a simple message indicating the HTTP status of the error like in the following pictures. This is a very useful feature in web development security.

6

7

The next tutorial will be about: how to configure IIS7 and run PHP on top of it.

[Bonus] This is how did I test my IIS from a Windows XP machine connecting to a public wifi, pretty nice isn't it :)

2009-09-21 23.38.52


Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.