My PhotoBruno Brás Silva

09, April 1986

info@brunosilva.net

Sintra Lisbon Portugal

Category: Programming

Windows Live Agents - Notifications and Automated Messages

Posted on 7:59pm 7/24/2008 by Bruno Silva in Programming, Web, Windows Live

I’ve been writing about Windows Live Agents recently, as you recall. Finally I had the time to leave some more tips.

First Goal: Trigger a notification (procedure that is scheduled to run some time after it has been triggered and which generate messages to the current user) based on a timer event. The notification will be triggered by the current user.

How to achieve this goal?

The code bellow is contained in the Main.ddl file. First of all I declare a global variable to count the number of notifications that were made to a specific user (counter1). Then I declare a procedure that increments the counter, sends the counter value to the user as a friendly message (Notify), and then the magic happens! :)

I declare a notification, which is composed by a time interval and the script that will be executed, in the example bellow the time interval is 10 seconds and the script to be executed is a call to the same procedure.

The usage of this recursive approach makes the notification to run every 10 seconds starting from the first call to the Notify procedure. The fist call is made when the user types “notify” and sends this message to our agent. Pretty simple.

Second Goal: Start a conversation with an user

If you want to start a conversation with a user, instead of reacting to the user input, BuddyScript allows you to achieve that objective. But be careful about the License Agreement and make sure you read it before you start developing a agent with this kind of behavior. In spite of this issue I’ll show an example for educational purposes only. ;)

How to achieve this goal?

While declaring a procedure you can add the startup keyword to run this procedure when the agent is loaded, and you can specify a time interval between calls to this procedure. In the example bellow we have a  timer-based procedure running every one minute since the agent is loaded.

To start a conversation with a user you can use the initiate command, which receives a user id (e-mail) and a procedure name to be called (this command can be used for other purposes, check the documentation in MSDN). The engine will start a conversation with the requested user and call the procedure in that user’s context (this way only that user will receive the messages sent by this call). You can initiate conversations with several users, just add several lines like the last one in the example above.

Blog - Archive pagination fixed

Posted on 8:02pm 7/08/2008 by Bruno Silva in Programming, Software, Web

I had a bug in pagination when navigating through categories or dates :(

It was due to a  customization I made sometime ago to make Wordpress 2.5 work o a IIS web server.

Instead of

$_SERVER[’REQUEST_URI’]=’/page/1′;

It must be

if($_SERVER['REQUEST_URI']=='')

$_SERVER[’REQUEST_URI’]=’/page/1′;

Shame on me :-P

Windows Live Agents - Data Sources and Input Matching

Posted on 4:59pm 7/07/2008 by Bruno Silva in Programming, Web, Windows Live

After I wrote about getting started with Windows Live Agents, it’s time to go deeper into BuddyScript, the script language that is used in agents development.

To make your code easier to manage and even to make it reusable you can create package files. These are files that aggregate functions, procedures and data sources that are strongly connected. These packages can be referenced in your scripts using the package statement in the beginning of your script file. To create a new package right click you solution and add a new item based on the PKG File template. The name you give to your package file will be also the package name.

Windows Live Agents - Packages

I’ve created a package named DataSources.pkg in which I will write sample code related to data sources usage. You can have several kinds of data sources which are listed in the official documentation. Today I’ll talk about 2 of them. First of all the HTTP data source. It Allows you to download data from the web to use in your scripts. When you declare a data source you must give it a name, return type and if you wish you can add parameters. The example bellow is called GetRSSFeedDataSource, has no parameters and the result type is composed by three fields: title, description and link of a RSS feed entry.

In the data source body I define the source file on the web, the timeout for the HTTP request and the expiration time (for caching purposes). You can even define HTTP headers like the user-agent in the example bellow. Now, lets take a look at the source code.

Windows Live Agents - HTTP Data Source

The data that is downloaded by this HTTP data source is parsed using a simple xml filter (there are other like script-based, XSLT, etc). This kind of filter allows you to filter and format your data source results. In this case you can dive into the returned XML and return particular parts of it. This data source only returns the first entry on my RSS feed, but as you can see in the commented code, you can iterate over the XML using loops.

Now lets see an example of usage of my data source.

Windows Live Agents - HTTP Data Source

I defined a procedure where I call the data source, extract the title and link, and send it to the default output device (which will be the Windows Live Messenger conversation window).

Another nice kind of data source that can be configured and used is based on SOAP web services. The example bellow uses a silly web service that I wrote in .NET that sums two numbers and return the result. This data source has 2 parameters (the operands) that are used as input to the web service. I had to define the proxy parameter (URL of the web service), the name of the operation to be performed (and the respective action) and also the namespace of the web service.

Windows Live Agents - SOAP Webservices Data Sources

SendSum() is a simple procedure that outputs the sum of two parameters. I’ll use it later. The demo web service is packaged with the rest of the project that I made as an example for this post. Download project source code and take a look at it.

Now that we have defined these two data sources, I’ll show you how to use them during a conversation of an agent with an user. The next lines of code were added to the Main.ddl script file. First of all the the reference to the new package, adding the “package DataSources” line to the beginning of the file.

I have chosen to show the first entry of my blog when the user types “news” into the conversation window. When I recognize that pattern, I call the SendLatestRSSEntry procedure that I’ve defined before.

Windows Live Agents - Testing Data Sources

The second example is great to show how simple it is to recognize patterns and split them into variables. The second expression matches with messages that have the format sum <integer> and <integer>. And store the two integer in two variables: OP1 and OP2, which are used as parameters to the procedure SendSum. You can learn more about matching expressions in MSDN.

Feel free to download the project source code and change it at will.

Getting started with Windows Live Agents

Posted on 4:17pm 7/05/2008 by Bruno Silva in Programming, Software, Web, Windows Live

Windows Live Agents

Recently I’ve been exploring the Windows Live Agents SDK. This framework allows you to develop interactive Windows Live Messenger bots. You may already talked to a messenger bot like encarta@conversagent.com which allows you to search information from Encarta Encyclopedia or if you are Portuguese maybe you use info@destakes.com which gives you news about topics you choose, using data from several news websites.

To start developing your Agent/Bot/Buddy you must have Visual Studio 2005/2008 installed as well as .NET Framework 3.0. More details about requirements can be found in the Installation Requirements web page.

After fulfilling the requirements you can download the Windows Live Agents SDK and install it.

Now you are able to start developing your first agent! Open Visual Studio and open the “New Project” window. There will be a new project template under the “BuddyScript” project type. After choosing the “Windows Live Agent” project template, and typing your project name, you will be prompted for some project settings. You can choose several languages for your Agent and define if it will be able to do some default chatting with users. The only problem I found is that a project created this way is way too complex for a beginner, and if (like me) your target language is not supported or if you want your agent being able just to respond to specific messages, it is pretty difficult to use this template.

After hours trying to find out that was really relevant, I’ve created a Simple Project Template from which I removed a lot of files and settings from the original project. It has only some usage examples in Main.ddl (main BuddyScript file). This project is testable out of the box :)

