Develop AI Bot Like Uber in Slack With Wit.ai and API Node.js Library

0
Shares
Develop AI Bot Like Uber in Slack With Wit.ai and API Node.js Library
3 (60%) 2 votes

Slack is one of the popular messaging application that exploded in the popularity among all development teams. What makes Slack more compelling? Slack integrates with all kinds of other services and application that best suited and fitted within API space.

Slack is available with a collection of standard integration, however, it’s easy to develop your own. From posting notifications to developing interactive bots, slack integration can do everything.

Now, Uber is also available on Slack for its users to hail Uber rides directly from their chat client with a command-line type interface. Uber on Slack allows users to type different commands to book ride chosen pickup location. Below are given command that you can use into slack to use Uber service on Slack.

  • /uber ride [origin] to [destination] – Request a ride from [origin] to [destination].
  • /uber estimate [origin] to [destination] – Get price and time estimates to drive from [origin] to [destination].
  • /uber status – See the status of the last ride you requested on Slack.
  • /uber share – Get a link to share your ride’s progress.
  • /uber cancel – Cancel the last ride you requested on Slack.
  • /uber help – Get a list of available commands.

A bot is a simple “software robot”, available for all kinds of applications. Some of the applications accept inputs in the conversational format to help automate different tasks for users. Apple’s Siri or Microsoft’s Cortana are some of the most examples of bots.

Being a new concept in the market, Bots are starting to become mainstream. There are many bots are now being developed for Slack. Some of the best examples of bots are Jeopardy!, and old-school. Bots can enable new and interesting ways of working and create new classes of applications.

Bots make an application more usable and natural. Some users feel more comfortable to use a conversational bot rather than other kinds of interfaces.

What Else Can Bot Do?

We have seen that chatbots are replacing individual application. Instead of closing Facebook messenger and opening Uber, users can easily message Uber and ask for a ride.

Assist, launched by San Francisco-based startup, is a chat bot that folds a host of services such as ordering delivery, hailing a ride, purchasing baseball tickets, sending flowers and scheduling an appointment for salon – in a single chat contact.

Sequel also allows developing media and entertainment bots without any coding. It has simple drag-and-drop and publishing tools with rich media capabilities. Sequel has become the first platform to provide businesses a personal touch to their bots with its ‘Customizable Personality.’ Like Sequel, Space-O’s bot development team allows developing bots for users with content and characters of their choice through audio, GIF, emoji, quiz, text, photo, video, poll options, etc.

Rather than downloading individual applications like Uber, Florist One, Postmates, users can send chat to bot from inside his/her favorite chat platforms like Slack, Telegram, Facebook Messenger, or Kik and tell what he/she wants.

Want to Develop Bots with Slack API Node.js library?

Here, we are using the Slack API Node.js library to develop a bot and exploring Wit.ai API and other tools to use natural language processing to evaluate and reply to a human-inputted text. We have the introductory toolset that helps to build you own team bot.

Here, we are using Node.js to develop a bot that integrates with Slack. It is recommended to download and install Node.js before continuing. You can have a variety of APIs for various purposes, but one of the most useful for writing bots is the Slack Real Time Messaging API.
We are using the official Slack client for Node.js to write our bot using the Slack RTM API, which can be installed by using NPM

npm install –save slack-client

How to Set up a Bot User?

Before starting with the coding part, you will have to create a new bot user integration in the Slack account. Give name and avatar to the bot to develop a unique personality of your bot.
Now, Copy the generated API token, get started on the bot.

Basic Structure

The following will be resembled by the basic structure of our bot. Ensure to paste in the Slack bot’s API token.

