wit.ai + 基于插槽的机器人 + 在客户端中保存实体值
我正在 wit.ai 中试用一个示例,这是链接:
'use strict';让智慧=空;让交互 = null;尝试 {//如果从 repo 运行机智=要求('../').机智;交互 = 要求('../').interactive;} 抓住 (e) {Wit = require('node-wit').Wit;交互 = 需要('node-wit').interactive;}常量 accessToken = (() => {if (process.argv.length !== 3) {console.log('用法:节点示例/creditcardbalance.js <wit-access-token>');process.exit(1);}返回进程.argv[2];})();//快速入门示例//见 https://wit.ai/ar7hur/quickstartconst firstEntityValue =(实体,实体)=>{const val = 实体 &&实体[实体] &&Array.isArray(entities[entity]) &&实体[实体].length >0&&实体[实体][0].value;如果(!val){返回空值;}返回 typeof val === 'object' ?val.value : 值;};常量动作 = {发送(请求,响应){常量 {sessionId,上下文,实体} = 请求;常量 {文本,快速回复} = 响应;返回新的承诺(功能(解决,拒绝){console.log('正在发送...', JSON.stringify(response));返回解析();});},getBalance({上下文,实体}){返回新的承诺(功能(解决,拒绝){var name = firstEntityValue(entities, "name");var last4digit = firstEntityValue(entities, "Last4digits");if (name && last4digit) {context.name = 名称;上下文.last4digit = last4digit;context.balance = 'Rs.10000' + name + last4digit;//我们应该在这里调用信用卡 API删除 context.cardnumbermissing;}别的 {context.cardnumbermissing = true;context.name = 名称;删除 context.balance;}返回解析(上下文);});},};const client = new Wit({accessToken, actions});交互式(客户端);//JavaScript 源代码
解决方案 我有一个类似的问题,发生的事情是 firstEntityValue 只从初始对话中提取,所以我实际上是在每次调用它来设置一个多变的.如果已经有上下文,我必须让函数知道上下文,而不是使用 firstEntityValue 设置这些变量.
I am trying out a sample in wit.ai, here is the link : https://wit.ai/Nayana-Manchi/CreditCardApp/stories
The first story "BalanceEnquiry" is a slot based story. The happy scenario works fine. To test "cardnumbermissing" branch, I would type in "I want my credit card balance on the card and my name is Nayana". Here the card number last 4 digits are missing. It ask for the last 4 digits of the card and then I would enter the last 4 digits of the card. But here it does not get name entity which was sent in earlier message.
How do I save the entity value "name" which was sent in the previous step? It should retain the entity value name and it should get the entered last 4 digits from the current conversation and then display the balance which is hard coded.
Code and the images attached.
It will be helpful if you share the client code (javascript) for the recipe- Build a slot-based bot.
'use strict';
let Wit = null;
let interactive = null;
try {
// if running from repo
Wit = require('../').Wit;
interactive = require('../').interactive;
} catch (e) {
Wit = require('node-wit').Wit;
interactive = require('node-wit').interactive;
}
const accessToken = (() => {
if (process.argv.length !== 3) {
console.log('usage: node examples/creditcardbalance.js <wit-access-token>');
process.exit(1);
}
return process.argv[2];
})();
// Quickstart example
// See https://wit.ai/ar7hur/quickstart
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value
;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
return new Promise(function(resolve, reject) {
console.log('sending...', JSON.stringify(response));
return resolve();
});
},
getBalance({context, entities}) {
return new Promise(function(resolve, reject) {
var name = firstEntityValue(entities, "name");
var last4digit = firstEntityValue(entities, "Last4digits");
if (name && last4digit) {
context.name = name;
context.last4digit = last4digit;
context.balance = 'Rs.10000' + name + last4digit; // we should call a credit card API here
delete context.cardnumbermissing;
}
else {
context.cardnumbermissing = true;
context.name = name;
delete context.balance;
}
return resolve(context);
});
},
};
const client = new Wit({accessToken, actions});
interactive(client);
// JavaScript source code
解决方案
I had a similar issue and what was happening was firstEntityValue only pulls from the initial conversation, so I was actually erasing previous variables each time I called it to set a variable. I had to make the function aware of the context and not set those variables using firstEntityValue if there was already a context.
相关文章