Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

   

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
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath
 
 

IM Active Directory Query Response Bot (phone number)

download code
The following code is another example of a particle use for an IM bot with a difference. Instead of doing a database query as in some of my other examples this Bot performs an active directory query for some user information . In this example it allows you to enter a name or part of a name of a person who's telephone number you want to know it will then  perform an active directory query for this and then return the results to you via an instant message. There are many other bits of information you could use this type of script to retrieve by just changing the active directory properties it searches for.

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.  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


Private Sub Form_Load()
On Error Resume Next   'Don't stop execution, continue on next line
 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.   Because users in Active Directory are not usually located all in the same container or OU in Active directory this part of the script is designed to recursively search though all the OU's and the user container in active directory for user objects and using Instr see if the search term is part of their display name. It uses functions to search through  OU's  and transverse each child OU to find all active directory user accounts. Once a user is found the phone number and username  is retrieved and then sent back to the user as an instant message

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)
slen = 1
If mhead = 123 Then
    sRootDomain = "DC=AD1,DC=MAN,DC=NET,DC=AU"
    Set oRootDomain = GetObject("LDAP://" & sRootDomain)
    oRootDomain.Filter = Array("organizationalUnit")
    qtext = bstrMsgText
    df = seachou(oRootDomain, qtext, pcontact)
    df2 = searchcont(oRootDomain, qtext, pcontact)
End If
End Sub

Function seachou(oOusear, qtext, pcontact)
For Each oOU In oOusear
    If oOU.Class = "user" Then
            If InStr(1, oOU.DisplayName, qtext, vbTextCompare) Then
                dname = oOU.DisplayName
                uname = oOU.TelephoneNumber
                df = SendNumber(dname, uname, pcontact)
            End If
    
    End If
    oOU.Filter = Array("user")
        For Each oUser In oOU
        If oUser.Class = "user" Then
            If InStr(1, oUser.DisplayName, qtext, vbTextCompare) Then
                dname = oUser.DisplayName
                uname = oUser.TelephoneNumber
                df = SendNumber(dname, uname, pcontact)
            End If
        End If
    Next
    oOU.Filter = Array("organizationalUnit")
    For Each oChild In oOU
        dd = seachou(oChild, qtext, pcontact)
    Next
Next
End Function
Function searchcont(oScont, qtext, pcontact)
oScont.Filter = Array("container")
For Each oContainer In oScont
    oContainer.Filter = Array("user")
            For Each oUser In oContainer
            If oUser.Class = "user" Then
                If InStr(1, oUser.DisplayName, qtext, vbTextCompare) Then
                    dname = oUser.DisplayName
                    uname = oUser.TelephoneNumber
                    df = SendNumber(dname, uname, pcontact)
                End If
            End If
        Next
Next
End Function
Function SendNumber(dname, uname, pcontact)
    mess = dname & " " & uname
    strMsgHeader = "Mime-Version: 1.0" & vbCrLf & "Content-Type: text/plain; charset=UTF-8" & vbCrLf & vbCrLf
    msgtype = IM_MSG_TYPE_NO_RESULT
    pcontact.SendText strMsgHeader, mess, msgtype
End Function

That's it once this is done you can compile the code and run it. It you want to run it on an 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.

Using the Code

To use this code all you do is send the name (or part of the name) as an Instant message to the IM bot and it will then
respond backup to you with the phone number.  Note it uses the displayname to perform the query so you need
to be careful if you have placed any comma's in your displayname or if your display name is lastname , firstname as to the
way this code will work if the user choses to query the phone number based on full name (some adaptation may be required).

 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 Stephen Bryant or Pro Exchange. OutlookExchange.Com, Stephen Bryant 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 Stephen Bryant 2008