User Guide

Table Of Contents
1054 Chapter 43: Using the Instant Messaging Event Gateways
Sample IM message handling application
The application described in this section consists of two CFCs: an employee phone directory
lookup CFC that responds to an onIncomingMessage event, and a Gateway management CFC
that responds to all other events. This example shows how an application can respond to events
and send outgoing messages.
You can configure a gateway to use both CFCs by entering the paths to the two CFCs, separated
by a comma, in the CFC Path field of the Add/Edit ColdFusion Event Gateways form on the
Gateways page in the ColdFusion MX Administrator.
Phone directory lookup CFC
The following CFC implements a simple employee phone directory lookup application. The user
sends an instant message containing some part of the name to be looked up (a space requests all
names). The
onIncomingMessage response depends on the number matches.
If there is no match, the onIncomingMessage function returns a message indicating that there
are no matches.
If there is one match, the function returns the name, department, and phone number.
If there are up to ten matches, the function returns a list of the names preceded by a number
that the user can enter to get the detailed information.
If there are over ten matches, the function returns a list of only the first ten names. A more
complex application might let the user get multiple lists of messages to provide access to all
names.
If the user enters a number, and previously got a multiple-match list, the application returns
the information for the name that corresponds to the number.
The following listing shows the CFC code:
<cfcomponent>
<cffunction name="onIncomingMessage">
<cfargument name="CFEvent" type="struct" required="YES">
<!--- Remove any extra white space from the message. --->
<cfset message = Trim(arguments.CFEvent.data.MESSAGE)>
<!--- If the message is numeric, a previous search probably returned a
list of names. Get the name to search for from the name list stored in
the Session scope. --->
<cfif isNumeric(message)>
<cfscript>
if (structKeyExists(session.users, val(message))) {
message = session.users[val(message)];
}
</cfscript>
</cfif>
<!--- Search the database for the requested name. --->
<cfquery name="employees" datasource="cfdocexamples">
select FirstName, LastName, Department, Phone
from Employees
where 0 = 0
<!--- A space indicates the user entered a first and last name. --->
<cfif listlen(message, " ") eq 2>