Chat BoT Exception: OnTurnAsync exception— Operation returned an invalid status code 'BadRequest'

倖福魔咒の 提交于 2020-01-16 16:29:10

问题


I am getting this error only after I deployed my bot to azure web app and tried to Test in azure portal Web Chat. But my bot is working as expected from bot emulator on my local machine.

I am not sure if this is due to the azure role assigned to me.My assigned role is showing as "Limited Contributor". I am able to create any resource and deploy my chat bot to azure.So not sure if this has anything to do with my "Limited Contributor" role.

Here is the line of code(marked in red) where it is throwing exception:

  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)

Any suggestions to resolve or determining why I am getting a 'BadRequest' would be appreciated.


回答1:


This is typically because the MicrosoftAppId/MicrosoftAppPassword configured in your App Service configuration, or within your code (depending on how you have it configured). Please make sure they are configured there and then try again.

My guess is that it does not have to do with your role. Additionally, I don't see the role '' as standard. I do see a Limited Administrator but not a Limited Contributor:

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




回答2:


The root cause of this error was

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

    }

So I changed my code from

var reply = new Activity();

to

var reply = turnContext.Activity.AsMessageActivity();

and the error is gone.

I still don't know why the exception was throwing from onTurnAsync while the actual issue was in OnMessageActivityAsync



来源:https://stackoverflow.com/questions/56652242/chat-bot-exception-onturnasync-exception-operation-returned-an-invalid-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!