0

I am new to Microsoft Bot Framework. Earlier I was using Gupshup to build my bots. Gupshup had designed the workflow in a very nice manner. I had used api.ai NLP engine with Gupshup. I want to switch and try MS Bot Framework now with api.ai.

Below is my Gupshup's code:

function MessageHandler(context, event) {
sendMessageToApiAi({
        message : event.message,
        sessionId : new Date().getTime() +'api',
        nlpToken : "74c04b2c16284c738a8dbcf6bb343f",
        callback : function(res){
             if(JSON.parse(res).result.parameters.Ent_1=="Hello"){
    context.sendResponse("Hello");
    }
}
},context);
};

function sendMessageToApiAi(options,botcontext) {
    var message = options.message; // Mandatory
    var sessionId = options.sessionId || ""; // optinal
    var callback = options.callback;
    if (!(callback && typeof callback == 'function')) {
       return botcontext.sendResponse("ERROR : type of options.callback should be function and its Mandatory");
    }
    var nlpToken = options.nlpToken;

    if (!nlpToken) {
       if (!botcontext.simpledb.botleveldata.config || !botcontext.simpledb.botleveldata.config.nlpToken) {
           return botcontext.sendResponse("ERROR : token not set. Please set Api.ai Token to options.nlpToken or context.simpledb.botleveldata.config.nlpToken");
       } else {
           nlpToken = botcontext.simpledb.botleveldata.config.nlpToken;
       }
    }
    var query = '?v=20150910&query='+ encodeURIComponent(message) +'&sessionId='+context.simpledb.roomleveldata.session+'&timezone=Asia/Calcutta&lang=en    '
    var apiurl = "https://api.api.ai/api/query"+query;
    var headers = { "Authorization": "Bearer " + nlpToken};
    botcontext.simplehttp.makeGet(apiurl, headers, function(context, event) {
       if (event.getresp) {
           callback(event.getresp);
       } else {
           callback({})
       }
    });
}

I have started off with MS bot Framework and linked with api.ai. Below is my code:

var builder = require('botbuilder');
var restify = require('restify');
var apiairecognizer = require('api-ai-recognizer');
var request = require('request');

//=========================================================
// Bot Setup
//=========================================================

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});

// Create chat bot
var connector = new builder.ChatConnector({
    appId: "8c9f2d7b-dfa6-4116-ac45-po34eeb1d25c",
    appPassword: "7CCO8vBGtdcTr9PoiUVy98tO"
});

server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector);


var recognizer = new apiairecognizer("74c04b2c16284c738a8dbcf6bb343f");
var intents = new builder.IntentDialog({
         recognizers: [recognizer]
});

bot.dialog('/',intents);



intents.matches('Flow_1',function(session, args){
    var fulfillment = builder.EntityRecognizer.findEntity(args.entities, 'fulfillment');

    if (fulfillment){
        var speech = fulfillment.entity;

        session.send(speech);
        console.log("Inside fulfillment");
    }else{
        session.send('Sorry...not sure how to respond to that');
    }
});

intents.matches('Intro',function(session, args){
    var fulfillment = builder.EntityRecognizer.findEntity(args.entities, 'fulfillment');
    if (fulfillment){
        var speech = fulfillment.entity;
        session.send(speech);
    }else{
        session.send('Sorry...not sure how to respond to that');
    }
});

intents.matches('Default Fallback Intent',function(session, args){
     var fulfillment = builder.EntityRecognizer.findEntity(args.entities, 'fulfillment');
    if (fulfillment){
        var speech = fulfillment.entity;
        session.send(speech);
    }else{
        session.send('Sorry...not sure how to respond to that');
    }
});

Now here is what I want to achieve:

JSON.parse(res).result.parameters.Ent_1 was a easy of parsing and getting the paramerters. How can I achieve something similar to that in Bot Framework? Do I have to construct a function sendMessageToApiAi() or is there a different way to achieve in MS Bot Framework?

AITea
  • 41
  • 1
  • 9

1 Answers1

0

Actually, Gupshup's template doesn't care about the intent which is sending the response. The template just gets the response from the API call and allows you to parse the response as desired.

Now in MSbot framework, if you want to get the value of Ent_1 then you can use the below sample code considering Flow_1 is the intent which will contain the entity Ent_1

intents.matches('Flow_1',function(session, args){
var fulfillment = builder.EntityRecognizer.findEntity(args.entities, 'Ent_1');

if (fulfillment){
    var speech = fulfillment.entity;

    session.send(speech);
    console.log("Inside fulfillment");
}else{
    session.send('Sorry...not sure how to respond to that');
}
});

You can also go through this blog which will help.

Shreyans
  • 1,738
  • 1
  • 13
  • 19
  • Hi Shreyans. Thanks for the answer. I have been using Gupshup from a long time. I have few important things to discuss. If possible, please send a message over personal chat. – AITea Jul 17 '17 at 13:30
  • Hi AITea, I work for Gupshup hence you can send in your queries at developer@gupshup.io – Shreyans Jul 18 '17 at 05:21
  • Hi Shreyans, Please refer to this question https://stackoverflow.com/questions/45158775/gupshup-chat-web-widget-redirection-issue as well – AITea Jul 18 '17 at 07:34