Accessibility

Table of Contents

Chatting Through IM Gateways in ColdFusion MX 7

Creating the JabberBot ColdFusion Component

With the crash course in the XMPP gateway and the Administrator API out of the way, it’s time to get down to business and create the JabberBot CFC that the application uses as an instance of the XMPP gateway. Actually "create" might not be an entirely appropriate term since I’ve been extremely nice and created JabberBot.cfc for you. You can thank me by creating ultra-cool IM applications in ColdFusion. Configure your CFC using the following steps:

  1. Open the JabberBot.cfc file included in the ZIP file that accompanies this tutorial.

    Because I wanted to provide several IM commands for you to play with, you’ll see that the CFC is about 230 lines long, but the basic concept is the same as the "Hello Jabber!" example above:

    • The onIncomingMessage function receives the incoming IM.
    • The outgoing IM’s buddy ID is set to the ID of the sender.
    • The appropriate action is taken based on the contents of the incoming IM and a response is created.
    • The outgoing IM is sent to the sender.

    The JabberBot CFC is most interested in the message portion of the IM packet because this specifies which actions the JabberBot will perform. JabberBot can respond to two basic types of commands: simple commands that consist of a single string and simply return information to the sender, or more complex commands (such as creating a data source) that require additional information. So you don’t get too bogged down in the details at this point, let’s take a look at two basic examples of how this works.

  2. Modify line 21 in JabberBot.cfc and insert your ColdFusion administrator password. To be able to access the Administrator API, you first need to log on to the administrator by instantiating the CFIDE.adminapi.administrator CFC and calling the login() method, to which you pass your ColdFusion administrator password. Obviously it would be a huge security hole if just anyone could instantiate Administrator API CFCs and use them. Lines 22 through 24 create the three Administrator API CFCs that JabberBot uses:
    • datasource: provides access to the server’s data source functionality
    • extensions: provides access to server settings such as mappings and web services
    • mail: provides access to the server’s mail settings
  3. Now look at the IM message. Simple commands are handled with the switch statement that begins on line 30. This switch statement evaluates the incoming IM’s message and responds appropriately. The first case in the switch, beginning on line 32, handles the user’s request for help, which the user signifies by sending an IM of "help" or "menu" to JabberBot. The outgoing IM’s message is set to some basic help information and because no other action is necessary for this request, the IM is sent out in the cfreturn tag on line 212. Similarly, the getDatasources case that begins on line 55 uses the datasource Administrator API object to retrieve a list of data sources on the server and return the list to the sender.
  4. Look at the more complex messages handled beginning at line 134. If more than a simple command is required for JabberBot to process a request, this can’t be handled by the switch statement. The four complex commands allow you to verify a data source, create a SQL Server data source, create an Access data source, and delete a data source.

There are several other commands within JabberBot. Experiment and add your own. Before you can start chatting with your ColdFusion server, however, you must register JabberBot as an XMPP event gateway and start it so it can respond to IMs, as you’ll do in the next section.