【问题标题】:How to update an adaptive card which is already sent to user from BOT?如何更新已从BOT发送给用户的自适应卡?
【发布时间】:2019-08-27 04:18:40
【问题描述】:

我已经发送了带有捕获详细信息和按钮的卡片。单击任务模块中的提交后,将通过 http API 保存详细信息,活动类型为 Invoke。 现在我必须更新现有的自适应卡片。

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

                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);

【问题讨论】:

    标签: c# botframework microsoft-teams adaptive-cards


    【解决方案1】:

    这涉及几个步骤。

    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. 发送消息后保持自适应卡片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
      
    3. 当用户单击 UpdateMe 操作按钮时,检查 uniqueId 的映射(这将在 activity.Value 中)。

    4. 创建新卡并使用更新的代码致电connector.Conversations.UpdateActivityAsync。

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

    【讨论】:

      【解决方案2】:

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

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

      Code snippet

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-25
        • 2020-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-04
        • 2020-12-09
        • 2020-10-19
        相关资源
        最近更新 更多