Friday 14 April 2017

wit.ai runActions how to handle context in follow-up message

I'm using node-wit to develop a chatbot application. This is working fine mostly, but I've run into a problem with the use of the context.

I'm using the runActions api :

       this.witClient.runActions(customer._key, messageText, witContext).then((newContext => {}
)).catch(reject);

I have defined a number of actions, which set the context.

This is working fine, as long the context is taking place over one message. For example, if I were to call an action called addProduct :

addProduct({sessionId, context, text, entities}) {

                return new Promise((resolve, reject) => {

                            context.product = `myNewProduct';
                            resolve(context);

                });
            },

It will then show a message using the 'product' context key.

However, when I try to use it over 2 messages, it seems to have lost the context ( for example, when asking a multiple choice question, and then handling that response ).

If I understand how it's working correctly, then node-wit doesn't keep the context beyond messages ( I assummed this at first because I'm passing a session key ).

A solution I see is to store the resulting context ( newContext in this case) in a session/user specific way, and then restore it and pass it again when the user is sending his new message.

Meaning, something like this :

witContext = getContextFromSession();
this.witClient.runActions(customer._key, messageText, witContext).then((newContext => { setContextInSession(newContext) } )).catch(reject);

Would this be the correct way of handling it ?



via Kenny

No comments:

Post a Comment