Saturday 20 May 2017

newbie node.js bot integration with database lookup (looking for best practice)

OK, new to Node.js and botframework. I built first bot using Azure site and downloaded the code. Chose the Luis integrated bot template.

I understand (finally) the event driven model of node.js and the concept of callbacks.

I have the code snippet below. When Luis finds an intent of "Help" it triggers this function. In turn, I have database calls to lookup the entity. Within the database I have an entity, response (if entity is bottle, answer "Recycle").

I have that code working too.

Below the first block is a function handleHelpRequester which is the callback function, I have that working as well. Where I am a little stuck (best practice) is that in this callback function I want to send something to the session object (session.send toward the bottom of the function).

Since I don't create the session object (directly) I'm not sure on the options. Should I pass the session object to the database function, then pass it back?

Should I create a global variable and set it to the session object (I'm concerned that if multiple people are using this bot then this approach won't work).

I'm open for suggestions.

Thanks

.matches('Help', (session, args) => {
            var entities = args.entities;
            var itype = builder.EntityRecognizer.findEntity(args.entities, 'ItemTypes');


     var respondToUser = '';
        var msg = 'Initialized';

        // if there is an entity provided, perform a lookup of the entity.
        if (itype.entity !== null) {
            //Use session.sendTyping so the user thinks something is happening rather than being ignored while we do the lookup in SharePoint.
            session.sendTyping();

            //perform lookup
            respondToUser = sp.lookupEntity(itype.entity, handleHelpRequest);
        };
    })

function handleHelpRequest(err, respondToUser) {
    var msg = 'uninitialized';
    if (err = 'success') {
        console.log('Respond to user from matches:'.concat(respondToUser));
        //return from lookup
        if (respondToUser === 'No match found') {
            msg = 'I think you are asking for help, but I don\'t understand what you need help with.';
        }
        else {
            msg = 'I can help you with that, \'%s\'.', respondToUser;
        }


        console.log(msg);
        session.send(msg);

        //The following two lines are for debugging
        session.send('How may I assist you? ' + JSON.stringify(args));
        session.send('Value of entity you said:  \'%s\'.', itype.entity);
    }
    else {
        console.log('an error occurred');
    }
}



via Desperate for help

No comments:

Post a Comment