|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS Feature
Building an IM Bot Using ColdFusion
It's easier than you think
By: Ben Forta
May. 7, 2008 01:45 PM
Digg This!
Page 2 of 4
« previous page
next page »
Creating The Config File
userid=account@gmail.com
Creating The Bot CFC 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"> 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"> 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"> Page 2 of 4 « previous page next page »
CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||