Bot Framework Teams环境中Hero Card点击后activity.value为空问题求助
Teams中Hero Card的messageBack按钮无法传递value的问题解决
这个问题我之前也遇到过,核心原因是Microsoft Teams对Bot Framework的messageBack按钮的处理逻辑和模拟器/WebChat存在差异——Teams并不会把按钮的value直接赋值给activity.value,这就是为什么你在Teams里拿到的是空对象{}。下面给你两个可行的解决方案:
方案1:调整messageBack按钮的配置,适配Teams的行为
Teams要求messageBack按钮额外配置text属性,并且建议把value设为对象格式(而非字符串),这样才能正确传递数据。修改你的Hero Card按钮代码如下:
CardFactory.actions([ { type: 'messageBack', title: 'Create Generic Service Request', text: 'Create Generic Service Request', // 新增text属性 value: { action: 'Create Generic Service Request' } // 改用对象存储值 }, { type: 'messageBack', title: 'Create Application Service Request', text: 'Create Application Service Request', value: { action: 'Create Application Service Request' } }, { type: 'messageBack', title: 'Create Virtual Desktop Service Request', text: 'Create Virtual Desktop Service Request', value: { action: 'Create Virtual Desktop Service Request' } } ])
然后在classifySRDialog中,通过channelData.postBack获取传递的value(Teams会把messageBack的value放在这个路径下):
async classifySRDialog(stepContext) { console.log('INSIDE CLASSIFY SR DIALOG'); // 从Teams的channelData中获取value const postBackData = stepContext.context.activity.channelData?.postBack; stepContext.values.classifiedSR = postBackData?.action; console.log(stepContext.values.classifiedSR); if (stepContext.values.classifiedSR === 'Create Generic Service Request') { return await stepContext.beginDialog('createGenericSRDialog'); } else if (stepContext.values.classifiedSR === 'Create Application Service Request') { console.log('Inside Application SR'); return await stepContext.beginDialog('createApplicationDialog'); } else if (stepContext.values.classifiedSR === 'Create Virtual Desktop Service Request') { return await stepContext.beginDialog('createVDIDialog'); } }
方案2:改用自适应卡片(Adaptive Card)替代Hero Card
这是更推荐的方案,因为自适应卡片在Teams中的交互支持更完善,Action.Submit按钮的data会直接映射到activity.value,和模拟器/WebChat的行为完全一致。
替换你的Hero Card生成逻辑为自适应卡片:
const adaptiveCard = { type: 'AdaptiveCard', version: '1.0', body: [ { type: 'TextBlock', text: 'Service Requests', size: 'Large', weight: 'Bolder' }, { type: 'TextBlock', text: 'Please choose the below options to create the appropriate service Requests' }, { type: 'Image', url: 'https://www.usnews.com/dims4/USNEWS/65f1856/2147483647/thumbnail/970x647/quality/85/?url=http%3A%2F%2Fcom-usnews-beam-media.s3.amazonaws.com%2Fbe%2Fdd%2F8f25b4174b6398285139452fb7d5%2F190313-collegeapplication-stock.jpg' } ], actions: [ { type: 'Action.Submit', title: 'Create Generic Service Request', data: { action: 'Create Generic Service Request' } }, { type: 'Action.Submit', title: 'Create Application Service Request', data: { action: 'Create Application Service Request' } }, { type: 'Action.Submit', title: 'Create Virtual Desktop Service Request', data: { action: 'Create Virtual Desktop Service Request' } } ] }; await stepContext.context.sendActivity({ attachments: [CardFactory.adaptiveCard(adaptiveCard)] });
然后在classifySRDialog中,直接从activity.value读取数据即可:
async classifySRDialog(stepContext) { console.log('INSIDE CLASSIFY SR DIALOG'); console.log(stepContext.context.activity.value); stepContext.values.classifiedSR = stepContext.context.activity.value.action; console.log(stepContext.values.classifiedSR); if (stepContext.values.classifiedSR === 'Create Generic Service Request') { return await stepContext.beginDialog('createGenericSRDialog'); } else if (stepContext.values.classifiedSR === 'Create Application Service Request') { console.log('Inside Application SR'); return await stepContext.beginDialog('createApplicationDialog'); } else if (stepContext.values.classifiedSR === 'Create Virtual Desktop Service Request') { return await stepContext.beginDialog('createVDIDialog'); } }
这个方案不需要额外适配Teams的特殊逻辑,代码更通用,后续扩展其他交互也更方便。
内容的提问来源于stack exchange,提问作者Arulvelu
相关产品推荐
相关产品推荐

