我正在使用webchat V3和bot框架V3。是否有可能向用户发送一个按钮,以关闭网页聊天?
发布于 2019-08-09 23:10:39
按照要求,这是v3解决方案。关键元素是.filter和.subscribe。筛选器侦听从bot到页面的传入活动。订阅页面上的活动和事件,允许您在向bot发送数据之前对它们进行操作。
这个例子是“使用”TJ的'imBack‘类型的英雄卡片。或者,如果你不想让机器人发短信给用户,显示你的选择,你可以发送一个'postBack‘。在这种情况下,将不显示该值,并要求您对略有不同的活动属性(channelData和value)进行筛选。
请注意,为了简单起见,我在这里实例化直线。在生产中,您将希望将您的秘密交换为令牌,以减轻任何安全问题。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Bot Chat</title>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
<style>
#botchat {
border: 1px solid #333;
float: left;
height: 600px;
position: relative;
width: 460px;
}
</style>
</head>
<body>
<div id="botchat"></div>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
const params = BotChat.queryParams(location.search);
const user = {
id: params['userid'] || 'userid',
name: params['username'] || 'username'
};
const bot = {
id: 'bot',
name: 'bot'
};
window['botchatDebug'] = params['debug'] && params['debug'] === 'true';
const botConnection = new BotChat.DirectLine({
secret: 'xxxxxxx'
});
BotChat.App({
bot: bot,
botConnection: botConnection,
user: user
}, document.getElementById('botchat'));
botConnection.activity$
.filter(function (activity) {
if (activity.type === 'message' && activity.text === 'close' ) {
window.close();
}
})
.subscribe(function (activity) {
// Make function calls and track activity/events from the page to the bot
});
</script>
</body>
</html>希望得到帮助!
发布于 2019-08-09 16:50:17
我认为这在Web v3中是不可能的;但是,在Web v4中,您可以使用卡片操作中间件来更改操作默认行为。然后,你可以通过机器人发送一张带有按钮的卡片来关闭窗口。
网络聊天v4
const cardActionMiddleware = () => next => card => {
const { cardAction: { value }} = card;
if (value === 'close') {
window.close();
}
next(card);
}
window.WebChat.renderWebChat({
cardActionMiddleware,
directLine
}, document.getElementById('webchat'));Bot v4 (节点)
const card = CardFactory.heroCard(
'Close Window',
null,
CardFactory.actions([
{
type: 'imBack',
title: 'close',
value: 'Close'
}])
);
await context.sendActivity({ attachments: [card] });注意,Web v3目前处于日落状态,不再受支持,但是Web v4与v3机器人兼容。有关更多细节,请查看迁移文档。
希望这能有所帮助!
https://stackoverflow.com/questions/57423595
复制相似问题