回复消息并等待自适应卡片操作

Reply to message and wait for adaptive card action

Teams Dev Docs 中的对话页面列出了我正在尝试实现的场景:

Updating messages

Rather than have your messages be static snapshots of data, your bot can dynamically update messages inline after sending them. You can use dynamic message updates for scenarios such as poll updates, modifying available actions after a button press, or any other asynchronous state change.

在我的代码中,我创建了一条消息并附加了一张自适应卡片。

var cardReply = activity.CreateReply();
cardReply.AddAdaptiveCard(card);

如果我使用 PostAsync 回复用户,则我不会收到更新回复所需的 ResponseResourceId

// PostAsync return Task, no ResourceResponse
await context.PostAsync(cardReply);

// OnDataItemInput is called when Action.Submit is triggered
context.Wait(this.OnDataItemInput);

如果相反,我回复 activity,则不会调用延续委托 (context.Wait)。执行的延续是堆栈上的前一个对话框。

// OnDataItemInput is never called 
context.Wait(this.OnDataItemInput);

ConnectorClient connector = 
    new ConnectorClient(new Uri(activity.ServiceUrl));

var cardReplyResource = await connector
    .Conversations
    .ReplyToActivityAsync(cardReply);

如何发送带有输入项的 AdaptiveCard 并让机器人等待 Action.Submit 响应?

用户按下提交按钮生成的 activity 消息,由您的机器人接收,将有一个 replyToId 字段 -- 这是您要更新的消息的 ID。

如果您需要一种方法来区分这些 activity 消息与其他消息,您可以为 Action.Submit 按钮提供一个 id 或使用它的 data 字段 - value activity 的对象会告诉你。