| By Ben Forta | Article Rating: |
|
| May 7, 2008 01:45 PM EDT | Reads: |
35,733 |
Creating The Config File
Next you'll need to save that account information into a configuration file. Gateway config files are typically stored in /gateway/config under the ColdFusion root (c:\cfusionmx7\gateway\config on a Windows standalone installation, c:\jrun4\servers\cfusion\cfusion-ear\cfusion-war\web-inf\cfusion\gateway\config on the default ColdFusion instance on a Windows multi-server installation), although they could be stored elsewhere too. That folder may contain a sample XMPP configuration file named xmpp.cfg which you may copy and modify. Your configuration file should contain (at a minimum) the following entries (obviously setting userid and password to your GMail account information):
userid=account@gmail.com
password=password
resourceName=ColdFusion MX 7
secureprotocol=TSL
securerequirement=true
serverip=talk.google.com
serverport=5222
Creating The Bot CFC
Next you need the CFC that will respond to inbound requests. At a minimum you need two methods in your CFC, onIncomingMessage and onAddBuddyRequest.
All instant messages sent to your bot are routed to a method named onIncomingMessage. This method is the one that actually receives all inbound messages, and this is where you'd process user requests, generating responses if needed.
Here is a simple example:
<cffunction name="onIncomingMessage">
<cfargument name="CFEvent" type="struct" required="yes">
<!--- Get the message --->
<cfset var message=CFEvent.data.message>
<cfset var originatorID=CFEvent.originatorID>
<!--- Result structure --->
<cfset var retValue=structNew()>
<cfset retValue.BuddyID= originatorID >
<cfset retValue.Message="You said: #message#">
<!--- send the return message back --->
<cfreturn retValue>
</cffunction>
All methods invoked by gateways receive a single argument, a CFEvent structure (the contents of which vary based on the gateway type). In this example, two local variables are created containing values extracted from the CFEvent structure, message is the actual text sent by the user, and originatorID is the sender name.
Typically your bot would do some processing based on the contents of message, but this simple example just echoes the content back to the sender. A return structure is created, and two values are set, BuddyID is set to originatorID (so that the message is sent back to the sender), and Message contains a string echoing the received message.
And that is all that is needed. The return structure is returned by the <cfreturn> tag, and the echo will be sent back to the sender.
That's onIncomingMessage. Next comes onAddBuddyRequest. When you add a user to your own IM buddy list, the network sends a message to that user asking them to accept or deny the request. If accepted you'll be able to see when that user is online and will be able to send him or her messages, and if denied then you'll not be able to see online status, and you may not even be able to send messages. When a user adds your bot as a buddy, the server will ask it to accept or decline the request. Obviously, these requests must be handled programmatically (as there is no actual user), and so these are routed to a CFC method named onAddBuddyRequest. This method can always return an accept response, or can conditionally accept or decline based on some code (maybe a database lookup, or some password or code provided, and so on).
Here is an example:
<cffunction name="onAddBuddyRequest">
<cfargument name="CFEvent" type="struct" required="true">
<cfset var retValue = structNew()>
<cfset retValue.Command="accept">
<cfset retValue.BuddyID=CFEvent.data.sender>
<cfset retValue.Reason="Welcome!">
<cfreturn retValue>
</cffunction>
Once again, onAddBuddyRequest receives a CFEvent structure. This method must respond to the network either accepting or denying the request. Here a return structure is created. Command is set to accept, accepting the request (set it to decline to deny the request). BuddyID is the name of the user being accepted, and Reason is a welcome message that may be sent to the user (depending on the network and IM client being used). And like before, the return structure is then returned using the <cfreturn> tag.
Other methods are supported too. And best practices dictate that every method be present in your CFC, even if they are not all used. So every IM bot CFC you create should probably contain the following (even if the methods are all empty):
<cffunction name="onAddBuddyResponse">
<cfargument name="CFEvent" type="struct" required="YES">
</cffunction>
<cffunction name="onBuddyStatus">
<cfargument name="CFEvent" type="struct" required="YES">
</cffunction>
<cffunction name="onIMServerMessage">
<cfargument name="CFEvent" type="struct" required="YES">
</cffunction>
<cffunction name="onAdminMessage">
<cfargument name="CFEvent" type="struct" required="YES">
</cffunction>
Published May 7, 2008 Reads 35,733
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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! |
||||
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe & Salesforce Cut Cloud Deal
- Adobe Fiddles with its Web Apps
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF























