正如 Nicolas R 所指出的,您应该使用反向渠道来执行您的询问。
您应该查看两个示例(它们实际上需要一起使用才能运行完整的示例)。它们是 backChannelBot 和 BotFramework-WebChat/samples/backchannel 样本。
从第一个示例中,您需要检查的重要代码可以在app.js 文件中找到:
//Bot listening for inbound backchannel events - in this case it only listens for events named "buttonClicked"
bot.on("event", function (event) {
var msg = new builder.Message().address(event.address);
msg.textLocale("en-us");
if (event.name === "buttonClicked") {
msg.text("I see that you just pushed that button");
}
bot.send(msg);
})
//Basic root dialog which takes an inputted color and sends a changeBackground event. No NLP, regex, validation here - just grabs input and sends it back as an event.
bot.dialog('/', [
function (session) {
var reply = createEvent("changeBackground", session.message.text, session.message.address);
session.endDialog(reply);
}
]);
//Creates a backchannel event
const createEvent = (eventName, value, address) => {
var msg = new builder.Message().address(address);
msg.data.type = "event";
msg.data.name = eventName;
msg.data.value = value;
return msg;
}
WebChat 示例中的相关代码部分是 section:
botConnection.activity$
.filter(function (activity) {
return activity.type === 'event' && activity.name === 'changeBackgroundColor';
})
.subscribe(function (activity) {
console.log('"changeBackground" received with value: ' + activity.value);
changeBackgroundColor(activity.value);
});
function changeBackgroundColor(newColor) {
document.getElementsByClassName('wc-message-groups')[0].style.backgroundColor = newColor;
}
function postButtonMessage(color) {
botConnection
.postActivity({
from: { id: 'me' },
name: 'changeBackgroundColor',
type: 'event',
value: color
})
.subscribe(function (id) {
console.log('"changeBackgroundColor" sent');
});
}