Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion

ColdFusion: Article

Building an IM Bot Using ColdFusion

It's easier than you think

<!--- Gateway id --->
<cfset gateway_id="jabber cfdocs">

<!--- Status options
Bit off a hack needed because the
strings returned by getStatusAsString()
are not the same as the ones passed to
setStatus().
--->
<cfset status_options="ONLINE,AWAY,DND,NA,FREE_TO_CHAT">
<cfset status_display="ONLINE,AWAY,DO NOT DISTURB,NOT AVAILABLE,FREE TO CHAT">

<!--- Get the GatewayHelper --->
<cfset helper=getGatewayHelper(gateway_id)>

<!--- If form post is present --->
<cfif IsDefined("FORM.status_new")>
<cfset helper.setStatus(ListGetAt(status_options, status_new), "")>
</cfif>

<!--- Get current status --->
<cfset status=helper.getStatusAsString()>

<cfoutput>
<!--- Display current status --->
<h3>Status: #status#</h3>

<!--- Form to change status --->
<form action="#CGI.SCRIPT_NAME#" method="post">

<!--- Loop through status options --->
<cfloop index="o" from="1" to="#ListLen(status_options)#">
<input type="radio" name="status_new" value="#o#"
<cfif ListGetAt(status_display, o) is status>checked</cfif>
onChange="submit()">#ListGetAt(status_display, o)#<br>
</cfloop>

</form>

</cfoutput>

This is a pretty simple example. The gateway id is defined, as are lists containing the status values returned by and passed to the GatewayHelper methods. Next, GetGatewayHelper() is used to obtain a GatewayHelper object. The form in this little app is self-posting, and if a new status has been specified, helper.setStatus() is used to set the status (which will immediately be reflected in relevant buddy lists). The code then uses helper.getStatusAsString() to obtain the current status, and then displays a form used to change the status.

This is just a simple example, and there are lots of other GatewayHelper methods, too. Some are informational, like numberOfMessagesReceived() which returns the number of received inbound messages, and isOnline() which indicates whether or not the gateway is online and connected. Others are used to control buddy lists, including getBuddyList() which returns the buddy list, getBuddyInfo() which returns information about a buddy, and addBuddy() and removeBuddy() which do exactly what their names suggest. The full list of GatewayHelper methods for IM gateway types is included in the ColdFusion documentation (in the ColdFusion MX Event Gateway Reference section). Other gateway types may have other helper methods, consult the relevant gateway documentation to determine which methods are available to you.

Managing Session State
The last topic to discuss is session state management. Fortunately, there is not much to discuss. If ColdFusion session state management is enabled then you can use the SESSION scope in your bot code as you would in any other applications.

It is worth noting that although you may use SESSION just as you would in a web application, ColdFusion uses a different mechanism to identify sessions. In web applications ColdFusion uses a jsessionid or a cfid/cftoken pair (in cookies or URL parameters) to identify sessions, and these are passed back and forth with each request. There are no cookies or URL parameters to be passed back and forth when using IM, and so ColdFusion uses a combination of gateway id, gateway type, and the originator id (sender's buddy name) to identify sessions. And this is all handled internally by ColdFusion, you can just use sessions as you usually would, and it'll just work.

Summary
And there you have it, all you need to know to create your own IM bots, one example of the type of integration made possible but event gateways in ColdFusion MX 7 Enterprise. Have fun with this one, and if you do bring a public bot online, be sure to let me know about it.

More Stories By Ben Forta

Ben Forta is Adobe's Senior Technical Evangelist. In that capacity he spends a considerable amount of time talking and writing about Adobe products (with an emphasis on ColdFusion and Flex), and providing feedback to help shape the future direction of the products. By the way, if you are not yet a ColdFusion user, you should be. It is an incredible product, and is truly deserving of all the praise it has been receiving. In a prior life he was a ColdFusion customer (he wrote one of the first large high visibility web sites using the product) and was so impressed he ended up working for the company that created it (Allaire). Ben is also the author of books on ColdFusion, SQL, Windows 2000, JSP, WAP, Regular Expressions, and more. Before joining Adobe (well, Allaire actually, and then Macromedia and Allaire merged, and then Adobe bought Macromedia) he helped found a company called Car.com which provides automotive services (buy a car, sell a car, etc) over the Web. Car.com (including Stoneage) is one of the largest automotive web sites out there, was written entirely in ColdFusion, and is now owned by Auto-By-Tel.

Comments (4) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Randy Smith 05/07/08 02:00:43 PM EDT

Got it working - remember to remove the pound sign from in front of each of the options! I assumed that if they said there was a "default" version that you didn't need to specify the parameters. Wrong!

Randy Smith 05/07/08 01:16:24 PM EDT

This was written in 2005. Here it is 2008 and I'm trying to apply this using Cold Fusion 2008 Enterprise, but I can't get the instance to start. The log files are basically saying that talk.google.com won't let me connect. Is there now a different IP I should connect to, or did Google shut down this "portal"?

emanuel 10/22/05 09:43:27 PM EDT

Where can i download this bot? Thanks.

Mark Holton 10/16/05 09:35:23 AM EDT

This is an awesome overview - Thanks for taking the time to supply this great info, Ben!