1

I write .NET Core 3.1 bot for personal conversation in MS Teams. I use waterfall dialogs, which generate adaptive cards and I need to update or delete those cards after submitting. But whenever I try call UpdateActivityAsync or DeleteActivityAsync methods, I've got error:

Operation returned an invalid status code 'Forbidden'. {"error":{"code":"BotNotInConversationRoster","message":"The bot is not part of the conversation roster."}}

But the error is not connected with adaptive cards. I tried this code and still have the same error:

var oldActivity = MessageFactory.Text("Old activity");
var activity_id = stepContext.Context.Activity.Id;
await stepContext.Context.SendActivityAsync(oldActivity);
var newActivity = MessageFactory.Text("New activity");
newActivity.Id = activity_id;
await stepContext.Context.UpdateActivityAsync(newActivity); //error!

or

var oldActivity = MessageFactory.Text("Old activity");
var activity_id = stepContext.Context.Activity.Id;
await stepContext.Context.SendActivityAsync(oldActivity);
await stepContext.Context.DeleteActivityAsync(activity_id);// error!

By the way call to personal info:

var member = await TeamsInfo.GetMemberAsync(stepContext.Context, 
stepContext.Context.Activity.From.Id, cancellationToken);

is successful.

What may be wrong?

nick66
  • 39
  • 6
  • Could you please try the sample code [here](https://github.com/microsoft/BotBuilder-Samples/blob/99c85863c3635bf0a1de4cbfe6b0a2ccee328680/samples/csharp_dotnetcore/57.teams-conversation-bot/Bots/TeamsConversationBot.cs#L208)? I tried your code and it works when i use turncontext instead of stepcontext? Could you please try this without the waterfall dialog? – Gousia Begum May 06 '20 at 09:09
  • @Gousia-MSFT Thanks, I've looked at the code, then tried to install Teams Conversational Bot from examples. Again had the same error. Then after some experiments I've found that the right activity id can be found from response only. And this works from dialogs also. – nick66 May 06 '20 at 13:17
  • Great! Thanks for letting us know. – Gousia Begum May 06 '20 at 13:40
  • @Gousia-MSFT I've corrected my answer. Please look at it. I never have seen an example how to update or delete Teams activities from bots dialogs. I would be highly appreciate if you will find out how to do it correctly. Thanks. – nick66 May 06 '20 at 13:53

1 Answers1

2

My mistake was in a way of taking activity id which has to be updated or deleted. The right activity id can be found from response only. So here is a working code:

var oldActivity = MessageFactory.Text("Old activity");
var response = await turnContext.SendActivityAsync(oldActivity);
var activity_id = response.Id;
var newActivity = MessageFactory.Text("New activity");
newActivity.Id = activity_id;
await turnContext.UpdateActivityAsync(newActivity);

By the way you can do the same from dialogs using stepContext.Context instead of turnContext:

var oldActivity = MessageFactory.Text("Old activity");
await stepContext.Context.SendActivityAsync(oldActivity);
var response = await stepContext.Context.SendActivityAsync(oldActivity);
var newActivity = MessageFactory.Text("New activity");
newActivity.Id = response.Id;
await stepContext.Context.UpdateActivityAsync(newActivity);

or for deleting, but in strange way - enter image description here

So looks it is not right solution for dialogs. But when you use adaptive cards everything is ok. I use waterfall dialogs with prompts to pop up adaptive cards as described here Using Adaptive Cards with Waterfall Dialogs. So to delete a card from previous waterfall step you need this code:

 private async Task<DialogTurnResult> ProcessResultsAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
              {
                dynamic choice = stepContext.Result;
                var activity_id = stepContext.Context.Activity.ReplyToId;
                await stepContext.Context.DeleteActivityAsync(activity_id, cancellationToken);
                return await stepContext.ContinueDialogAsync();
              }  
nick66
  • 39
  • 6