![]() |
|
|
| Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site | ||
|
|
IM Database auto response botThe 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 codeIntroduction 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.
|
|
|
|
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