I have a bot developed using the Bot Framework v4 using NodeJS and deployed on multiple channels in Teams. Is there a way we can update a message sent by the bot? I tried im
The key to this is making sure that when you use updateActivity()
, you use the right activity ID that is created by the Teams Channel. You also need to make sure that the updated activity gets all of the Teams data set to it.
In onTurn
, capture outgoing activities so that you can easily save all of the necessary Teams Channel data:
public onTurn = async (turnContext: TurnContext) => {
turnContext.onSendActivities(async (ctx, activities, nextSend) => {
activities.forEach(async (activity) => {
if (activity.channelData.saveMe) {
this.savedActivity = activity;
}
});
return await nextSend();
});
channelData
, conversation
info, and activity.id
, at a minimumInstantiate some key variables:
const teamsChannel = '19:8d60061c3d10xxxxxxxxxxxxxxxx@thread.skype';
const serviceUrl = 'https://smba.trafficmanager.net/amer/';
activity
serviceUrl
likely varies by geo regionSend the first activity and store the ID:
// This ensures that your bot can send to Teams
turnContext.activity.conversation.id = teamsChannel;
turnContext.activity.serviceUrl = serviceUrl;
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
// Add the saveMe flag
yourActivity.channelData = { saveMe: true };
const response = await turnContext.sendActivity(yourActivity);
this.activityToUpdateId = response.id;
Update your saved activity:
// New data
const card2 = CardFactory.adaptiveCard(adaptiveCard2);
// Set the saved activity.id and new activity data (an adaptiveCard, in this example)
this.savedActivity.id = this.activityToUpdateId;
this.savedActivity.attachments = [card2];
Send the update:
await turnContext.updateActivity(this.savedActivity);
Before:
After:
I've tried this using the middleware but keep getting: "The bot is not part of the conversation roster". Question: My bot is updating a message that a user wrote, so do I need special permissions?
let ActivityID = context.activity.conversation.id.split("=")[1];
let updatedActivity: Partial<Activity> = {
"id": ActivityID,
"channelId": context.activity.channelId,
"channelData": context.activity.channelData,
"conversation":
{
"name": "",
"id": context.activity.conversation.id,
"isGroup": context.activity.conversation.isGroup,
"conversationType": context.activity.conversation.conversationType,
"tenantId": context.activity.conversation.tenantId
},
"type": "message",
"text": "",
"summary": "",
"attachments": [ attachment ]
} await context.updateActivity(updatedActivity);