microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.51k stars 2.45k forks source link

Facebook channel give an error: Failed with message: Operation returned an invalid status code 'BadRequest' #4537

Closed TranNgocKhoa closed 6 years ago

TranNgocKhoa commented 6 years ago

Bot Info

Issue Description

I'm trying to get and display data from web Api by Herocard. This work properly on Emulator, Web Chat but on Facebook Messenger, this give me an Error: Failed with message: Operation returned an invalid status code 'BadRequest'

Code Example

This return an IMessageActivity HostValueUtils.GETALLROOM is "https://sohot-webapp.azurewebsites.net/api/room/getallparents"

public IMessageActivity GetRooms(IMessageActivity message)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Headers.Add(header: HttpRequestHeader.ContentType, value: "application/json; charset=utf-8");
                var json = (wc.DownloadString(HostValueUtils.GETALLROOM));

                List<Room> postList = (List<Room>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Room>));
                ////postList = postList.GetRange(0, 9);
                message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                foreach (Room p in postList)
                {
                    List<CardImage> imgList = new List<CardImage>
                    {
                        new CardImage(HostValueUtils.DOMAIN + ":" + HostValueUtils.PORTSSL + p.Image)
                    };
                    var heroCard = new HeroCard
                    {
                        Title = p.Name,
                        Text = p.Description,
                        Images = imgList,
                        Buttons = new List<CardAction> { new CardAction(ActionTypes.PostBack, "Đặt phòng này", value: p) }
                    };
                    message.Attachments.Add(heroCard.ToAttachment());
                }
                return message;
            }
        }

I get IMessageActivity here:

[LuisIntent("AvailableRoom")]
        public async Task AvailableRoom(IDialogContext context, LuisResult result)
        {

            IMessageActivity messageRooms;
            string message = $"Dưới đây là thông tin các phòng còn trống:";
            await context.PostAsync(message);
            using (RoomService roomService = new RoomService())
            {

                messageRooms = roomService.GetRooms(context.MakeMessage());
                await context.PostAsync(messageRooms);
                context.Wait<Activity>(this.BookRoomClick);

            }
        }

Room class:

[Serializable]
    public class Room
    {
        public object RoomType { get; set; }
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Image { get; set; }
        public string MoreImages { get; set; }
        public decimal Price { get; set; }
        public object Tags { get; set; }
        public bool? HotFlag { get; set; }
        public int RoomTypeID { get; set; }
        public decimal PromotionPrice { get; set; }
        public object ViewCount { get; set; }
        public DateTime CreatedDate { get; set; }
        public object CreatedBy { get; set; }
        public DateTime? UpdatedDate { get; set; }
        public object UpdatedBy { get; set; }
        public object MetaKeyword { get; set; }
        public object MetaDescription { get; set; }
        public bool Status { get; set; }
    }

Reproduction Steps

  1. Publish code to Azure
  2. Test in Web Chat
  3. Work properly
  4. Test on Messenger
  5. Fails

Expected Behavior

Work properly like Emulator and Web Chat.

Actual Results

Failed with message: Operation returned an invalid status code 'BadRequest'

JasonSowers commented 6 years ago

I ran the following code with no problems:

image

        public IMessageActivity GetRooms(IMessageActivity message)
        {
            using (WebClient wc = new WebClient())
            {
//                wc.Headers.Add(header: HttpRequestHeader.ContentType, value: "application/json; charset=utf-8");
//                var json = (wc.DownloadString(HostValueUtils.GETALLROOM));
                //List<Room> postList = (List<Room>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<Room>));
                ////postList = postList.GetRange(0, 9);
                message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
//                foreach (Room p in postList)
//                {
                for (int i = 0; i < 5; i++)
                {
                    List<CardImage> imgList = new List<CardImage>
                    {
                        new CardImage(@"https://i.imgur.com/vAHUOP5.png")
                    };
                    var heroCard = new HeroCard
                    {
                        Title = "nachos" ,
                        Text = "nachos",
                        Images = imgList,
                        Buttons = new List<CardAction> { new CardAction(ActionTypes.PostBack, "stuff", value: "things") }
                    };
                    message.Attachments.Add(heroCard.ToAttachment());
                }

//                }
                return message;
            }
        }

when calling that method like this:

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
           var activity = await result as Activity;
            var reply = activity.CreateReply();
            GetRooms(reply);
            await context.PostAsync(reply);
            context.Wait(MessageReceivedAsync);
        }

The issue seems to be that facebook does like something in the code I had to comment out because I did not have everything to make it resolve. This is really as far as i can look into it with the code given, hope it helps.

TranNgocKhoa commented 6 years ago

Actually, I have a simillar code bellow, and it works on Facebook Messenger:

 public IMessageActivity GetRoomTypes(IMessageActivity message)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Headers.Add(header: HttpRequestHeader.ContentType, value: "application/json; charset=utf-8");
                var json = (wc.DownloadString(HostValueUtils.GETALLROOMTYPE));

                List<RoomType> items = (List<RoomType>)Newtonsoft.Json.JsonConvert.DeserializeObject(json, typeof(List<RoomType>));

                message.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                foreach (RoomType p in items)
                {
                    var heroCard = new HeroCard
                    {
                        Title = p.Name,
                        Subtitle = p.Description,
                    };

                    message.Attachments.Add(heroCard.ToAttachment());
                }
                return message;
            }
        }

And I call the method above here

 [LuisIntent("RoomType")]
        public async Task RoomTypeInfo(IDialogContext context, LuisResult result)
        {
            using (RoomTypeService roomTypeService = new RoomTypeService())
            {
               //I call here
                var message = roomTypeService.GetRoomTypes(context.MakeMessage());
                await context.PostAsync(message);
                context.Wait(this.MessageReceived);
            }
        }

You can download my code here https://github.com/15110064/StupidBotMessengerMultiDialogs

JasonSowers commented 6 years ago

@TranNgocKhoa Is this resolved for you, or did you still need help looking into it?

EricDahlvang commented 6 years ago

Closing due to inactivity. Please open a new issue if you require further assistance.