Search code examples
.netazurebotframeworkbotsazure-bot-service

'Operation returned an invalid status code 'badrequest' is returned when the bot is published on Teams channel.: Bot Framework


I am a beginner learning the Bot framework in Azure.NET, I came across this bot sample (https://github.com/tompaana/intermediator-bot-sample) which perfectly demonstrates the human-hand off scenario. I could execute it on local host and it worked as expected.

When I published this POC on MS Teams channel through Azure Bot service, The code returns me the exception 'Operation returned an invalid status code 'badrequest'. This occurs particularly when '@Bot GetRequests' is typed in Teams. (This command is used to retrieve the pending accepted requests during human-hand off scenarios.)

Here is the code snippet which is causing the issue:

  //This is the Entry point method of the Bot application
  protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
  {

    if (turnContext.Activity.Text == "@Bot GetRequests")
    {
     await HandleCommandAsync(turnContext);
    }

 }

 public async virtual Task<bool> HandleCommandAsync(ITurnContext context)
 {
        Activity activity = context.Activity;           
        Command command = Command.FromMessageActivity(activity);          
        bool wasHandled = false;
        Activity replyActivity = null;
        ConversationReference sender = MessageRouter.CreateSenderConversationReference(activity);

        switch (command.BaseCommand)
        {               
            case Commands.GetRequests:

                IList<ConnectionRequest> connectionRequests = _messageRouter.RoutingDataManager.GetConnectionRequests();

                replyActivity = activity.CreateReply();

                replyActivity.Attachments = CreateMultipleConnectionRequestCards(_messageRouter,
                       connectionRequests, activity.Recipient?.Name);

                replyActivity.ChannelData = JsonConvert.SerializeObject(connectionRequests);                                
                wasHandled = true;                  
                break;                             
       }

       if (replyActivity != null)
       {
            //Exception is thrown here when the Bot is published to Teams channel'
            await context.SendActivityAsync(replyActivity);
       }            
        return wasHandled;
   }


   public static IList<Attachment> CreateMultipleConnectionRequestCards(MessageRouter messageRouter,
        IList<ConnectionRequest> connectionRequests, string botName = null)
    {
        IList<Attachment> attachments = new List<Attachment>();

        foreach (ConnectionRequest connectionRequest in connectionRequests)
        {
            attachments.Add(CreateConnectionRequestCard(messageRouter, connectionRequest, botName).ToAttachment());
        }
        return attachments;
  }

I am just curious to know that if anyone in the developer community has ever been through the similar problem. If yes, I would really appreciate if I can get any help on this to come out of the issue.


Solution

  • The Problem is in the below line of HandleCommandAsync(ITurnContext context) method.

    replyActivity.ChannelData = JsonConvert.SerializeObject(connectionRequests);  
    

    Just comment it and it should work.