我可以从其他文件调用该函数,但是由于某种原因,我的sessionPayload.context没有通过。
我正在使用Watson的中间件bot,但这是我到目前为止所掌握的。
文件1:
middleware.before = function(message, conversationPayload, callback) {
var common = require('./tools/toneAnalyzer');
queryInput = conversationPayload.input.text
common.func1(queryInput);
};
file2
module.exports = {
func1: function (queryInput,conversationPayload) {
console.log('fink1!!!!! ' + queryInput);
var toneParams = {
tone_input: { 'text': queryInput },
content_type: 'application/json'
};
toneAnalyzer.tone(toneParams, function (err, tone) {
let toneScore = ''
let toneJoy = ''
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(tone, null, 2));
const emotionTones = tone.document_tone.tones;
const len = emotionTones.length;
for (let i = 0; i < len; i++) {
if (emotionTones[i].tone_id === 'anger') {
console.log('Input = ', queryInput )
console.log('emotion_id = ', 'Emotion_id', emotionTones[i].tone_id);
toneScore = emotionTones[i].tone_id;
console.log(toneScore + ' = toneScore')
break;
}else if(emotionTones[i].tone_id === 'joy'){
toneJoy = emotionTones[i].tone_id;
}
}
}if (conversationPayload.context) {
conversationPayload.context['tone_anger'] = toneScore
conversationPayload.context['tone_joy'] = toneJoy
}
callback(null, conversationPayload);
});
}
如果(conversationPayload.context)...,则脚本运行平稳,直到获得代码为止... 那我得到 TypeError:无法读取未定义的属性'context'
如果文件1中包含所有内容,此方法将完美无缺。 我已经尝试了一些有关移动消息,回调和talkingPayload参数的实验。但没有运气。
有什么建议吗?
答案 0 :(得分:0)
我认为有人在尝试做类似的事情。 解决方案是发送2个参数sessionPayload和callback
module.exports = {
func1: function (queryInput,conversationPayload,callback) {
console.log('fink1!!!!! ' + queryInput);
var toneParams = {
tone_input: { 'text': queryInput },
content_type: 'application/json'
};
toneAnalyzer.tone(toneParams, function (err, tone) {
let toneScore = ''
let toneJoy = ''
if (err) {
console.log(err);
} else {
console.log(JSON.stringify(tone, null, 2));
const emotionTones = tone.document_tone.tones;
const len = emotionTones.length;
for (let i = 0; i < len; i++) {
if (emotionTones[i].tone_id === 'anger') {
console.log('Input = ', queryInput )
console.log('emotion_id = ', 'Emotion_id', emotionTones[i].tone_id);
toneScore = emotionTones[i].tone_id;
console.log(toneScore + ' = toneScore')
break;
}else if(emotionTones[i].tone_id === 'joy'){
toneJoy = emotionTones[i].tone_id;
}
}
}if (conversationPayload.context) {
conversationPayload.context['tone_anger'] = toneScore
conversationPayload.context['tone_joy'] = toneJoy
}
callback(null, conversationPayload);
});
}