如何更新已从 BOT 发送给用户的自适应卡?

How to update an adaptive card which is already sent to user from BOT?

我已经发送了带有捕获详细信息的卡片,并且 buttons.After 从任务模块单击提交,这将通过 http API 保存详细信息,这里 activity 类型是调用。 现在我必须更新现有的自适应卡。

我有更新消息的代码,但如何更新卡片或重新发送卡片。

                connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                reply = activity.CreateReply($"You sent {activity.Text} which was {activity.Text.Length} characters");
                var msgToUpdate = await connector.Conversations.ReplyToActivityAsync(reply);
                Activity updatedReply = activity.CreateReply($"This is an updated message");
                await connector.Conversations.UpdateActivityAsync(reply.Conversation.Id, msgToUpdate.Id, updatedReply);

这涉及几个步骤。

  1. 创建自适应卡片并在自适应卡片操作中添加唯一 ID (GUID)。

    var Card = new AdaptiveCard()
    {
          Body = new List<AdaptiveElement>()
    {
        new AdaptiveTextBlock(){Text="This is a test adaptive card"}
    },
    Actions = new List<AdaptiveAction>()
    {
        new AdaptiveSubmitAction()
        {
            Title="UpdateMe",
            DataJson= @"{'id':'uniqueId'}"
        }
    }
    };
    
  2. 发送消息后保持自适应卡唯一ID和消息ID的映射。

    connector = new ConnectorClient(new Uri(activity.ServiceUrl));
    reply = activity.CreateReply();
    reply.Attachments.Add(Card.ToAttachment());
    var msgToUpdate = await connector.Conversations.ReplyToActivityAsync(reply);
    // Keep mapping of uniqueId and messageToUpdate.Id
    // UniqueId1 => messageId1
    // UniqueId2 => messageId2
    
  3. 当用户单击 UpdateMe 操作按钮时,检查 uniqueId 的映射(这将在 activity.Value 中)。

  4. 创建新卡并使用更新后的代码调用 connector.Conversations.UpdateActivityAsync

如果您需要更多详细信息,请告诉我们。

您可以在 MS Teams 等频道中更新自适应卡,而不是在直线上。要更新卡片,您需要访问器来存储发送当前消息时生成的 ActivityId。

这里我提供了更新消息的方式

Code snippet