var SlackClient = require('slack-client');
var slackClient = new SlackClient("PASTE YOUR BOT API TOKEN HERE");
var bot; // Track bot user .. for detecting messages by yourself
slackClient.on('loggedIn', function(user, team) {
bot = user;
console.log("Logged in as " + user.name
+ " of " + team.name + ", but not yet connected");
});
slackClient.on('open', function() {
console.log('Connected');
});
slackClient.on('message', function(message) {
if (message.user == bot.id) return; // Ignore bot's own messages
var channel = slackClient.getChannelGroupOrDMByID(message.channel);
channel.send('Hello world!');
// More goes here later..
});
slackClient.login();

Now, you have to detect the bot’s own messages to make sure it doesn’t reply to itself and get into a loop. This is an excellent start, however, the bot is its initial stage, so we only see ‘Hello world!’.

Handling Simple Commands

Bot can understand a set of basic commands. The advantage of using bot is they are simple to parse and will be familiar to users that have used a command line prompt. This is some good stuff, however, they are few extra details that you should consider the most. Here, we are using the 99designs Tasks client for Node.js in this example.

if (message.text == 'create') {
slackClient._send({ type: "typing", channel: message.channel });
api.create({ body: 'Please edit this image', urls: ['http://i.imgur.com/YryoTju.jpg']}, function(err, task) {
if (err) { return channel.send('Something went wrong') }
channel.send("Success! Created: " + task);
});
} elseif (message.text == 'list' || message.text == 'status') {
slackClient._send({ type: "typing", channel: message.channel });
api.myTasks(function(err, results) {
if (err) { return channel.send('Something went wrong') }
var list = results.items.join("\n");
channel.send("Your Tasks: \n" + list);
});
} elseif (message.text == 'help') {
channel.send("Possible commands are: create, status, help, ...");
} else {
channel.send("Unknown command. Possible commands are: create, status, help, ...");
}

Allow synonyms and pseudonyms for commands, as it is also helpful for providing useful help text and guidance when users are making mistakes. Use Slack by typing indicator for responses. The typing indicator is for human, who starts typing and let other human users know that they are replying.

However, the bot is a program that doesn’t type messages like a human, so a typing pointer may seem pointless. If you are providing instant feedback to the users, they really help them to respond like when making API calls or doing some processing that takes a few seconds.
Currently, it is not implemented in the Slack node.js library, however, it worked around by calling the private _send method, as seen in above example. It may also be useful for managing file uploads, although it depends on the bot type that you are writing.

if (message.upload) {
channel.send("This file was uploaded: " + message.file.url);
}

How to Use the Wit API?

First, install the Wit JavaScript library

npm install --save node-wit
var wit = require('node-wit');

var ACCESS_TOKEN = "YOUR WIT ACCESS TOKEN HERE";
wit.captureTextIntent(ACCESS_TOKEN, "What's the weather in Melbourne?", function (err, res) {
console.log("Response from Wit for text input: ");
if (err) console.log("Error: ", err);
console.log(JSON.stringify(res, null, " "));
});

See an example of what you retrieve from Wit:
{
"msg_id": "17c81127-5c02-4646-9d9d-ba72bccd03b2",
"_text": "What's the weather in California?",
"outcomes": [
{
"_text": "What's the weather in California?",
"intent": "weather",
"entities": {
"location": [
{
"suggested": true,
"value": "California",
"type": "value"
}
]
},
"confidence": 1
}
]
}

Here, the main idea is to specify the kinds of “intents” that you want your bot to understand. One of the best things about Wit is it takes care of parsing sentences to work the intent and any referred entities. Users can have a list of community contributed “intents”, which can be changed according to the needs.

Bots Are the Future of Development Industry

There are many teams using group messaging system like Slack that automates tasks like an eventual net development through natural language empowered software bots. Bots will only become more universal. It will grow to take over other features that websites and applications have.

With Bots, one can easily deliver different contents and features on the small screens. It is supposed that personal and work bots help businesses to automate common tasks and gives an interface to detonate many connected devices.

This page was last edited on June 16th, 2016, at 17:54.
 
0
Shares
 

Want to develop bots with Node.js library? Contact us today.

Get your free consultation now