c# - 如何让 BOT 发起对话
问题描述
我创建了一个使用新的多圈 QnAmaker 功能的 QnABot。BOT 在与模拟器一起使用时可以很好地启动对话,但在 Iframe 或 Azure 测试环境中使用时则不行。任何人都可以帮助我了解我需要在代码中添加或更改什么以使其启动。澄清一下,当我在本地运行代码时,它可以工作。它不适用于 iframe 或类似内容
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
// Greet anyone that was not the target (recipient) of this message.
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Welcome to the IBC Leave Bot, I can help you answer questions about your leave.\n\n Type HELP to get some ideas about what to ask me"), cancellationToken);
}
}
}
}
}
解决方案
你不能从服务器端做任何事情,你必须从客户端发起对话。在 Azure 测试环境或 Iframe(直连)上,它在您发送第一条消息时完成。
这是嵌入机器人的 html 页面示例
<!DOCTYPE html>
<html>
<head>
<title>
chatbot
</title>
<meta charset="UTF-8">
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
function guid() {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
var userId = guid().toUpperCase();
var userName = 'User-' + Math.floor((1 + Math.random()) * 10000);
var secret = 'XXXXXX-BotSecret-XXXXXXX';
var user = {
id: userId,
name: userName
};
var bot = {
id: 'Demo-WebAppBot',
name: ' Demo ChatBot'
};
var botConnection = new BotChat.DirectLine({
secret: secret,
webSocket: true
});
console.log("Init bot component");
BotChat.App({
botConnection: botConnection,
user: user,
bot: bot,
resize: 'detect'
}, document.getElementById("bot"));
<!-- Conversation is initiated here by sending a dummy message to the bot -->
botConnection.postActivity({ type: "event", from: user, name: "firstMessage", value: "ping" }).subscribe(id => console.log("Conversation updated"));
</script>
</body>
</html>
推荐阅读
- css - 标签内文本的css选择器并使用之前和之后
- html - writeHTML 后更改页面格式
- angularjs - 在旧版 AngularJS 网站 (Java/Spring) 中集成 Webpack
- time - 如何将一个时域中的时钟转换为另一个时域?
- version-control - 控制版的优势还是非优势?
- java - PDFBox 文本矩阵缩放行为
- javascript - 通过 if 启用和禁用按钮
- python - 如何在不每次都对模型收费的情况下进行预测 - tensorflow?
- html - 导航栏切换器弄乱了导航栏品牌对齐
- database - 如何在 Go DRY 中扫描数据库行?