0

I am writing my first apps with wit.ai using a node.js backend. I found some posts here similar to my question, but not really the answer : I use a socket.io to communicate with my node script. The two relevant parts of my node are :

io.sockets.on('connection', function (socket) {
    socket.on('message',
        function(data)  {
            var json = JSON.parse(data);
            var sid = json['sessionid'];
            console.log("Received on sid " + sid);
            if (_sessions[sid] == undefined)    {
                        _sessions[sid] = {};
                        _sessions[sid].context = {};
            }
            _sessions[sid].socket = socket;
            client.runActions(sid, json['text'], _sessions[sid].context, 30)
            .then((context) =>  {
                    _sessions[sid].context = context;
                }
            )
            .catch((err) =>
                {
                    console.error('Oops! Got an error from Wit: ', err.stack || err);
                }
            );
        }
    );
    }  
);

========

const actions = {
  send(request, response) {
    const {sessionId, context, entities} = request;
    const {text, quickreplies} = response;
    return new Promise(function(resolve, reject) {
        var session = _sessions[sessionId];

        console.log("-------------------------------");
        console.dir(context);
        console.log("-------------------------------");


        session.socket.emit("message", JSON.stringify(response));
      return resolve();
    });
  },
  gettaxi ({sessionid, context, text, entities}) {
      return new Promise(function(resolve, reject) {
            console.log(`Session ${sessionid} received ${text}`);
            var quand = firstEntityValue(entities, "quand");
            if (!quand && context['quand'] != undefined) quand = context['quand'];
            var depart = firstEntityValue(entities, "depart");
            var arrivee = firstEntityValue(entities, "arrivee");

            if (depart) {
                console.log("Found depart");
                context.depart = depart;
                delete context.missing_depart;
            }
            else    {
                context.missing_depart = true;
            }

            if (arrivee)    {
                console.log("Found arrivee");
                context.arrivee = arrivee;
                delete context.missing_arrivee;
            }
            else    {
                context.missing_arrivee = true;
            }

            console.dir(context);

            if (quand)  {
                console.log("Found quand");
                context.quand = quand;
                delete context.missing_quand;
            }
            else    {
                context.missing_quand = true;
            }
            return resolve(context);
        }
    );
  },

};

All is working rather good, except than my gettaxi receives a undefined sessionid. It's not the case for the send function that receives the correct sessionid.

What am I doing wrong ?

  • Is there a reason for using 'sessionId' in the send action while using 'sessionid' in the gettaxi action? – Bcf Ant Sep 28 '16 at 04:41
  • Depends on how you are invoking the actions. From the code I can see that both the methods expect different parameters. So likely, you are not invoking them in a similar fashion. That would be _relevant code_ not the action defenition – kranthi117 Sep 28 '16 at 05:06
  • Thanks so much, indeed I has a mismatch between sessionid and sessionId, works now. – Frédéric Clement Sep 29 '16 at 08:04

0 Answers0