在来自 Wit.ai 聊天机器人的每个响应之前添加 'typing_on' 发件人操作气泡
Adding 'typing_on' sender action bubble before each response from Wit.ai chatbot
我使用 FB Messenger Wit.ai 和 node.js 构建了一个基于流的聊天机器人。它运行良好,但为了使交互看起来更自然,我希望我的机器人暂停片刻,并且似乎正在输入它的每个响应。
我希望 'typing' 气泡在我的机器人发送的每个响应之前短暂显示,理想情况下能够定义在发送响应之前气泡可见的时间。目前,我的对话中有部分内容是机器人连续发送消息,而且一次发送的速度太快了。
FB Messenger 发送 API 说 either the 'message' or 'sender_action' property must be set。我试过像这样设置两者:
const fbMessage = (id, text) => {
if(fruits.apples.indexOf(text) >= 0 || fruits.oranges.indexOf(text) >= 0) {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on",
message: {
attachment: {
"type": "image",
"payload": {
"url": text
}
}
},
});
} else {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on",
message: { text },
});
}
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
但我收到以下错误:
我不确定我需要做什么 - 我假设我必须设置某种 'sender_action' 机器人响应,它会在每次对话响应之前触发,但我不知道如何我会这样做。
要显示打字气泡,您只需发送 typing_on 的发件人操作即可。这会显示输入指示器长达 20 秒,在此期间您将发送您要发送的实际消息。
为此 JSON 将是:
{
"recipient":{
"id":"USER_ID"
},
"sender_action":"typing_on"
}
调用已记录 here
成功了,不知道如何控制气泡时间,但目前没问题。下面的代码将使打字气泡在我的机器人的每个响应之前短暂显示,而不会影响我的对话流程。
FB Messenger 代码:
const typingBubble = (id, text) => {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on"
});
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
const fbMessage = (id, text) => {
if(scenarioCombos.trends.indexOf(text) >= 0 || scenarioCombos.disruptions.indexOf(text) >= 0) {
var body = JSON.stringify({
recipient: { id },
message: {
attachment: {
"type": "image",
"payload": {
"url": text
}
}
},
});
} else {
var body = JSON.stringify({
recipient: { id },
message: { text },
});
}
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
Wit.ai 发送操作代码(在 'actions' 内):
send({sessionId}, {text}) {
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
return typingBubble(recipientId, text), fbMessage(recipientId, text)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
return Promise.resolve()
}
},
我使用 FB Messenger Wit.ai 和 node.js 构建了一个基于流的聊天机器人。它运行良好,但为了使交互看起来更自然,我希望我的机器人暂停片刻,并且似乎正在输入它的每个响应。
我希望 'typing' 气泡在我的机器人发送的每个响应之前短暂显示,理想情况下能够定义在发送响应之前气泡可见的时间。目前,我的对话中有部分内容是机器人连续发送消息,而且一次发送的速度太快了。
FB Messenger 发送 API 说 either the 'message' or 'sender_action' property must be set。我试过像这样设置两者:
const fbMessage = (id, text) => {
if(fruits.apples.indexOf(text) >= 0 || fruits.oranges.indexOf(text) >= 0) {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on",
message: {
attachment: {
"type": "image",
"payload": {
"url": text
}
}
},
});
} else {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on",
message: { text },
});
}
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
但我收到以下错误:
我不确定我需要做什么 - 我假设我必须设置某种 'sender_action' 机器人响应,它会在每次对话响应之前触发,但我不知道如何我会这样做。
要显示打字气泡,您只需发送 typing_on 的发件人操作即可。这会显示输入指示器长达 20 秒,在此期间您将发送您要发送的实际消息。
为此 JSON 将是:
{
"recipient":{
"id":"USER_ID"
},
"sender_action":"typing_on"
}
调用已记录 here
成功了,不知道如何控制气泡时间,但目前没问题。下面的代码将使打字气泡在我的机器人的每个响应之前短暂显示,而不会影响我的对话流程。
FB Messenger 代码:
const typingBubble = (id, text) => {
var body = JSON.stringify({
recipient: { id },
"sender_action":"typing_on"
});
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
const fbMessage = (id, text) => {
if(scenarioCombos.trends.indexOf(text) >= 0 || scenarioCombos.disruptions.indexOf(text) >= 0) {
var body = JSON.stringify({
recipient: { id },
message: {
attachment: {
"type": "image",
"payload": {
"url": text
}
}
},
});
} else {
var body = JSON.stringify({
recipient: { id },
message: { text },
});
}
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
Wit.ai 发送操作代码(在 'actions' 内):
send({sessionId}, {text}) {
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
return typingBubble(recipientId, text), fbMessage(recipientId, text)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
return Promise.resolve()
}
},