IMified Relaunches

Via Sean, IMified has relaunched their IM integration service as a web based application. More information can be found here: (Re)introducing IMified Folks may not remember them, but they used to have an event gateway for ColdFusion to handle doing instance messaging work. Their new service is incredibly easier to use now. You simply sign up and then define an IM bot. You can use a bot on their Jabber network (I did cfjeditest@bot.im) as well as working with accounts from AIM, MSN, Yahoo, and GTalk). When you register your bot/application, you define a URL end point. This is a file that will be called whenever someone IMs the account. So let’s say you wanted to build an IM bot that returns the current time. How hard is it?

<cfoutput>#now()#</cfoutput>

Yeah, that’s real difficult, isn’t it? ;) So obviously you can do a bit more. Read the docs for a full explanation, but here are the basics.

Whenever someone IMs your bot, IMified will send to your script a set of values including a unique user key, their message (duh!), and session info including how many times they pinged you and a history of their messages. This allows you to easily do stuff like showing a menu and responding to particular commands.

On the flip side, your server side code can hit IMified for 2 API calls. The first is the ability to get info about the user. This seems to be the same info you get when you right click, get info, on a contact in your favorite IM program. (Adium, right?) The second API call lets you send a message from your bot to a user. You probably wouldn’t do that in the script itself, but in a scheduled task or some other file.

Both of these API calls use a simple POST API. But that wasn’t simple enough for me. Here are two UDFs I’m using in my test bot.

<cffunction name="getUserDetails" output="false" returnType="struct"> <cfargument name="username" type="string" required="true"> <cfargument name="password" type="string" required="true"> <cfargument name="botkey" type="string" required="true"> <cfargument name="userkey" type="string" required="true"> <cfset var result = ""> <cfset var packet = ""> <cfset var s = structNew()>

&lt;cfhttp url="https://www.imified.com/api/bot/" method="post" result="result" username="#arguments.username#" password="#arguments.password#"&gt;

	&lt;cfhttpparam type="formfield" name="botkey" value="#arguments.botkey#"&gt;
	&lt;cfhttpparam type="formfield" name="apimethod" value="getuser"&gt;
	&lt;cfhttpparam type="formfield" name="userkey" value="#arguments.userkey#"&gt;
&lt;/cfhttp&gt;

&lt;cfset packet = xmlParse(result.fileContent)&gt;

&lt;cfif structKeyExists(packet, "rsp") and packet.rsp.xmlAttributes.stat is "ok"&gt;
	&lt;cfset s.status = packet.rsp.user.status.xmltext&gt;
	&lt;cfset s.screenname = packet.rsp.user.screenname.xmltext&gt;
	&lt;cfset s.network = packet.rsp.user.network.xmltext&gt;
	&lt;cfset s.created = packet.rsp.user.created.xmltext&gt;
	&lt;cfset s.lastonline = packet.rsp.user.lastonline.xmltext&gt;
	&lt;cfset s.lastcall = packet.rsp.user.lastcall.xmltext&gt;
	&lt;cfreturn s&gt;
&lt;cfelse&gt;
	&lt;cfthrow message="#packet.rsp.err.xmlAttributes.msg#"&gt;
&lt;/cfif&gt; &lt;/cffunction&gt;

<cffunction name=”sendMessage” output=”false” returnType=”void”> <cfargument name=”username” type=”string” required=”true”> <cfargument name=”password” type=”string” required=”true”> <cfargument name=”botkey” type=”string” required=”true”> <cfargument name=”userkey” type=”string” required=”true”> <cfargument name=”message” type=”string” required=”true”> <cfset var result = ““> <cfset var packet = ““>

&lt;cfhttp url="https://www.imified.com/api/bot/" method="post" result="result" username="#arguments.username#" password="#arguments.password#"&gt;

	&lt;cfhttpparam type="formfield" name="botkey" value="#arguments.botkey#"&gt;
	&lt;cfhttpparam type="formfield" name="apimethod" value="send"&gt;
	&lt;cfhttpparam type="formfield" name="userkey" value="#arguments.userkey#"&gt;
	&lt;cfhttpparam type="formfield" name="msg" value="#arguments.message#"&gt;
&lt;/cfhttp&gt;

&lt;cfset packet = xmlParse(result.fileContent)&gt;

&lt;cfif packet.rsp.xmlAttributes.stat is "fail"&gt;
	&lt;cfthrow message="#packet.rsp.err.xmlAttributes.msg#"&gt;
&lt;/cfif&gt; &lt;/cffunction&gt; </code>

And here is how my bot uses it: (Note I’ve masked my password and botkey)

<cftry> <cfset info = getUserDetails("ray@camdenfamily.com","isecretelyusephp","xxx", form.userkey)> <cfoutput>Hello, #info.screenname#. I see you came from #info.network#. Your last call was #info.lastcall#</cfoutput> <cfset sendMessage("ray@camdenfamily.com","dotnetmakesmeahem", "xxx", form.userkey, "Why did you say #form.msg# to me?")> <cfcatch> <cfoutput>oops! #cfcatch.message#</cfoutput> </cfcatch> </cftry>

The service is currently in beta, and free. The blog entry says it will be a pay service soon, but a free version will still be offered. I’ve got to say - Kudos to these guys. They made a system that is so easy even a PHP coder can use it!

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments