我为MS Teams中的个人对话编写.NET Core 3.1机器人。我使用瀑布对话框,该对话框会生成自适应卡片,并且在提交后需要更新或删除这些卡片。但是,每当我尝试调用UpdateActivityAsync或DeleteActivityAsync方法时,都会出现错误:
操作返回了无效的状态代码“禁止”。 {“ error”:{“ code”:“ BotNotInConversationRoster”,“ message”:“该漫游器不属于会话名册。”}}
但是该错误与自适应卡无关。我尝试了这段代码,但仍然有相同的错误:
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!
或
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!
通过电话拨打个人信息:
var member = await TeamsInfo.GetMemberAsync(stepContext.Context,
stepContext.Context.Activity.From.Id, cancellationToken);
成功。
怎么了?
答案 0 :(得分:1)
我的错误是采用必须更新或删除的活动ID的方式。正确的活动ID只能从响应中找到。所以这是一个工作代码:
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);
通过这种方法,您可以使用stepContext.Context而不是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);
所以看起来这不是对话框的正确解决方案。 但是当您使用自适应卡时,一切都很好。我使用瀑布对话框,并按提示弹出自适应卡,如此处Using Adaptive Cards with Waterfall Dialogs所述。 因此,要从上一个瀑布步骤删除一张卡片,您需要以下代码:
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();
}