1
votes

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.

2
When you say it works as expected when executing on localhost, do you mean you tested it in the Emulator or you connected your local bot to Teams using a tunneling service? - Kyle Delaney
Hi @KyleDelaney, I have tested it in Emulator. - praneeth
See if you can reproduce the Teams error while debugging the bot locally with ngrok. If you can, show us which line is throwing the exception. - Kyle Delaney

2 Answers

1
votes

I have the same error on teams, below is my code, on the emulator it shows nothing but on teams, it gives that operation returned an invalid bad request,

enter image description here

if (reasonintent == "Schedule")
                    {

                        //******************
                        UAT_LUIS_Entity UAT_LUIS = await GetEntityFromLUIS(turnContext.Activity.Text);
                        List<string> entityList = new List<string>();
                        String HourFromTime = "", HourToTime = "", MinToTime = "", MinFromTime = "", checkspecificdate = "";
                        String FromTime = "", ToTime = "", FromTimeHour = "", FromTimeMin = "", ToTimeHour = "", ToTimeMin = "";
                        for (int i = 0; i < UAT_LUIS.entities.Length; i++)
                        {
                            entityList.Add(UAT_LUIS.entities[i].type.ToString().ToLower());
                            if (UAT_LUIS.entities[i].type.ToString().ToLower() == "room")
                            {
                                roomluisname = UAT_LUIS.entities[i].entity.ToString().ToLower();
                            }

                            if (UAT_LUIS.entities[i].type.ToString().ToLower() == "date")
                            {
                                dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
                                checkspecificdate = "true"
                ;
                            }

                            if (UAT_LUIS.entities[i].type.ToString().ToLower() == "today")
                            {
                                dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
                            }

                            if (UAT_LUIS.entities[i].type.ToString().ToLower() == "the day after tomorrow")
                            {
                                dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
                            }
                            if (UAT_LUIS.entities[i].type.ToString().ToLower() == "tomorrow")
                            {
                                dateluis = UAT_LUIS.entities[i].entity.ToString().ToLower();
                            }




                        }

                        String d = dateluis;


                        if (checkspecificdate == "true")
                        {
                            datevar = Convert.ToDateTime(dateluis);
                        }
                        else if (dateluis.Contains("today"))
                        {
                            datevar = DateTime.Today;
                        }
                        else if (dateluis.Contains( "tomorrow"))
                        {
                            datevar = DateTime.Today.AddDays(1);
                        }
                        else if (dateluis == "the day after tomorrow" || dateluis.Contains("day after tomorrow"))
                        {
                            datevar = DateTime.Today.AddDays(2);
                        }
                        else
                        {
                            datevar = DateTime.Today;
                        }


                        DateTime dateobject;
                        String datestring = "";
                        if (DateTime.TryParse(datevar.ToString(), out dateobject))
                        {

                            datestring = dateobject.ToString("yyyy-MM-ddT");

                        }




                        string datevarparticularstart = datestring + "00" + ":" + "00" + ":" + "01-08:00";
                        string datevarparticularend = datestring + "23" + ":" + "59" + ":" + "00-08:00";
                        var queryparticulardate = new List<QueryOption>()
                        {
                            new QueryOption("startDateTime", datevarparticularstart),
                            new QueryOption("endDateTime", datevarparticularend)
                        };

                        var calendarViewparticulardate = await graphClient.Me.CalendarView
                            .Request(queryparticulardate)
                            .GetAsync();
                        String luiscalenderentity = "";


                        //***********************





                        for (int i = 0; i < UAT_LUIS.entities.Length; i++)
                        {
                            entityList.Add(UAT_LUIS.entities[i].type.ToString().ToLower());
                            if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("tulip"))
                            {
                                luiscalenderentity = "tulip";

                            }

                            else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("lotus"))
                            {
                                luiscalenderentity = "lotus";

                            }

                            else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("snowdrop"))
                            {
                                luiscalenderentity = "snowdrop";

                            }
                            else if (UAT_LUIS.entities[i].entity.ToString().ToLower().Contains("chanyaky"))
                            {
                                luiscalenderentity = "chanyaky";

                            }
                        }

                        string boolvarcheck = "";

                        if (calendarViewparticulardate.Count != 0)
                        {


                            for (int i = 0; i < calendarViewparticulardate.Count; i++)
                            {


                                if (calendarViewparticulardate[i].Location.DisplayName.ToLower().Contains(luiscalenderentity))
                                {
                                    boolvarcheck = "true";

                                    string converttostring1 = "";
                                    DateTime date1;
                                    if (DateTime.TryParse(Convert.ToDateTime(calendarViewparticulardate[i].Start.DateTime).AddHours(-8).ToString(), out date1))
                                    {

                                        converttostring1 = date1.ToString("HH:mm:ss");
                                    }

                                    String vartime1 = converttostring1;

                                    // Convert.ToDateTime(calendarViewdate[0].End.DateTime).AddHours(-8);
                                    string converttostring2 = "";
                                    DateTime date2;
                                    if (DateTime.TryParse(Convert.ToDateTime(calendarViewparticulardate[i].End.DateTime).AddHours(-8).ToString(), out date2))
                                    {

                                        converttostring2 = date2.ToString("HH:mm:ss");
                                    }

                                    String vartime2 = converttostring2;


                                    EventNameschedule.Add(new AdaptiveTextBlock()
                                    {
                                        Text = calendarViewparticulardate[i].Location.DisplayName,
                                        Size = AdaptiveTextSize.Small,
                                        Color = AdaptiveTextColor.Accent
                                    });
                                    Eventschedulestart.Add(new AdaptiveTextBlock()
                                    {
                                        Text = vartime1,
                                        Size = AdaptiveTextSize.Small,
                                        Color = AdaptiveTextColor.Accent
                                    });
                                    Eventscheduleend.Add(new AdaptiveTextBlock()
                                    {
                                        Text = vartime2,
                                        Size = AdaptiveTextSize.Small,
                                        Color = AdaptiveTextColor.Accent
                                    });
                                    Eventschedulesubject.Add(new AdaptiveTextBlock()
                                    {
                                        Text = calendarViewparticulardate[i].Subject,
                                        Size = AdaptiveTextSize.Small,
                                        Color = AdaptiveTextColor.Accent
                                    });
                                }
                            }





                            if (boolvarcheck == "true")
                            {
                                card.Body.Add(new AdaptiveContainer()
                                {


                                    Items = new List<AdaptiveElement>()
                                                {




                                                      new AdaptiveColumnSet()
                                                        {
                                                            Type = "ColumnSet",
                                                            Height = AdaptiveHeight.Auto,
                                                            Columns=new List<AdaptiveColumn> ()
                                                            {
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="200px",

                                                                  Items=new List<AdaptiveElement>()
                                                                  {

                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text=luiscalenderentity,//+" Conference Hall",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color=AdaptiveTextColor.Good
                                                                      }
                                                                  }
                                                              }  ,
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="150px",
                                                                  Items=new List<AdaptiveElement>()
                                                                  {
                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text=datevar.ToString("dd-MM-yyyy"),//DateTime.Today.ToString("dd-MM-yyyy"),
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color = AdaptiveTextColor.Good

                                                                      }
                                                                  }
                                                              },

                                                             }
                                                      },

                                                      new AdaptiveColumnSet()
                                                        {
                                                            Type = "ColumnSet",
                                                            Height = AdaptiveHeight.Auto,
                                                            Columns=new List<AdaptiveColumn> ()
                                                            {
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="200px",

                                                                  Items=new List<AdaptiveElement>()
                                                                  {

                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text="Name",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color=AdaptiveTextColor.Good
                                                                      }
                                                                  }
                                                              }  ,
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="80px",
                                                                  Items=new List<AdaptiveElement>()
                                                                  {
                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text="Start Time",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color = AdaptiveTextColor.Good

                                                                      }
                                                                  }
                                                              },
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="80px",
                                                                  Items=new List<AdaptiveElement>()
                                                                  {
                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text="End Time",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color = AdaptiveTextColor.Good

                                                                      }
                                                                  }
                                                              },

                                                             }
                                                      },




                                                    new AdaptiveColumnSet()
                                                    {
                                                        Type = "ColumnSet",
                                                        Height = AdaptiveHeight.Auto,

                                                        Columns=new List<AdaptiveColumn> ()
                                                        {


                                                          new AdaptiveColumn()
                                                          {
                                                              Type="Column",
                                                              Width="200px",
                                                              Items=Eventschedulesubject,


                                                          },

                                                           new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="80px",
                                                                 Items= Eventschedulestart,
                                                              },

                                                             new AdaptiveColumn()
                                                          {
                                                              Type="Column",
                                                              Width="80px",
                                                             Items= Eventscheduleend,
                                                          }



                                                        }


                                                    },
                                                }
                                });
                            }

                            else
                            {
                                card.Body.Add(new AdaptiveContainer()
                                {


                                    Items = new List<AdaptiveElement>()
                                                {




                                                      new AdaptiveColumnSet()
                                                        {
                                                            Type = "ColumnSet",
                                                            Height = AdaptiveHeight.Auto,
                                                            Columns=new List<AdaptiveColumn> ()
                                                            {
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="500px",

                                                                  Items=new List<AdaptiveElement>()
                                                                  {

                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text=" No scheduling for "+luiscalenderentity+" hall",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color=AdaptiveTextColor.Good
                                                                      }
                                                                  }
                                                              }


                                                             }
                                                      },








                                                }
                                });
                            }


                        }

                        else
                        {
                            card.Body.Add(new AdaptiveContainer()
                            {


                                Items = new List<AdaptiveElement>()
                                                {




                                                      new AdaptiveColumnSet()
                                                        {
                                                            Type = "ColumnSet",
                                                            Height = AdaptiveHeight.Auto,
                                                            Columns=new List<AdaptiveColumn> ()
                                                            {
                                                              new AdaptiveColumn()
                                                              {
                                                                  Type="Column",
                                                                  Width="500px",

                                                                  Items=new List<AdaptiveElement>()
                                                                  {

                                                                      new AdaptiveTextBlock()
                                                                      {
                                                                          Type="TextBlock",
                                                                          Text=" No scheduling for "+luiscalenderentity+" hall",
                                                                           Weight=AdaptiveTextWeight.Bolder,
                                                                           Color=AdaptiveTextColor.Good
                                                                      }
                                                                  }
                                                              }


                                                             }
                                                      },








                                                }
                            });
                        }


                        var attachment1 = new Microsoft.Bot.Schema.Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content = card,
                        };
                        var reply1 = MessageFactory.Attachment(attachment1);
                        await turnContext.SendActivityAsync(reply1, cancellationToken);



                    }
0
votes

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

replyActivity.ChannelData = JsonConvert.SerializeObject(connectionRequests);  

Just comment it and it should work.