Today, I am going to go over something a little different from my normal regiment. Lately, I have been messing around with creating IM bots, mainly for GTalk, but I have thrown in AIM also. I feel between those two I am covering a decent amount of instant messaging ground. In this tutorial I am going to show how to build an IM bot that will connect to GTalk and optionally multiple other services.
While researching IM Bots I ran across a really neat webservice called IMified which allows you to use their service to connect to multiple IM protocols (GTalk/Jabber, AIM, Yahoo, MSN). Then all you have to do is create a script in practically any server side language you like. My expertise is in PHP so that is what I have chosen.
The bot that we are going to create today is very simple. It will send the user a list of the most recent Switch on the Code articles, with links, when sent the command recent.
To get started head on over to www.imified.com and sign up for an account. Once signed up you should be on the "My Applications" tab, if not click on it. On the right hand side there is a big "New Application" button. Once clicked you will be presented with a screen similar to this:
Go ahead and fill out the information, making the screen name and bot name whatever you would like. The url is what their service will send requests (messages) to from the bot - in my case a url to the PHP script. Once created you should be sent back to the "My Applications" screen with your bot listed. It should look similar to the following.
You can add more IM protocols by clicking the name of your application and updating the network settings. You do, however, already have to have the screen names created. You can register a new AIM screen name over at the AIM site.
All that is left is to actually create the code, which is easy peasy. The IMified service sends multiple variables to the script handling the requests. In PHP these all come set on the $_REQUEST object and the most important one is msg which holds the content of the message sent. You can see all the interesting pieces that are sent over at the API documentation.
if('recent' == $_REQUEST['msg']) {
$xmlString = file_get_contents("path/feed");
$xml = simplexml_load_string($xmlString);
foreach ($xml->channel as $channel) {
echo $channel->title . '<br><br>';
foreach ($xml->channel->item as $item) {
echo $item->title . '<br>';
echo $item->link . '<br><br>';
}
}
}
?>
That is all the PHP needed to handle this simple task. The first line checks to see if the content sent is equal to recent. If it is we then pull the contents of our rss feed into a variable. Then we use the function simplexml_load_string to load the feed contents into a SimpleXML object.
The following foreach loop go through the channels of the feed (in most cases there is only one) and echos its title. The inner foreach loop goes through each item (post) of the feed and prints out the title and then on the following line a link.
To make sure it works, you need to first make sure the script can be found at the correct url (the one set in the bot application settings). Then you can go to the application debugger, which can be found on the right side of the "My Applications" page. Once there, you can test sending commands to your bot.
That pretty much wraps it up. You can really expand on this and build very complex bots. I suggest taking a look at the "Hello World" bot over at the API documentation, it shows off some of the other information sent along with the message contents. The IMified service is still beta so it has some bugs around the site, but they seem to be working diligently to increase the stability. As always if you have any questions feel free to leave a comment.
One last thing - you can check out the working SOTC bot by adding either:
SwitchOnTheCode (AIM)
to you IM buddy list and sending recent to it.
10/12/2009 - 04:17
Hi,
It is interesting. That works.
However I have a more requirement than automatic response.
I need to send a message to list of buddy on a periodic basis.
Can you give an example of sending unsolicited message to a gtalk buddy?
Thanks and best regards,
Agus
03/28/2010 - 08:06
were u able to achieve this agus?
I require the same.
Thanks
06/28/2010 - 15:50
Hi,
I'm really interested in creating a bot of my own and not take the help of imified.
I want to turn on my current gtalk id to a bot (preferably an autoresponder) when I do not stay online.
06/12/2011 - 00:35
hi sir thanks for this useful tutorial but can you make
bot 'msn IMbot" say only : hi , happy birthday and thank you very much my email
smartx02 (at) gmail (dot) com
send it in email thank you so much :)
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.