1

대화 서비스에서 샘플 교육 데이터를 만들고 이제 해당 서비스에 대한 게시 통화를 만들려고합니다. 노드 js를 사용하여 채팅 응용 프로그램을 만듭니다. 나는 게시물 호출을 만들었지 만 예상대로 작동하지 않습니다. 모든 호출에 대한 기본 응답을 제공합니다.노드 js에서 게시 통화를 만들 때 Watson 대화가 모든 요청에 ​​대해 동일한 기본 응답을 반환하는 이유

나는 흐름을 수행하기 위해 다음 호출에 대한 응답에서 얻은 컨텍스트 값을 전달해야한다는 것을 알게되었다. 그러나 어떻게해야하는지 잘 모르겠다. 누군가 나를 도와 줄 수 있어요. 다음은 누군가가 우리가 help.U 테스트 현지에서 코드를 실행할 수있는 사람을 work..Can 만들 수있는 컨텍스트를 전달할 수있는 방법을 좀 도와 줄래 내 코드

var express = require('express'); 
var conversationV1 = require('watson-developer-cloud/conversation/v1'); 
var bodyParser = require('body-parser'); 
var app = express(); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: false 
})); 
var conversation = new conversationV1({ 
    username: 'xxxxxx-1a06-4a90-xxxxx-xxxxxxxxxxx', 
    password: 'xxxxxxxxxx', 
    version_date: conversationV1.VERSION_DATE_2016_09_20 
}); 
const updateMessage = (input, response) => { 
    var responseText = null; 
    if (!response.output) { 
     response.output = {}; 
    } else { 
     return response; 
    } 
    if (response.intents && response.intents[0]) { 
     var intent = response.intents[0]; 
    if (intent.confidence >= 0.75) { 
     responseText = 'I understood your intent was ' + intent.intent; 
    } else if (intent.confidence >= 0.5) { 
     responseText = 'I think your intent was ' + intent.intent; 
    } else { 
     responseText = 'I did not understand your intent'; 
    } 
} 
response.output.text = responseText; 
return response; 
}; 

app.post('/api/message', (req, res, next) => { 
    const workspace = '254654de-2bfe-423a-92ec-6aa66620625a'; 
    if (!workspace || workspace === '<workspace-id>') { 
     return res.json({ 
      output: { 
       text: 'Please check the workspace' 
      } 
     }); 
    } 
    const payload = { 
     workspace_id: workspace, 
     input: req.body.input || {}, 
     context: req.body.context || {} 
    }; 

    // Send the input to the conversation service 
    conversation.message(payload, (error, data) => { 
     if (error) { 
      return next(error); 
     } 
     return res.json(updateMessage(payload, data)); 
    }); 
}); 

app.listen(process.env.PORT || 3000); 

exports.app = app 

입니다.

+0

이것은 대화의 단순한 사본입니다. 간단합니까? 그래서 그것은 밖으로 작동합니다. 대화 ID가 변경되지 않는 것을 확인하는 것이 좋습니다. 그렇다면 앱에 문제가 있습니다. 그렇지 않은 경우 대화에 문제가 있으므로 디버그해야합니다. –

+0

@ SimonO'Doherty 대화 ID가 매번 변경 중입니다. – user7350714

+0

대화 단순 응용 프로그램을 변경 한 경우 거기에서 확인하십시오. 게시 한 내용으로 계속 진행하기에 충분하지 않습니다. –

답변

1

api/message 끝점에 게시 할 때마다 컨텍스트를 보내야합니다. 처음에는 컨텍스트가 없습니다. Watson Conversation에서 컨텍스트를 만들고 반환 한 다음이 응답으로 반환합니다. res.json(updateMessage(payload, data))

반환되는 JSON 개체에는 컨텍스트 속성이 있어야합니다. 호출자는 해당 컨텍스트를 저장 한 후 다음 호출에서 게시해야합니다.

FIRST CALL :

resp = POST api/message {} 

STORE 문맥 :

ctxt = resp.context 

다음 CALL :

그래서, 발신자 코드는이 (의사)처럼 보일 것이다
resp = POST api/message {context: ctxt} 
,

STORE 컨텍스트 (EVERY TIME) :

ctxt = resp.context 

항상 서버에 의해 반환 된 컨텍스트 호출자의 컨텍스트를 업데이트합니다. 새로운 사용자가 생길 때마다 다시 시작합니다.

발신자의 코드가 표시되지 않으므로 문제가 아닌지 확실하지 않습니다.

0

같은 문제가있었습니다. 여기에 내가 무슨 짓을했는지 있습니다 :

app.post('/api/message', function (req, res) { 
var workspace = process.env.WORKSPACE_ID || '<workspace-id>'; 
if (!workspace || workspace === '<workspace-id>') { 
return res.json({ 
    'output': { 
    'text': 'Config workspace' 
    } 
}); 
} 

var context; 

var payload = { 
workspace_id: workspace, 
context: {}, 
input: {} 
}; 

if (req.body) { 
if (req.body.input) { 
    payload.input = req.body.input; 
} 
if (req.body.context) { 
    // The client must maintain context/state 
    payload.context = req.body.context; 
} 
} 

conversation.message(payload, function (err, data) { 
if (err) { 
    return res.status(err.code || 500).json(err); 
} 
context = data.context; 
return res.json(updateMessage(payload, data)); 

}); 
}); 

function updateMessage(input, response) { 
var responseText = null; 
if (!response.output) { 
response.output = {}; 
} else { 
return response; 
} 
if (response.intents && response.intents[0]) { 
var intent = response.intents[0]; 
if (intent.confidence >= 0.75) { 
    responseText = 'I understood your intent was ' + intent.intent; 
} else if (intent.confidence >= 0.5) { 
    responseText = 'I think your intent was ' + intent.intent; 
} else { 
    responseText = 'I did not understand your intent'; 
} 
} 
response.output.text = responseText; 
return response; 
} 
module.exports = app; 

그냥 상황 단어를 클릭, 당신은 같은 맥락이 기능을 통해 전달하고 있는지 확인할 수 있습니다, 코드에 비주얼 스튜디오를 사용하는 경우. 나는 프로가 아니지만 그것은 나를 위해 일했습니다. 행운을 빕니다!