Putting your Bot into Slack

It all began back in 1990, when Hugh Loebner initiated a contest, designed to implement the Turing Test. The Loebner Prize is an annual competition in artificial intelligence, where judges decide, which chatbot is the most human-like. The format of the competition is that of a standard Turing test. In each round, a human judge simultaneously holds textual conversations with a computer program and a human being via computer. Based upon the responses, the judge must decide which is which.
The most recent Loebner Prize in Artificial Intelligence competition happened last September, in Bletchley Park, where during World War II, the German secret codes were broken. The winner was a chatbot named Rose, created by Bruce Wilcox, and developed in ChatScript. You can chat with her here. You can use text input or use your voice, when you open the link in a Google Chrome web-browser.
“Rose is a twenty-something computer hacker, living in San Franscisco. As Rosette she won the 2011 Loebner competition. As Rose she won in 2014.”

Mitsuku, a chatbot created by Steve Worswick, developed in AIML – The Artificial Intelligence Markup Language, was the winning bot the year before. Again, you can use text input and chat with Mitsuku here.

Slack is a very popular cloud-based team collaboration tool, used for mostly for team messaging. From the very beginning, Slack was very open and published APIs, allowing 3rd party developers to build plugins and integrations. That and making access-tokens swiftly and easily accessible turned Slack from a product into a platform.

Slack Bot

Enough with the introduction, lets add two of the best chatbots to your Slack Team.

Adding a new bot integration

Start by clicking the down-arrow, next to your team name in slack and select “Apps & integrations”. In the opening web page, click on “Build” (somewhere at the top right). Now click the “Make a Custom Integration” button and finally select “Bots“.

Here you need to enter a name for your bot (I’d suggest you use @rose or @mitsuku) and click the “Add bot integration” button. This will generate the API-Token that is going to be used to connect the web-app (which we are going to develop next) to your Slack Team. While still on the slack page, you can upload an icon, which will serve as the bot’s avatar.

Reverse Engineering Bot interfaces

Both aforementioned bots are available on the internet and observing network usage, using for instance the developer-tools inside the Google Chrome web-browser, reveals how the bots’ engines can be accessed more directly.

Accessing Rose

Request URL:http://ec2-54-215-197-164.us-west-1.compute.amazonaws.com/ui.php
Request Method:POST
Content-Type:application/x-www-form-urlencoded
Accept:*/*
Payload: user, message, send

User can be any unique id, message is the input you want to send to the bot, and while the value for send is empty, it is still required.

Accessing Mitsuku

Just observing the network traffic didn’t really help all that much, since the bot always returns HTML content. Instead of scraping the HTML, looking at Pandorabots (which is the AIML hosting platform for Mitsuku) exposes that the request-uri /talk-xml will in fact have the bot return XML encoded content.

Request URL:http://fiddle.pandorabots.com/pandora/talk-xml?botid=f326d0be8e345a13&input=<YOUR INPUT>
Request Method:GET

– or –

Request URL:http://fiddle.pandorabots.com/pandora/talk-xml
Request Method:POST
Content-Type:application/x-www-form-urlencoded
Accept:text/xml
Payload: botid, custid, input

The response will look something like this:

 

The retuned custid parameter value needs to be used with followup requests, allowing the bot to keep state. I.e., a followup requests:
Request URL: http://fiddle.pandorabots.com/pandora/talk-xml?botid=f326d0be8e345a13&custid=afb4c2475e003b06&input=…

While this is not really a Webapp, I still chose to implemented it in a way, where the final artifact is simply a war file that can be throw into any web-container (like Tomcat for instance) where it will bootstrap itself. I use the open source Simple Slack API project, which seemed well maintained and also important, uses Tyrus, an the open source JSR 356 – Java API as the WebSocket implementation. Implementing the bot integration as an JAX-RS application and using the Simple Slack API turns this into an rather short and simple piece of code. Check it out at Github: https://github.com/wolfpaulus/slackbot

 

Leave a Reply