The examples I left on the file are pretty simple. They cover how to set an expected input and how the bot is supposed to react. It also uses a package that makes available information about the current user, like his user name. I left some comments in the code. There are a lot of possibilities that can be explored. Something I tried already and that I will share in future posts is the usage of data sources like XML, Databases, RSS, etc. If you are curious about BuddyScript capabilities take a look at the BuddyScript Language Reference.

In order to test your agents you must have a license key, which is easy to obtain from within Visual Studio. Check out How to: Install the License Key.

You can test your agents directly in Visual Studio (Menu View > Other Windows > Conversation Window) with the interface showed bellow.

Windows Live Agents - Testing

More information about this kind of testing can be found on the How to: Launch the Agent in the Windows Live Agents IDE article.

Another way of testing your Agent which is much more fun, since you can ask your friends to test it too, is deploying your Agent on the Windows Live Messenger network.

Windows Live Agents - Deploying

Read How to: Launch the Agent in IM Environment to learn how achieve it.

More detailed information can be found in the Windows Live Agents SDK documentation at MSDN.

Next time I will talk about using data sources, background procedure (code that runs detached from user conversations), notifications (scheduling messages) and how to create pro-active agents that starts conversations instead of responding to user requests.

Multiplatform Games in XNA - Preprocessor Directives

Posted on 7:25pm 6/22/2008 by Bruno Silva in .NET, Programming, Visual Studio, XBox 360, XNA

You probably already know that developing a game that runs both in XBox 360 and Windows is possible and pretty manageable using XNA Game Studio. If you start developing your game for Windows (which is great to compile/debug your code more quickly), you can then duplicate that project as an XBox 360 game. The same files will be used, but during compilation, the assemblies of XNA Framework that are used are chosen based on the target platform.

Create Copy of Project fot XBox 360

Developing a game for both platforms isn’t that trivial. There are a lot of functionalities that are exclusive to each platform. For instance, the Gamercard integration that I talked about in an earlier post, only works in XBox 360. In the other hand, since XNA games targeting Windows are pure .NET managed code, you can use whatever .NET libraries you want (such as WiimoteLib :) )

If you have a game that you want to compile to both platforms, but which has slight differences, you can use the C# Preprocessor Directives. When you create an XBox 360 project, the XBOX and XBOX360 conditional compilation symbols are used by the preprocessor.

XBox 360 Project Properties

Those symbols can be used to create conditions that make the preprocessor able to choose which lines of code of a file to compile. The example bellow is pretty self-explanatory.

Preprocessor Directives

If you compile this game targeting the XBox 360 you’ll get a blue background, otherwise you’ll get a red background.

This is not a XNA or XNA Game Studio feature, but this was the first time I needed to use such feature. More information about C# Preprocessor Directives are available at MSDN.

XNA - Getting GamerCard Info in Xbox 360

Posted on 5:30pm 6/12/2008 by Bruno Silva in .NET, Programming, XBox 360, XNA

Xbox 360 - GamerCard

One nice XNA feature is the ability to manage Xbox 360 users gamer data. This way you can access your gamercard information which include your name, avatar, achievements, played games and list of friend among other things. How can you do that?

In the Initialize method of your game you must add a specific component into the game, in order to prepare the game to access the data.

XNA Gamercard Manipulation

Since while the Initialize and LoadContent methods are executed, the gamer data is not available yet, you must access it in the Update method. There you can check if a variable is set, and if it is not, you load the gamercard information. Be careful, you must not load the gamer data in every iterations of the game loop. Your Xbox will hang if you do that… It is an expensive operation. Try to load this data only one time or so during the game lifetime.

XNA Gamercard Manipulation

This example show your gamer picture in the screen, followed by some of your informations such as your gamer tag.

XNA Gamercard Manipulation

You can download the full source code and deploy it into your Xbox 360 console and give it a try. (I’ve written before about how to get a free XNA Creators Club Membership in order to allow XNA deploying).

© Bruno Silva | Powered by Wordpress