Messenger First App 的 Facebook Bot 教程 - firstEntityValue 未定义
Facebook Bot for Messenger First App tutorial - firstEntityValue is not defined
我正在关注此处的 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();
有什么帮助吗?
遇到同样的问题,找到这里定义的函数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 或将其作为模块包含。
我正在关注此处的 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();
有什么帮助吗?
遇到同样的问题,找到这里定义的函数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 或将其作为模块包含。