聊天BoT异常:OnTurnAsync异常-操作返回了无效的状态码“ BadRequest”

时间:2019-06-18 15:14:25

标签: c# azure .net-core botframework chatbot

仅在将我的机器人部署到azure Web应用并尝试在azure门户网络聊天中进行测试之后,我才收到此错误。但是我的机器人正在通过本地计算机上的机器人模拟器按预期工作。

我不确定这是否归因于分配给我的天蓝色角色。我分配的角色显示为“有限贡献者”。我能够创建任何资源并将聊天机器人部署到azure。所以不确定是否与我的“有限贡献者”角色有关。

这是引发异常的代码行(用红色标记): enter image description here

  Exception OnTurnAsync  exception inner ex.Message:
 Operation returned an invalid status code 'BadRequest'  ex:
 Microsoft.Bot.Schema.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.Bot.Connector.Conversations.ReplyToActivityWithHttpMessagesAsync(String conversationId, String activityId, Activity activity, Dictionary`2 customHeaders, CancellationToken cancellationToken)
   at Microsoft.Bot.Connector.ConversationsExtensions.ReplyToActivityAsync(IConversations operations, String conversationId, String activityId, Activity activity, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.BotFrameworkAdapter.SendActivitiesAsync(ITurnContext turnContext, Activity[] activities, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.TurnContext.<>c__DisplayClass22_0.<<SendActivitiesAsync>g__SendActivitiesThroughAdapter|1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Bot.Builder.TurnContext.SendActivityAsync(IActivity activity, CancellationToken cancellationToken)
   at AbcChatBot.Bots.AbcsBot.OnMessageActivityAsync(ITurnContext`1 turnContext, CancellationToken cancellationToken)

任何解决或确定我为什么收到“ BadRequest”的建议,将不胜感激。

2 个答案:

答案 0 :(得分:0)

这通常是因为MicrosoftAppId / MicrosoftAppPassword是在您的App Service配置或代码中配置的(取决于您的配置方式)。请确保已在此处配置它们,然后重试。

我的猜测是,这与您的角色无关。此外,我认为角色“”不是标准设置。我确实看到了Limited Administrator,但 不是 Limited Contributor

https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/directory-assign-admin-roles

答案 1 :(得分:0)

此错误的根本原因是

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
            {
    var reply = new Activity(); //this line caused the error
    ……..

    }

所以我从

更改了代码

var reply = new Activity();

var reply = turnContext.Activity.AsMessageActivity();

错误消失了。

我仍然不知道为什么实际问题出在 OnMessageActivityAsync

中的 onTurnAsync 抛出异常