Lab Notes: Machine Learning Slackbot

Blagovest Dachev
Mission Data Journal
3 min readOct 17, 2016

In the last installment of our machine learning Labs project to predict availability of Capital Bikeshare stations, we took the prediction engine we built earlier this year and added weather data. This gave us a significant boost in precision and recall which meant the engine was finally ready for prime time.

We quickly built a web page and a REST API that collects user input and displays predictions for the given location, date, and time. Next, we decided to add a conversational interface so we can access the API using natural language. Slack was a logical choice because it has a large footprint at workplaces, including our own, and more and more people are choosing to bike to work each day.

How We Did It

First, we needed a way to understand user messages and extract key information such as location and time. There were a few tools that met our requirements, but in the end we chose wit.ai as it’s free and seems to come with very few restrictions. Employing their visual designer (fig. 1), we created a story that captures different scenarios and actively solicits date and location information from the user in conversational form.

Fig. 1

Next, we had to connect Slack and wit.ai. To do this we set up a node.js server that can talk to both systems and act as a proxy. For example, when the user sends a message to the bot, it gets forwarded and evaluated by wit.ai which in turn responds, and this too is forwarded, this time back to the user. We accomplished this with some help from Botkit, which is a node.js library to the Slack Real Time Messaging API, and wit.ai’s own Javascript SDK.

When a message is received from Slack it is evaluated by wit.ai where the entities that it contains are extracted, stored in a session, and then a response is created in one of two ways. If the session is missing either time or location the user is prompted to provide the missing information. This type of bot is called slot-based because its purpose is to fill a number of information “slots”, just like a web form. However, if both entities are found, it invokes a designated function on the node.js side. This, in turn makes a request to our API, formats the response, and posts the result in Slack.

When the interaction is complete, our node.js server rests the wit.ai state and the next time we hear from the user a new conversation and session will begin.

Challenges

We ran into timezone problems that only became apparent after the bot was deployed. Due to Capital Bikeshare’s Washington DC location, we wanted to interpret user inputs to be in the Eastern Time Zone, so for example “9PM tonight” would be parsed as 21:00 ET. Unfortunately, wit.ai interpreted user input in the timezone of its data center so we had to spend some time massaging the data.

We also faced some difficulties defining the conversation branches in the wit.ai story designer. This is the conditional switching that uses session state to determine if we should prompt the user for more data. Wit.ai doesn’t seem to allow negation in condition expressions so we couldn’t specify “!date”. Instead we had to create a few extra variables (e.g. missingDate, missingLocation) and ensure that they are updated accordingly as the conversation progresses. This felt unnecessary and cumbersome.

Lastly, we wanted to add some variance to bot responses so it doesn’t always prompt for data using the exact same words. However, we couldn’t find a way to alternate messages within the wit.ai story. We probably could have exported this logic to the node.js side but this would have violated the architectural simplicity of a simple proxy. We felt that it wasn’t worth it.

Conclusion

Creating a bot was easy. We were able to quickly integrate a number of free services and create a natural language interface to our prediction engine. As messaging platforms are becoming the focal point of daily communication both at the workplace and home, it is becoming increasingly important to provide access to content and services right in the chat window. Recent advancements in natural language processing and machine learning have enabled the advent of sophisticated bots that are easy to build and interact with.

Have a vision for a vision for a machine learning project? Drop us a line.

--

--