首先,我想提一下,此功能已经是 BotFramework-WebChat 存储库中正在处理的功能请求。我不知道这方面的预计到达时间,所以不要计划很快。但是,请留意。
这可以通过一点黑客攻击来实现。简而言之,这些是您的步骤:
- 在您的自适应卡中创建一个“触发器”属性并为其分配一个唯一值。
- 创建一个查找触发器值的
activityMiddleware。收到后,通过附加 id 更新将容纳传入自适应卡的元素。
- 将纯 CSS 添加到您的 html 中,根据第 2 步中附加的
id 设置卡片样式。
- 将
activityMiddleware 对象添加到直线。
希望有帮助!
下面是示例代码:
mainDialog.js - 从我的机器人发送的自适应卡片
async basicAdaptiveCard ( stepContext ) {
const card = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"trigger": "ServiceCardTrigger",
"body": [
{
"type": "TextBlock",
"text": "Hi!! How can I help you today?",
"weight": "Bolder",
"size": "Medium"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Placeholder Message",
"data": "close"
}
]
}
const ac_card = CardFactory.adaptiveCard( card );
await stepContext.context.sendActivity(
{
attachments: [ ac_card ]
}
);
return { status: DialogTurnStatus.waiting };
}
index.html - 仅显示基本信息。在此,我通过一个包含“服务详细信息”值的按钮进行跟踪。它很简单,但适用于演示。必要时更新。
<style>
#ServiceCard {
background-color: #e6eaed;
color: #323232;
}
#ServiceCard:hover p {
color: red
}
</style>
[...]
<script type="text/babel">
[...]
const activityMiddleware = () => next => card => {
const { activity: { from, type, attachments } } = card;
if (type === 'message' && from.role === 'bot') {
if(attachments && attachments[0].content.trigger === 'ServiceCardTrigger') {
let children = document.getElementsByClassName('ac-adaptiveCard');
for (let i = 0, j = children.length; i <= j; i++) {
if(i === j - 1) {
let child = children[i];
if(child.lastChild.innerHTML.includes('Service details')) {
child.id = 'ServiceCard';
}
}
};
}
}
else {
return next(card)
}
}
[...]
window.ReactDOM.render(
<ReactWebChat
activityMiddleware={ activityMiddleware }
directLine={ directLine }
username={'johndoe'}
/>,
document.getElementById( 'webchat' )
);
[...]
</script>