Falling in love with chatbots

Ben James

December 06th, 2016

I recently watched the excellent film Her by Spike Jonze. Without giving too much away, the lead character slowly becomes enamoured with his new, emotionally-capable OS. It got me thinking, "How far off are chatbots from being like this?".

Lately, countless businesses are turning to chatbots as a way of more meaningfully interacting with their users. Everyone from Barclays to UPS is trying bots in the hope of increasing engagement and satisfaction. Why talk to a suit over the phone when you can message a bot whenever you want? They'll always reply to you and they can read your personal data just like magic, solving your problems in a timely and friendly way.

However, one inherant trend I've noticed is that everything is task based. Platforms like Amazon's Alexa, api.ai, and wit.ai are all based around resolving a user query to an intent and then acting on it. That's great, and works rather well nowadays, but what if you want to tell a story? What if you want to make your user feel grief, happiness, or guilt?

Enter SuperScript

SuperScript is a chatbot framework designed around conversations, written from the bottom-up in lovely asynchronous Node.js. Unlike other frameworks, it puts more emphasis on how a typical human conversation might go over more traditional methods like intent mapping. Let's find out a little more about how it works.

To get started, you can run the following in your terminal. Note that we're using the alpha version at the moment, which is nearing completion but is still liable to change:

npm install -g superscript@alpha
bot-init myBot// Creates a bot in myBot directory
cd myBot
npm install
parse
npm run build
npm run start

Then, in another tab, run:

telnet localhost 2000

And you'll find that you can talk to a very simple script that'll say “hello!”.

Let's see how that script works. Open chat/main.ss and take a look. Triggers (what you say to the bot) are prefixed with the + symbol, while replies (what it says back to you) are prefixed with the - symbol, like this:

+ Hi
- Greetings, human! How are you?

Under the hood, Hi gets transformed into ~emohello, which matches a number of different greetings like Hey, Greetings or just the original Hi. So, whenever you greet the bot, it'll say Greetings, human!.

Conversations

Conversations are the first of the building blocks of true power of SuperScript, and look something like this:

+ * good *
% Greetings, human! How are you?
- That's great to hear!

The % references a previous reply that the bot must have said in order for this bit of chat to be considered. In this case, SuperScript will only look at this gambit (a trigger plus a reply) if the last bot reply was Greetings, human! How are you?.

Let's imagine it was this. Note how we have a star “*” in the trigger this time. This matches anything. Here, if the user says something like Good thanks orI'm feeling good, then we'll match the trigger and send the response That's great to hear!.

But, if the user doesn't say anything with good in it, we're not going to match anything at present. So let's write a new trigger to catch anything else. We can actually go a step further and use a plugin function to analyse the user's input:

+ (*)
% Greetings, human! How are you?
- ^sentiment(<cap1>)

Here, we have a conversation just as we did earlier. But, now we have a plugin function,sentiment,which will analyze the captured input <cap1> (what the user said to the bot) and respond with an appropriate message.

Let's write a plugin function using the npm library sentiment. Create a new file, say, sentiment.js, and stick it into the plugins folder in your bot directory. Inside this file, write something like this:

import sentimentLib from 'sentiment';

const sentiment = functionsentiment(message, callback) {
const score = sentimentLib(message).score;
if (score >2) {
returncallback(null, "Good for you.");
  } elseif(score < -2) {
returncallback(null, "I'm glad you share my angst.");
  }
returncallback(null, "I'm pretty so-so too.");
};

exportdefault { sentiment };

Now, we can access our plugin from our bot, so give it a whirl. Pretty neat, huh?

Topics

The other part of SuperScript that we get a load of power and flexibility from is its support of topics. In normal conversations, humans tend to spend a lot of time on a specific subject before moving onto another subject. For example, this hipster programming bot will talk about programming until you don't move on to another topic:

> topic programming
+ My favourite language is Brainfuck
- Mine too! I love its readability!

+ My favourite language is (*)
- Hah! <cap1> SUCKS! Brainfuck is where it's at!

+ spaces or tabs
- Why not mix both?

+ * 1337 *
- You speak 13375P34K too?! 
< topic

Once you've hit any of these triggers, you're in the programming topic and won't get out unless you say something that doesn't match any of the triggers within the topic.

Topics and conversations are the foundations of building conversational interfaces, and you can build a whole lot of interesting experiences around them. We're really at the beginning of these types of chatbots, but as interest grows in interactive stories, they'll only get better and better.

About the author

Ben James is currently the Technical Director at To Play For, creating games, interactive stories, and narratives using artificial intelligence. Follow us on Twitter at @ToPlayFor.