Put Messenger Contacts on Your Site Heath Stewart (Clubstew@hotmail.com)
A thread here at DevHood started back on Monday, June 10, 2002, about getting the status of users in your MSN / Windows Messenger ( "Messenger" from now on) list and displaying it on your web site. If you take a quick peek , there were a lot of questions about such a feat. As I researched the problem, I thought this would warrant a tutorial since it's a great way to enhance your site with Messenger. This tutorial will help you understand a little about the Messenger API's and will help you put a Messenger Buddy List on your site. Once finished, you should be able to understand the API's and extend the features, such as putting images next to a pre-existing list of users, perhaps from a database, for those that . have Messenger and associated Passports What you'll Need Before we get started, you'll want to download one or two things Even though the web page uses client-side scripting to display (or harness) a Messenger contact list -. which requires The end-user to have messenger - you'll Want Messenger Too in Order to Test IT (if You Don't Already). If You're Not Using MSN Messenger (PRE-Windows XP) OR WindowsMessenger (Windows XP), You'RE Missing Out. Packed with Features and A Low Footprint On Your System, You May Download It At http://mesenger.msn.com/.you'll Also Benefit from the Messenger API Documents (chm - or compiled html help - files), Which you can get from http: / /messenger.msn.com/for_developers/default.asp. If you havea newer version of Messenger, you already have the automation interfaces registered on your system. Introduction to the Messenger API's The Messenger API is a very rich API (unlike AOL IM, WHO WON '
t let anyone touch their precious protocol), but can be a little confusing. There is an automation interface for controlling the user interface, an automation interface for writing plugins, and an automation interfacefor services extensions (like the Exchange service plugin for Messenger). The one we're interested in today is the user interfaceautomation library. If you're using VC , you'll want to take a look at the MessengerUI.chm file, especially the documentation for VC describing all the interfaces and UUIDs. If you're a VB programmer or .NET developer, you'll want to take a look at the section for VB. It describes everything using objects, enumerations, and still gives you the juicy stuff. Writing a Simple Macro with the Messenger API 'Sfor A SIMPLE TEST, I ADDED A Reference To The "Messenger API TYPE LIBRARY" in Excel's VBA Environment, And Made this Quick Little Macro: Public Sub getBuddies ()
DIM MSGR AS New MessengeRapi.Messenger
Dim Groups as IMessengergroups
DIM Group As IMessengergroup
Dim Contacts as ImessengerContacts
DIM Contact As IMessengerContact
DIM I as integer
DIM J AS INTEGER
I = 1
J = 1
Set groups = msgr.mygroups
For Each Group in Groups
Sheet1.cells (i, 1) = group.name
SET Contacts = Group.Contacts
J = i 1
For Each Contact in Contacts
Sheet1.cells (J, 2) = Contact.FriendlyName
Sheet1.cells (j, 3) = contact.signinname
Sheet1.cells (J, 4) = getStatus (Contact.Status)
J = J 1
NEXT
I = j
NEXT
End Sub
Public Function GetStatus (I as integer) AS String
Dim Str As String
SELECT CASE I
Case 1
Str = "offline"
Case 2
Str = "Online"
Case 6
Str = "invisible" case 10
Str = "busy"
Case 14
Str = "be right back"
Case 18
Str = "iDLE"
Case 34
Str = "away"
Case 50
Str = "on the phone"
Case 66
Str = "Out to Lunch"
Case Else
Str = "unknown"
End SELECT
GetStatus = STR
END FUNCTION
With command-completion, this was a breeze. If you try taking the same approach with a scripting language like JScript (Microsoft's JavaScript) or VBScript, you'll find that it does not work. Even if you use "var msgr = new ActiveXObject (...) "for jscript or" set msgr = creteObject (...) "for vbscript, you'll get a scripting error from Internet Explorer (IE) That States It Can't create the Automation Object.so What gives? Beats me. I use the delightful and extremely helpful OLEView.exe utility that comes with every free and commercial development environment Microsoft distributes to look at all the automation interfaces, coclasses, and type library information and wound up just scratching my head. While Visual Basic can use the ProgID to create an instance of the coclass (class that implements the interface to which you think you're talking) and Visual C can use the CLSID, you can use neither in your scripting environment because the ProgIDs do not Actually Create Objects in That INSTA nce. If you can figure this one out, by all means email me! There is hope, however. If you dig around in Hotmail's source, you'll find a reference to a JavaScript file (.js) that contains all the goodies. Microsoft uses the infamous tag to instantiate the object.Writing a Simple Script with the Messenger API'sSo, lets create a simple HTML page with some basic functionality to display your name:
Title>
IF ("undefined"! = TypeOf (msgrui))
Document.all.welcome.innertext = msgrui.myfriendlyname "'s Messenger Contacts";
Script>
HEAD>
Messenger buddies
B>
P>
P>
Body>
HTML>
When you open this page, you get a scripting error that "MyFriendlyName" is not supported. You'll notice it's in the docs for the MessengerAPI.Messenger coclass that you instantiate (that's the CLSID for that ProgID in the tag). If you try the VC method of prepending "get_" to the property like "get_MyFriendlyName", you do not get an error but "undefined" is returned. In fact, as you'll find, "undefined" is returned for almost everything, including "MyContacts" and "MyGroups". Even though these properties are available in compiled languages such as VC and VB, they are not available in scripting languages. I'm sure this is by design, it's just not documented. Do not give up hope, yet! Where there's a will, there's a way. Digging around in Hotmail's source a little more, I noticed a different set of CLSID's that they were using. Unfortunately, the one CLSID they were using was not documented In The Chm Files I Had You Download, Which Was for The Messenger.msgrobject Progid. OLE View.exe showed me the light once again, however, and I ended up using the CLSID's for both the Messenger.MsgrObject and Messenger.UIAutomation objects, the latter of which implements all the things that Visual Basic could use so easily.Writing a Advanced Script using the Messenger API'sWe will finally create a page that works. We will use two objects, one of which is undocumented, but is described thoroughly by OLEView.exe if you understand IDL (the "COM language"). We can ' T Rely on the Messenger.uiautomation Object Since IT Apparently Returns "undefined"
when hosted in IE (or perhaps any scripting language). The other object (Messenger.MsgrObject) we will use does the trick, but it's by no means as elegant as Messenger.UIAutomation, which provides more functionality that actually does work in a scripting Environment.What Follows Is The Final Html for this Tutorial, And I'll Explain Certain Sections Briefly Below It:
Title>
Script>
Script>
HEAD>
Messenger Contacts
B>
P>
OL>
P>
Body>
HTML>
As you can see, we now use two tags:. "Msgr" is the Messenger.MsgrObject object, and "msgrUI" is the Messenger.UIAutomation object Remember, if you try to instantiate these using "new ActiveXObject () "CreateObject ()", The Automation Interface Won't be Created. Also, Don n't Try This Server-Side, IT Will Just Use Your Messenger Contact List - IF IT Even Works (The Documentation Warns Against this, TOO) .You could add some browser detection code in there to keep Mozilla / Netscape from trying to do something it can not, but that should be taken care of by specifying the language as "JScript" instead of "JavaScript" (last time I checked , at least with 4.x) .The block that is attached to the "window.onload" event displays the list, or an error message depending on the state of you or your clients' Messenger. The regular block contacts helper functions and event handlers buy throughout the page.in the "window.onload ()" Event handler, we use the "msgr" Object and simply get your friendly name and the list of users. If you used "msgrUI", this would not work since "undefined" would be return for each. We then iterate through the list of users and display HTML appropriate for each one. In That Event Handler - At The Point WE Add HTML To Output - We Call Another Function, "SetLink ()". This method Outputs An "a.onclick" Event in the output html. When the user clicks the link for a name, IT Calls the "InstantMessage ()" Function.The "instantMessage ()" Function Uses the other object, "msgrui"
. It contains a method (which actually works) that displays the Instant Messaging (chat) window with the specified user as the recipient.We also use the "AutoSignin ()" method of the "msgrUI" object in the "signIn ()" method we defined because it does not require user credentials (username and password). It tried to log in with the last-known credentials, just like Messenger does when it starts-up. The "msgr" object does have a "Login ( ) "method, but it requires a username and password, which you must prompt for and handle on your own. There could also be security hazards depending on your environment as well. If the" msgrUI "object can not automatically signin, it will prompt for credentials on its own, which is probably a more secure method than JScript or VBScript could provide.The rest of the code is pretty basic and self-explanitory, so I will not explain those.Extending Functionality: What Else You can DoThis Script Just Scratched The Surface of What You Can Do. as you can see from Hotmai l, they display an icon for each user that is in your list and that is online. If you wanted, you can create a bunch of icons (or tear them from the Messenger executable) to display next to users instead of their status as text Which is what i did in my latter sample "" with the properties INSTEAD OF "OFFLINE", "Online", etc .if You're Displaying a Bunch of Users with email address A DATABASE ALREADY AND You Would LIKE TO Add An icon Next To Each As Described Above, You Could Write a Method That Would Search for the email address in the clients'
Contact List and the place an icon next to each, such as the following:
Script>