The code samples in this section are based on version 4.6 and later versions of the Bot Framework SDK. If you are looking for documentation for earlier versions, see the bots - v3 SDK section in the Legacy SDKs folder of the documentation.
Your bot can dynamically update messages after sending them instead of having them as static snapshots of data. Messages can also be deleted using the Bot Framework's DeleteActivity method.
Note
A bot can't update or delete messages sent by the user in Microsoft Teams.
Update messages
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.
It is not necessary for the new message to match the original in type. For example, if the original message contains an attachment, the new message can be a simple text message.
To update an existing message, pass a new Activity object with the existing activity ID to the UpdateActivityAsync method of the TurnContext class.
// Send initial message
var response = await turnContext.SendActivityAsync(MessageFactory.Attachment(card.ToAttachment()), cancellationToken);
var activityId = response.Id; // Fetch activity id.
// MessageFactory.Text(): Specifies the type of text data in a message attachment.
var newActivity = MessageFactory.Text("The new text for the activity");
newActivity.Id = activityId;
// UpdateActivityAsync(): A method that can participate in update activity events for the current turn.
await turnContext.UpdateActivityAsync(newActivity, cancellationToken);
To update an existing message, pass a new Activity object with the existing activity ID to the updateActivity method of the TurnContext object.
// Send initial message
var message = await context.sendActivity("<Your Message>");
var activityId = message.id; // Fetch activity id.
// MessageFactory.Text(): Specifies the type of text data in a message attachment.
const newActivity = MessageFactory.text('The new text for the activity');
newActivity.id = activityId;
// A method that can participate in update activity events for the current turn.
await turnContext.updateActivity(newActivity);
To update an existing message, pass a new Activity object with the existing activity ID to the update_activity method of the TurnContext class.
# Send initial message
message = await turn_context.send_activity("<Your Message>")
activityId = message.id # Fetch activity id.
# MessageFactory.Text(): Specifies the type of text data in a message attachment.
new_activity = MessageFactory.text("The new text for the activity")
new_activity.id = activity_id
# A method that can participate in update activity events for the current turn.
update_result = await context.update_activity(new_activity)
Note
You can develop Teams apps in any web-programming technology and directly call the Bot Connector service REST APIs. To do so, you need to implement Authentication security procedures with your API requests.
To update an existing activity within a conversation, include the conversationId and activityId in the request endpoint. To complete this scenario, you must cache the activity ID returned by the original post call.
PUT /v3/conversations/{conversationId}/activities/{activityId}
To update existing card on a button selection, pass a new Activity object with updated card and ReplyToId as activity ID to the UpdateActivityAsync method of the TurnContext class.
// Returns a message activity that contains an attachment.
var activity = MessageFactory.Attachment(card.ToAttachment());
activity.Id = turnContext.Activity.ReplyToId;
// A method that can participate in update activity events for the current turn.
await turnContext.UpdateActivityAsync(activity, cancellationToken);
To update existing card on a button selection, pass a new Activity object with updated card and replyToId as activity ID to the updateActivity method of the TurnContext object.
// MessageFactory.attachment(): Returns a message activity that contains an attachment.
const message = MessageFactory.attachment(card);
message.id = context.activity.replyToId;
// updateActivity(): A method that can participate in update activity events for the current turn.
await context.updateActivity(message);
To update existing card on a button click, pass a new Activity object with updated card and reply_to_id as activity ID to the update_activity method of the TurnContext class.
# MessageFactory.attachment(): Returns a message activity that contains an attachment.
updated_activity = MessageFactory.attachment(CardFactory.hero_card(card))
updated_activity.id = turn_context.activity.reply_to_id
# update_activity(): A method that can participate in update activity events for the current turn.
await turn_context.update_activity(updated_activity)
Note
You can develop Teams apps in any web programming technology and directly call the bot connector service REST APIs. To do this, you must implement authentication security procedures with your API requests.
To update an existing activity within a conversation, include the conversationId and activityId in the request endpoint. To complete this scenario, you must cache the activity ID returned by the original post call.
PUT /v3/conversations/{conversationId}/activities/{activityId}
To delete a message, pass that activity's ID to the DeleteActivityAsync method of the TurnContext class.
foreach (var activityId in _list)
{
// When overridden in a derived class, deletes an existing activity in the conversation.
await turnContext.DeleteActivityAsync(activityId, cancellationToken);
}
To delete a message, pass that activity's ID to the deleteActivity method of the TurnContext object.
for (let i = 0; i < activityIds.length; i++) {
// deleteActivity(): deletes an existing activity in the conversation.
await turnContext.deleteActivity(activityIds[i]);
}
Källan för det här innehållet finns på GitHub, där du även kan skapa och granska ärenden och pull-begäranden. Se vår deltagarguide för mer information.