const API_AI_TOKEN = 'my token here';
const apiAiClient = require('apiai')(API_AI_TOKEN);
const apiaiSession = apiAiClient.textRequest('hi', {sessionId: 'test'});
apiaiSession.on('response', (response) => {
const result = response.result.fulfillment.speech;
console.log(result);
});
apiaiSession.on('error', error => console.log(error));
我在 Dialogflow 闲聊中配置并进行了测试,我应该收到“嘿”回复,但我得到 nothing.. 我尝试了客户端和开发 token
最佳答案
Problem is i'm obligated to end session with apiaiSession.end()
您没有义务结束 session 。您正在做的事情不是 session ,而是请求。
如果没有.end()
,请求永远不会完全发送,因此您永远不会收到响应。
I tried both client and dev token
您必须使用开发 token 。
<小时/>代码应该是这样的:
const API_AI_TOKEN = 'my token here'; // You need dev token
const apiAiClient = require('apiai')(API_AI_TOKEN);
const request = apiAiClient.textRequest('hi', {sessionId: 'test'});
request.on('response', (response) => {
const result = response.result.fulfillment.speech;
console.log(result);
});
request.on('error', error => console.log(error));
request.end(); // This.
session 由 Dialogflow 管理,因此只要您为每次用户交互发送相同的 sessionId
就可以了。
来自docs :
User entities and sessions last 30 minutes.
关于node.js - API.ai/Dialogflow 基本文本请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49762933/