3

I'm trying to write an example app in wit.ai. I followed the quickstart app using node.js client that is shown at https://wit.ai/docs/quickstart. The example shown there has only one custom action. But when I try to add a new story and a new action, I see that the context is being shared between the stories. This is causing wrong behaviour(a custom action from another story is being executed).

I cannot find any example with multiple custom actions and stories. Are there any node.js or python examples other than the ones from wit.ai websites?

user3344591
  • 567
  • 5
  • 21

2 Answers2

3

You need to create a context for each session, and this is a quick example (from https://github.com/wit-ai/node-wit/blob/master/examples/messenger.js):

const findOrCreateSession = (fbid) => {
  let sessionId;
  // Let's see if we already have a session for the user fbid
  Object.keys(sessions).forEach(k => {
    if (sessions[k].fbid === fbid) {
      // Yep, got it!
      sessionId = k;
    }
  });
  if (!sessionId) {
    // No session found for user fbid, let's create a new one
    sessionId = new Date().toISOString();
    sessions[sessionId] = {
      fbid: fbid,
      context: {         // New context per session id.
        _fbid_: fbid
      }
    }; // set context, _fid_
  }
  return sessionId;
};

You can find a working example at https://github.com/hunkim/Wit-Facebook.

Sung Kim
  • 8,417
  • 9
  • 34
  • 42
3

I suppose wit engine don't store context on their side. You 'merge' function must merge entities in different ways, depending on your app logic.

But if you story is completed, you need to clear context for next stories. I added a built-in function clear-context and call this function from wit as action.

Check out my example. It's not an official api, but you can understand how wit http api works.

Vitaly Domnikov
  • 306
  • 2
  • 4