我正在关注Facebook Bot for Messenger First App教程: https://wit.ai/docs/quickstart
案例是,在克隆项目(https://github.com/wit-ai/node-wit.git)并按照第一步后,当我运行机器人时,我收到此错误: firstEntityValue未定义
我的代码:
'use strict';
const Wit = require('../').Wit;
const token = (() => {
if (process.argv.length !== 3) {
console.log('usage: node examples/weather.js <wit-token>');
process.exit(1);
}
return process.argv[2];
})();
const actions = {
say: (sessionId, msg, cb) => {
console.log(msg);
cb();
},
merge: (context, entities, cb) => {
//Retrieve the location entity and store it into a context field
const loc = firstEntityValue(entities, 'location');
if(loc) {
context.loc = loc;
}
cb(context);
},
error: (sessionId, msg) => {
console.log('Oops, I don\'t know what to do.');
},
'fetch-forecast': (context, cb) => {
// Here should go the api call, e.g.:
// context.forecast = apiCall(context.location)
context.forecast = 'cloudy';
cb(context);
},
};
const client = new Wit(token, actions);
client.interactive();
任何帮助?
答案 0 :(得分:3)
遇到同样的问题,找到了这里定义的函数https://github.com/wit-ai/node-wit/blob/1c4aedea09332b471d8c45060a1dc1f91f65eff5/examples/joke.js
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;
};
您需要将其添加到index.js或将其作为模块包含。