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

Raj*_*sal 3 c# botframework microsoft-teams adaptive-cards

我已经发送了包含捕获详细信息和按钮的卡片。从任务模块单击提交后,该模块将通过 http API 保存详细信息,此处的活动类型为“调用”。现在我必须更新现有的自适应卡。

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

                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);
Run Code Online (Sandbox Code Playgroud)

010*_*011 5

这涉及几个步骤。

  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'}"
        }
    }
    };
    
    Run Code Online (Sandbox Code Playgroud)
  2. 发送消息后保持自适应卡 uniqueId 和消息 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
    
    Run Code Online (Sandbox Code Playgroud)
  3. 当用户单击 UpdateMe 操作按钮时,检查映射uniqueId(这将在 中activity.Value)。

  4. 创建新卡并connector.Conversations.UpdateActivityAsync使用更新的代码进行呼叫。

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