更新和删除从你的 bot 发送的邮件Update and delete messages sent from your bot

重要

本节中的代码示例基于 Bot 框架 SDK 的4.6 和更高版本。The code samples in this section are based on 4.6 and later versions of the Bot Framework SDK. 如果您正在查找早期版本的文档,请参阅文档的资源文件夹中的 " bot-V3 SDK " 一节。If you're looking for documentation for earlier versions, see the Bots - v3 SDK section in the Resources folder of the documentation.

更新邮件Updating messages

你的 bot 可以在发送邮件后动态更新邮件,而不是让你的邮件成为数据的静态快照。Rather than have your messages be static snapshots of data, your bot can dynamically update messages 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.

新邮件需要与类型中的原始邮件不匹配。The new message need not match the original in type. 例如,如果原始邮件包含附件,则新邮件可以是简单的短信。For instance, if the original message contained an attachment, the new message can be a simple text message.

若要更新现有的邮件,请将Activity具有现有活动 ID 的新对象传递UpdateActivityAsyncTurnContext该类的方法。To update an existing message, pass a new Activity object with the existing activity ID to the UpdateActivityAsync method of the TurnContext class. 请参阅 TurnContextClassSee TurnContextClass

var newActivity = MessageFactory.Text("The new text for the activity");
newActivity.Id = activityId;
await turnContext.UpdateActivityAsync(newActivity, cancellationToken);

删除邮件Deleting messages

在 Bot 框架中,每封邮件都有其自己唯一的活动标识符。In the Bot Framework, every message has its own unique activity identifier. 可以使用 Bot 框架的DeleteActivity方法删除邮件,如下所示。Messages can be deleted using the Bot Framework's DeleteActivity method as shown here.

若要删除该消息,请将该活动的 ID DeleteActivityAsync传递给TurnContext该类的方法。To delete that message, pass that activity's ID to the DeleteActivityAsync method of the TurnContext class. 请参阅 DeleteActivityAsync 方法 TurnContextSee TurnContext.DeleteActivityAsync Method

foreach (var activityId in _list)
{
    await turnContext.DeleteActivityAsync(activityId, cancellationToken);
}