0

我是 Microsoft Bot Framework 的新手。早些时候,我使用 Gupshup 来构建我的机器人。Gupshup 以非常好的方式设计了工作流程。我曾在 Gupshup 中使用过 api.ai NLP 引擎。我现在想切换并尝试使用 api.ai 的 MS Bot Framework。

下面是我的 Gupshup 的代码:

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({})
       }
    });
}

我从 MS bot Framework 开始并与 api.ai 链接。下面是我的代码:

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');
    }
});

现在这是我想要实现的目标:

JSON.parse(res).result.parameters.Ent_1 很容易解析和获取参数。如何在 Bot Framework 中实现类似的功能?我必须构造一个函数 sendMessageToApiAi() 还是在 MS Bot Framework 中有不同的方法来实现?

4

1 回答 1

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.

于 2017-07-17T07:17:29.117 回答