As explained in this Wit.ai blog post, we decided to focus on a more reliable, scalable, and integrated NLP experience, and as such discontinued our support for Stories and /converse.
If you were using Stories, this means two things:
/converse
API, alongsidewit.runActions()
are no longer supported since February 13, 2018 10am PST.- In the majority of use cases, you can still use /message and code on your side to manage the conversation.
This tutorial will show you how you can go about converting a stories app to one that uses /message
.
For this tutorial, we will assume that you had an appointment-bot Wit app built on top of stories. You would have had two basic stories: apptMake
, and apptShow
.
In our code, we would:
- implement our
actions
: thesend
function andshowAppointments
function - we would manage context, and invoke
wit.runActions
every time we receive a message
Here is how that would have looked like:
const {firstEntity, WIT_TOKEN} = require('../shared');
const Wit = require('node-wit/lib/wit');
const actions = {
send(request, response) {
console.log(`π€ > ${response.text}`);
return Promise.resolve();
},
showAppointments(x) {
// implement logic here to display a view of appointments
console.log('π€ > Okay, showing appointments');
},
makeAppointments({entities, context}) {
// implement logic here to display a view for making appointments
console.log('π€ > Okay, making an appointment');
},
};
// database for contexts
const contextsForSession = {};
function handleMessage(userText, sessionId) {
const wit = new Wit({accessToken: WIT_TOKEN, actions});
return wit.runActions(
sessionId,
userText,
contextsForSession[sessionId] || {}
).then(context => {
contextsForSession[sessionId] = context;
});
}
Conceptually, Stories is simply using /message
itself, and choosing a different path to take, based on what entities we extract.
We could do this ourselves, by:
- running
/message
on the user text, to extract entities - decide what to do next, based on our user data, and the extracted entities.
Here's how that could look like:
const {firstEntity, WIT_TOKEN} = require('../shared');
const Wit = require('node-wit/lib/wit');
const wit = new Wit({accessToken: WIT_TOKEN});
function handleMessage(question) {
return wit.message(question).then(({entities}) => {
const intent = firstEntity(entities, 'intent');
if (!intent) {
// use app data, or a previous context to decide how to fallback
return;
}
switch (intent.value) {
case 'appt_make':
console.log('π€ > Okay, making an appointment');
break;
case 'appt_show':
console.log('π€ > Okay, showing appointments');
break;
default:
console.log(`π€ ${intent.value}`);
break;
}
});
}
With the /message
use case, you can build your own stories. You can both simplify the code, and add more rich features.
For example:
- What if there are no intents?
- You could use the
N-best
feature to get multiple options for entity values, and let the user clarify
- You could use the
- What if you want to dynamically change your bot's answers?
- You can use a datastore and add dynamic answers
- what if you want to use Messenger?
- With Messenger Built-in NLP you can get your entities directly in the Send/Receive API so you don't even have to make a call to the Wit.ai server.
You can learn more about leveraging wit features on the wit-api-only-tutorial.
Thank you for being with us on this journey. We learned so much from you over the years, and we look forward to focusing in, creating the best NLP experience, and continue to serve you further.