在Teams平台上更新BotFramework v4中的活动 [英] Updating an activity in BotFramework v4 on Teams platform
问题描述
我有一个机器人,该机器人是使用Bot Framework v4和NodeJS开发的,并已部署在Teams的多个渠道上.有没有办法更新机器人发送的消息?我尝试在BotFrameworkAdapter中实现updateActivity()函数.但是,它不会更新活动.
I have a bot developed using the Bot Framework v4 using NodeJS and deployed on multiple channels in Teams. Is there a way we can update a message sent by the bot? I tried implementing the updateActivity() function in the BotFrameworkAdapter. However, it does not update the activity.
我已将此卡从机器人中发送到了Teams频道.有人单击按钮时,有什么方法可以更新卡或消息(禁用按钮)?
I have this card sent from the bot to a Teams channel. When someone clicks on the button, is there a way I can update the card or the message (disabling the button)?
推荐答案
关键是要确保在使用updateActivity()
时,使用由团队渠道创建的正确活动ID.您还需要确保更新后的活动已将所有团队"数据集都设置为该数据.
The key to this is making sure that when you use updateActivity()
, you use the right activity ID that is created by the Teams Channel. You also need to make sure that the updated activity gets all of the Teams data set to it.
在onTurn
中,捕获外发活动,以便您可以轻松保存所有必要的Team Channel数据:
In onTurn
, capture outgoing activities so that you can easily save all of the necessary Teams Channel data:
public onTurn = async (turnContext: TurnContext) => {
turnContext.onSendActivities(async (ctx, activities, nextSend) => {
activities.forEach(async (activity) => {
if (activity.channelData.saveMe) {
this.savedActivity = activity;
}
});
return await nextSend();
});
- 注意:可能还有另一种方法.我只是发现这是最简单的,因为您需要至少保存所有
channelData
,conversation
信息和activity.id
- 如何存储该活动以供以后使用取决于您自己.如果将其存储在构造函数中,它将在每条消息上重新实例化(C#SDK),或者任何用户都可以对其进行更改(JS SDK).您可能会考虑
- Note: There might be another way to do this. I just found this to be the easiest, since you need to save all of the
channelData
,conversation
info, andactivity.id
, at a minimum - How you store that activity to be used later is up to you. If you store it in the constructor, it will either be re-instantiated on every message (C# SDK) or any user has the ability to change it (JS SDK). You might consider writing custom storage.
- Activities keep all channelData. By specifying a saveMe flag, we ensure we save the right activity
实例化一些关键变量:
const teamsChannel = '19:8d60061c3d10xxxxxxxxxxxxxxxx@thread.skype'; const serviceUrl = 'https://smba.trafficmanager.net/amer/';
- 注意:获取这些变量的最简单方法是将Teams的消息发送到机器人,同时在传入的
activity
上设置断点
-
serviceUrl
可能因地理位置而异 - Note: the easiest way to get these variables is to send a message from Teams to the bot while putting a breakpoint on the incoming
activity
serviceUrl
likely varies by geo region
发送第一个活动并存储ID:
Send the first activity and store the ID:
// This ensures that your bot can send to Teams turnContext.activity.conversation.id = teamsChannel; turnContext.activity.serviceUrl = serviceUrl; MicrosoftAppCredentials.trustServiceUrl(serviceUrl); // Add the saveMe flag yourActivity.channelData = { saveMe: true }; const response = await turnContext.sendActivity(yourActivity); this.activityToUpdateId = response.id;
- 如何存储该ID以供以后使用取决于您.如果将其存储在构造函数中,它将在每条消息上重新实例化(C#SDK),或者任何用户都可以对其进行更改(JS SDK).您可能会考虑
- How you store that ID to be used later is up to you. If you store it in the constructor, it will either be re-instantiated on every message (C# SDK) or any user has the ability to change it (JS SDK). You might consider writing custom storage.
更新您保存的活动:
// New data const card2 = CardFactory.adaptiveCard(adaptiveCard2); // Set the saved activity.id and new activity data (an adaptiveCard, in this example) this.savedActivity.id = this.activityToUpdateId; this.savedActivity.attachments = [card2];
发送更新:
await turnContext.updateActivity(this.savedActivity);
- 注意:您可以使用任何内容更新活动.我换出了完全不同的自适应卡
之前:
之后:
这篇关于在Teams平台上更新BotFramework v4中的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Note: There might be another way to do this. I just found this to be the easiest, since you need to save all of the