我是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中有不同的方法吗?
答案 0 :(得分:0)
实际上,Gupshup的模板并不关心发送响应的意图。模板只是从API调用中获取响应,并允许您根据需要解析响应。
现在在MSbot框架中,如果你想获得 Ent_1 的值,那么你可以使用下面的示例代码,考虑 Flow_1 是包含实体的意图<强> 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');
}
});
您也可以查看这将有用的blog。