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:
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:
onIncomingMessage function receives the incoming IM.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.
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 functionalityextensions: provides access to server settings such as mappings and web servicesmail: provides access to the server’s mail settingsswitch 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. 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.