Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

       How did you like this article? Please vote and let us know.          

Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Shannal L. Thomas
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath

 

 
 

Instant Messaging Auto Response Bots Introduction

What are they?

Instant Messaging Auto response bots are bits of code that are designed to respond automatically to Instant messages that are sent to them. These can be used for basic things like out of office or away from desk auto reply messages to more complicated functions such as doing database searches or even Active directory queries and then respond back to the user with the requested information.

What types are there?

The auto response code runs as part of your messenger client so you can use the MSN messenger client or you can use the Exchange 2000 Instant Messaging Client SDK Runtime Components which can be downloaded from http://www.microsoft.com/downloads/release.asp?ReleaseID=24779 or from the Exchange SDK.).

If you want to look at using the MSN messenger client there's lots of sample code out there check out the following links

http://www.msnfanatics.com - lots of forums that contain code samples and ideas for using the MSN messenger client

http://www.tk-x.com/simplebot.php a simple Auto response bot for the MSN client

http://download.com.com/3000-2150-8697385.html?legacy=cnet this is a complex version of a bot it lets you set away messages, different for different people, or one message for all. Even allows people to get jokes from you

The other types of bots you can have is one created using the exchange Instant messaging runtime components and these are the ones I've focused on. The advantage of using the runtime components (that I can see) is that you can write a fully contained bot that can be run as an NT service.

Building a Simple Response bot using the Exchange runtime components

Vbscript or VB ??
 
The first problem you need to work out is what language do you use, if you haven't already guessed I a big fan of using VBS script its the simplest type of programming around (bar BAT files) but for this type of job it doesn't make the grade. Basically the whole crux of an IM bot works around using the OnTextRecieved event of the messenger object. Although you can use events in VBscript because of the way the events have been implemented in the IM runtime components its not possible with the current version to bind to the OnTextRecieved events. Because of this all the IM bot code needs to be written in VB.

Example

Let look at an example using a simple VB project with one form in the form_load method you need to put in two lines which creates an instance of the MSIMHost class and an interface pointer to this class. The next  two lines provides a logon to Instant messaging through calling the logon method of the newly created  MSIMHost object. The parameters passed to this method can vary depending on your requirements. In the above code i have passed the IMlogon, username, password and domain name. You can however choose to pass only the logon name and as long as the user executing the code has rights to logon to the IM account (and your using NTLM authentication) it will works fine .You will need to customise these parts of the code with your own IM user details

Public WithEvents oIMServ As MSIMCliSDKLib.MSIMService
Public WithEvents oIMApp As MSIMCliSDKLib.MSIMHost

Private Sub Form_Load()
On Error Resume Next   'Don't stop execution, continue on next line
Err.Clear
Set oIMApp = CreateObject("MSExchangeIM.MSIMHost")
Set oIMServ = oIMApp.CreateContext("Default", 0)
strtest = Array("IM-address@", "username", "password", "Domain")
oIMServ.Logon strtest
Rem If Err.Number <> 0 Then MsgBox "Error: " & Err.Description  'Display error message
End Sub

This will fire up and logon the IM runtime component with the user specified in the code. The next part of the code is to simply implement something for the OnTextRecieved event. The first if statement checks to see what caused the event the OnTextRecieved event can be fired by someone starting to type a message to a user or by receiving an Instant message. To determine what is what a check is performed on the length of the IM header which will tell you if its a message or just someone typing.

Private Sub oIMServ_OnTextReceived(ByVal pIMSession As Object, ByVal pContact As Object, ByVal bstrMsgHeader As String, ByVal bstrMsgText As String, pfHandled As Variant)
mhead = Len(bstrMsgHeader)
If mhead = 123 Then
    message = "My response"
    strMsgHeader = "Mime-Version: 1.0" & vbCrLf & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
    msgtype = IM_MSG_TYPE_NO_RESULT
    pContact.SendText strMsgHeader, message, msgtype

End If

End Sub

And that's it all you need now is to compile the code and run it, of course you can be a lot more fancier with what you can do for some examples please see my other articles. If you wanted to run the Bot as a service just use srvany from the NT resource kit or the NT schedule service.

download sample


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Pro Exchange. OutlookExchange.Com and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

© Copyright Pro Exchange, Inc., 2006