0

I have trouble to understand when the actions are triggered by the Node.js SDK. At first, I thought that an action is only triggered when we explicitely call it using the story Web page.

I just realized that this is not the case.

For instance, when a user's context is not properly cleared, some actions may be run instead of another (or sometimes two actions may be run for a single message). Even if some actions are not part of the current story.

What are exactly the conditions to trigger an action in the Node.js SDK ?

Nitsuja
  • 73
  • 6

2 Answers2

1

Here is something that I have setup for an FB Bot, so you will need to change the recipientId mechanism to suit your needs.

// CALL THIS TO SEND MESSAGES TO WIT 
function sendToWit(sessionId, messageText) {
    // This will run all actions until nothing left to do
    wit.runActions(sessionId, // Current session
        messageText, sessions[sessionId].context // Current session state
    ).then(function (context) {
        // Waiting for further messages to proceed.
        if (context['done']) {
            delete sessions[sessionId];
        }
        // Updating current session state
        sessions[sessionId].context = context;
    }).catch(function (err) {
        console.error('WIT ERROR MSG: ', err.stack || err);
    });
}
//Wit Actions
var actions = {
    // Basic text message response 
    send: function send(request, response) {
        var sessionId = request.sessionId,
              context = request.context,
              entities = request.entities,
              recipientId = sessions[sessionId].fbid,
              text = response.text;
              return new Promise(function (resolve, reject) {
                callMySendAPI(recipientId, text);
                return resolve();
            });
    }
    , // All of your custom actions will go here ie;
    myCustomAction: function (request) {
        console.log('myCustomAction Called');
        var sessionId = request.sessionId;
        var recipientId = sessions[sessionId].fbid;
        return new Promise(function (resolve, reject) {
            //Call some API and do lookup by recipientId, you could lookup by anything ie; context.city for weather lookup etc..
            callSomeAPI(recipientId).then(function (result) {
                context.something = result.something; //Parsed JSON
                return Promise.resolve(context);
            }).catch(error => {
                // something went wrong
            });
            return resolve(context);
        });
    }
}
Ed Barahona
  • 721
  • 5
  • 7
0

The bot is going to try all the stories until it finds one that match, if it doesn't it replies the last one it finds.

So I did a story that I called no_match which calls an action on my client. wit story You have to make sure too that in the Understanding tab, Search Strategy and Values are correctly set.

eXa
  • 618
  • 8
  • 18
  • Thanks. Yes I used the same method too. However, I was talking about the actions in the node.js SDK, not the stories. – Nitsuja Nov 04 '16 at 12:38
  • You define in your node.js code the actions and then in the stories you can use them. If you create an action `myAction` in your story and you copy pasted these actions https://github.com/wit-ai/node-wit#response it will execute the action named `myAction` It's always the wit reply that execute actions and if it executes an unexpected action it's because it thought it was the correct one. – eXa Nov 09 '16 at 23:43
  • Yes you're right. The problem is not the SDK as I first thought but Wit.ai itself since it does not always always predict the right actions. Thanks for the help. – Nitsuja Nov 10 '16 at 17:49
  • Glad it I could be of help, could you accept the answer. – eXa Nov 17 '16 at 00:51