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

 

 
 

IM Database auto response bot

The following are two examples of an auto response bot that use's a database to determine how to respond to an Instant message sent by a user. The first performs a language Translation from French to English and the second creates an online thesaurus.

Article 2 IM Thesaurus response bot

Language Translation IM response bot download code

Introduction

The following code is an example of a particle use for an IM bot. What it does is allows you to send a phrase in another language to an IM user and the bot will then separate the words sent and try and translate these words into English and send a reply in English to you. I know in reality translation is a lot more complicated then this but the idea does have some merits as an example. For this example I used a translation database of about 2000 French to English words I cobbled together of the internet, word lists are a little hard to come by if you not willing to pay. The database is basically two columns one with the French word the other with the English word

How it works

The code works similarly to what's described in my IM bot introduction  article please see this for a description of the front end of this code. The one notable acceptation is the addition of the ADO code to this project. For this example I connect to an SQL database table to do the translation. You will need to customise these parts of the code with your own connection settings and IM user details

Public WithEvents oIMServ As MSIMCliSDKLib.MSIMService
Public WithEvents oIMApp As MSIMCliSDKLib.MSIMHost
Dim Rec As New ADODB.Record
Dim Rs As New ADODB.Recordset
Dim cnxn As New ADODB.Connection
Dim StrCnxn As String

Private Sub Form_Load()
On Error Resume Next   'Don't stop execution, continue on next line

StrCnxn = "Provider=sqloledb;Data Source=server;Initial Catalog=testdev;User Id=testdev;Password=test;"
cnxn.Open StrCnxn
Set oIMApp = CreateObject("MSExchangeIM.MSIMHost")
Set oIMServ = oIMApp.CreateContext("Default", 0)
strtest = Array("IMaddress", "username", "password", "domain")
oIMServ.Logon strtest



End Sub

The next part of the code is the one that matters which is the OnTextRecieved event, this part of the code checks to see if its a real IM message and not a typing event, it then separates out each of the words if multiple words are sent, then queries the database and sends back a translation as an instant message to the originator.

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)
stest = bstrMsgText
loop1 = 0
schar1 = 1
If mhead = 123 Then
Do Until loop1 = 1
If InStr(schar1, stest, " ") Then
    echar = InStr(schar1, stest, " ")
    echar1 = echar - schar1
    cword = Mid(stest, schar1, echar1)
    strmailq = "SELECT * FROM lang Where French = '" & cword & "'"
    Rem df = msgbox(strmailq)
    Set Rs = cnxn.Execute(strmailq)
    If Not Rs.EOF Then
        trans = trans & Rs.Fields("English").Value & " "
    Else
        trans = trans & "unknown" & " "
    End If
    schar1 = echar + 1
Else
    echar = Len(stest) + 1
    echar1 = echar - schar1
    cword = Mid(stest, schar1, echar1)
    strmailq = "SELECT * FROM lang Where French = '" & cword & "'"
    Rem df = msgbox(strmailq)
    Set Rs = cnxn.Execute(strmailq)
    If Not Rs.EOF Then
        trans = trans & Rs.Fields("English").Value & " "
    Else
        trans = trans & "unknown" & " "
    End If
    loop1 = 1
End If
Loop
strMsgHeader = "Mime-Version: 1.0" & vbCrLf & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
msgtype = IM_MSG_TYPE_NO_RESULT
pContact.SendText strMsgHeader, trans, msgtype
End If

End Sub

That's it once this is done you can compile the code and run it. It you want to run it on a ongoing basis you might want to look at running it as a service using srvany from the NT resource kit or just use the schedule service and run it on machine start-up.

download code

 


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