Search code examples
c#azure.net-corebotframeworkchatbot

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


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: 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)

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


Solution

  • 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