コード例 #1
0
            public async Task ShouldThrowExceptionWhenRichMenuIsInvalid()
            {
                var richMenu = new RichMenu();

                ILineBot bot = TestConfiguration.CreateBot();
                await ExceptionAssert.ThrowsAsync <InvalidOperationException>("The areas cannot be null.", async() => { await bot.CreateRichMenu(richMenu); });
            }
コード例 #2
0
        public void CreateRequest_JsonFormatTest()
        {
            RichMenu richMenu = new RichMenu()
            {
                name       = "test",
                richMenuId = "",
                size       = new Size()
                {
                    height = 100,
                    width  = 100
                },
                chatBarText = "test",
                selected    = false,
                areas       = new List <Area>()
                {
                    new Area()
                    {
                        action = new PostBackAction()
                        {
                            data        = "i=2&c=3",
                            displayText = "display text"
                        },
                        bounds = new Bounds(2, 3, 100, 100)
                    }
                }
            };

            // Verify
            Assert.AreEqual <string>(JsonConvert.SerializeObject(richMenu, Formatting.None, jsonSettings), "{\"richMenuId\":\"\",\"size\":{\"width\":100,\"height\":100},\"selected\":false,\"name\":\"test\",\"chatBarText\":\"test\",\"areas\":[{\"bounds\":{\"x\":2,\"y\":3,\"width\":100,\"height\":100},\"action\":{\"data\":\"i=2&c=3\",\"displayText\":\"display text\",\"type\":\"postback\"}}]}");
        }
コード例 #3
0
            public void ShouldThrowExceptionWhenSizeIsInvalid()
            {
                var richMenu = new RichMenu()
                {
                    Areas = new[]
                    {
                        new RichMenuArea()
                        {
                            Action = new MessageAction()
                            {
                                Label = "Foo", Text = "Bar"
                            },
                            Bounds = new RichMenuBounds(1, 2, 3, 4)
                        }
                    },
                    ChatBarText = "foobar",
                    Name        = "barfoo",
                    Size        = new RichMenuSize()
                };

                ExceptionAssert.Throws <InvalidOperationException>("The height is not set.", () =>
                {
                    richMenu.Validate();
                });
            }
コード例 #4
0
            public async Task ShouldCreateRichMenu()
            {
                var richMenu = new RichMenu()
                {
                    Size = new RichMenuSize(1686),

                    Selected = false,

                    Name = "testName",

                    ChatBarText = "testChatBarTxt",

                    Areas = new[]
                    {
                        new RichMenuArea()
                        {
                            Action = new UriAction {
                                Label = "testLabel", Url = new Uri("http://www.google.com")
                            },
                            Bounds = new RichMenuBounds
                            {
                                Width  = 110,
                                Height = 120,
                                X      = 11,
                                Y      = 12
                            }
                        },
                        new RichMenuArea
                        {
                            Action = new UriAction {
                                Label = "testLabel2", Url = new Uri("http://www.bing.com")
                            },
                            Bounds = new RichMenuBounds
                            {
                                Width  = 210,
                                Height = 220,
                                X      = 21,
                                Y      = 22
                            }
                        }
                    }
                };

                var richMenuIdJson = @"{""richMenuId"": ""richmenu-801b2cd26b2f13587329ed501d279d27""}";
                var httpClient     = TestHttpClient.ThatReturnsData(Encoding.ASCII.GetBytes(richMenuIdJson));

                var bot    = TestConfiguration.CreateBot(httpClient);
                var result = await bot.CreateRichMenu(richMenu);

                Assert.AreEqual("/richmenu", httpClient.RequestPath);

                string postedData =
                    @"{""areas"":[{""action"":{""type"":""uri"",""label"":""testLabel"",""uri"":""http://www.google.com""},""bounds"":{""x"":11,""y"":12,""width"":110,""height"":120}},{""action"":{""type"":""uri"",""label"":""testLabel2"",""uri"":""http://www.bing.com""},""bounds"":{""x"":21,""y"":22,""width"":210,""height"":220}}],""chatBarText"":""testChatBarTxt"",""name"":""testName"",""selected"":false,""size"":{""width"":2500,""height"":1686}}";

                Assert.AreEqual(postedData, httpClient.PostedData);

                Assert.AreEqual(result, "richmenu-801b2cd26b2f13587329ed501d279d27");
            }
コード例 #5
0
            public void ShouldThrowExceptionWhenValueIsMoreThan300Chars()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The name cannot be longer than 300 characters.", () =>
                {
                    richMenu.Name = new string('x', 301);
                });
            }
コード例 #6
0
            public void ShouldNotThrowExceptionWhenValueIsNull()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The name cannot be null or whitespace.", () =>
                {
                    richMenu.Name = null;
                });
            }
コード例 #7
0
            public void ShouldThrowExceptionWhenAreasIsNull()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The areas cannot be null.", () =>
                {
                    RichMenu.Convert(richMenu);
                });
            }
コード例 #8
0
            public void ShouldThrowExceptionWhenValueIsNull()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The areas cannot be null.", () =>
                {
                    richMenu.Areas = null;
                });
            }
コード例 #9
0
            public void ShouldNotThrowExceptionWhenValueIsWhitespace()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The chat bar text cannot be null or whitespace.", () =>
                {
                    richMenu.ChatBarText = " ";
                });
            }
コード例 #10
0
            public void ShouldThrowExceptionWhenValueIsMoreThan14Chars()
            {
                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The chat bar text cannot be longer than 14 characters.", () =>
                {
                    richMenu.ChatBarText = new string('x', 15);
                });
            }
コード例 #11
0
 public ResponseRichMenu(string richMenuId, RichMenu source)
 {
     RichMenuId  = richMenuId;
     Size        = source.Size;
     Selected    = source.Selected;
     Name        = source.Name;
     ChatBarText = source.ChatBarText;
     Areas       = source.Areas;
 }
コード例 #12
0
            public void ShouldConvertCustomIRichMenuBoundsToRichMenuBounds()
            {
                var richMenu = new TestRichMenu();

                var convertedRichMenu = RichMenu.Convert(richMenu);

                Assert.AreNotSame(richMenu, convertedRichMenu);
                Assert.AreNotSame(richMenu.Areas, convertedRichMenu.Areas);
                Assert.AreNotSame(richMenu.Size, convertedRichMenu.Size);
            }
コード例 #13
0
        /// <summary>
        /// Creates a rich menu.
        /// Note: You must upload a rich menu image and link the rich menu to a user for the rich menu to be displayed.You can create up to 10 rich menus for one bot.
        /// The rich menu represented as a rich menu object.
        /// https://developers.line.me/en/docs/messaging-api/reference/#create-rich-menu
        /// </summary>
        /// <param name="richMenu">RichMenu</param>
        /// <returns>RichMenu Id</returns>
        public virtual async Task <string> CreateRichMenuAsync(RichMenu richMenu)
        {
            var json    = JsonConvert.SerializeObject(richMenu, _jsonSerializerSettings);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            var response = await SendAsync(HttpMethod.Post, $"{_uri}/bot/richmenu", content);

            var responseJson = await response.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeAnonymousType(responseJson, new { richMenuId = "" }).richMenuId);
        }
コード例 #14
0
            public void ShouldNotThrowExceptionWhenValueIs300Chars()
            {
                var value = new string('x', 300);

                var richMenu = new RichMenu
                {
                    Name = value
                };

                Assert.AreEqual(value, richMenu.Name);
            }
コード例 #15
0
        /// <summary>
        /// Creates a rich menu.
        /// </summary>
        /// <param name="richMenu"></param>
        /// <returns></returns>
        public async Task <LineClientResult <RichMenuIdResponse> > Create(RichMenu richMenu)
        {
            if (richMenu == null)
            {
                throw new ArgumentNullException(nameof(RichMenu));
            }

            var request = new LinePostRequest <RichMenuIdResponse>(this, $"bot/richmenu");

            return(await request.Execute(richMenu));
        }
コード例 #16
0
            public void ShouldNotThrowExceptionWhenValueIs14Chars()
            {
                var value = new string('x', 14);

                var richMenu = new RichMenu
                {
                    ChatBarText = value
                };

                Assert.AreEqual(value, richMenu.ChatBarText);
            }
コード例 #17
0
        internal static ResponseRichMenu CreateFrom(dynamic dynamicObject)
        {
            var menu = new RichMenu()
            {
                Name        = (string)dynamicObject?.name,
                Size        = new ImagemapSize((int)(dynamicObject?.size?.width ?? 0), (int)(dynamicObject?.size?.height ?? 0)),
                Selected    = (bool)(dynamicObject?.selected ?? false),
                ChatBarText = (string)dynamicObject?.chatBarText
            };

            return(new ResponseRichMenu((string)dynamicObject?.richMenuId, menu));
        }
コード例 #18
0
            public void ShouldNotThrowExceptionWhenValueIsNotNull()
            {
                var size = new RichMenuSize()
                {
                    Height = 1686
                };
                var richMenu = new RichMenu();

                richMenu.Size = size;

                Assert.AreEqual(size, richMenu.Size);
            }
コード例 #19
0
            public void ShouldThrowExceptionWhenChatBarTextIsNull()
            {
                var richMenu = new RichMenu()
                {
                    Areas = new RichMenuArea[1]
                };

                ExceptionAssert.Throws <InvalidOperationException>("The chat bar text cannot be null.", () =>
                {
                    richMenu.Validate();
                });
            }
コード例 #20
0
            public void ShouldThrowExceptionWhenNameIsNull()
            {
                var richMenu = new RichMenu()
                {
                    Areas       = new RichMenuArea[1],
                    ChatBarText = "foobar"
                };

                ExceptionAssert.Throws <InvalidOperationException>("The name cannot be null.", () =>
                {
                    richMenu.Validate();
                });
            }
コード例 #21
0
            public void ShouldThrowExceptionWhenSizeIsNull()
            {
                var richMenu = new RichMenu()
                {
                    Areas       = new IRichMenuArea[1],
                    ChatBarText = "foobar",
                    Name        = "barfoo"
                };

                ExceptionAssert.Throws <InvalidOperationException>("The size cannot be null.", () =>
                {
                    RichMenu.Convert(richMenu);
                });
            }
コード例 #22
0
            public void ShouldThrowExceptionWhenAreasHasNullValue()
            {
                var richMenu = new RichMenu()
                {
                    Areas       = new RichMenuArea[1],
                    ChatBarText = "foobar",
                    Name        = "barfoo",
                    Size        = new RichMenuSize()
                };

                ExceptionAssert.Throws <InvalidOperationException>("The rich menu area should not be null.", () =>
                {
                    richMenu.Validate();
                });
            }
コード例 #23
0
            public void ShouldThrowExceptionWhenAreasIsInvalid()
            {
                var richMenu = new RichMenu()
                {
                    Areas       = new[] { new RichMenuArea() },
                    ChatBarText = "foobar",
                    Name        = "barfoo",
                    Size        = new RichMenuSize()
                };

                ExceptionAssert.Throws <InvalidOperationException>("The action cannot be null.", () =>
                {
                    richMenu.Validate();
                });
            }
コード例 #24
0
            public void ShouldPreserveInstanceWhenValueIsRichMenu()
            {
                var richMenu = new RichMenu
                {
                    ChatBarText = "testChatBarTxt",
                    Name        = "testName",
                    Areas       = new[]
                    {
                        new RichMenuArea
                        {
                            Action = new UriAction {
                                Label = "testLabel", Url = new Uri("http://www.google.com")
                            },
                            Bounds = new RichMenuBounds
                            {
                                Width  = 110,
                                Height = 120,
                                X      = 11,
                                Y      = 12
                            }
                        },
                        new RichMenuArea
                        {
                            Action = new UriAction {
                                Label = "testLabel2", Url = new Uri("http://www.bing.com")
                            },
                            Bounds = new RichMenuBounds
                            {
                                Width  = 210,
                                Height = 220,
                                X      = 21,
                                Y      = 22
                            }
                        }
                    },
                    Size = new RichMenuSize {
                        Height = 1686
                    },
                    Selected = false
                };

                var convertedRichMenu = RichMenu.Convert(richMenu);

                Assert.AreSame(richMenu, convertedRichMenu);
                Assert.AreSame(richMenu.Areas, convertedRichMenu.Areas);
                Assert.AreSame(richMenu.Size, convertedRichMenu.Size);
            }
コード例 #25
0
ファイル: RichMenuSampleApp.cs プロジェクト: oodworld/linebot
        private Image CreateRichMenuImage(RichMenu menu)
        {
            var bitmap = new Bitmap(menu.Size.Width, menu.Size.Height);
            var g      = Graphics.FromImage(bitmap);

            var bkBrush = Brushes.White;

            if (menu.Name == RichMenuA.Name)
            {
                bkBrush = Brushes.Red;
            }
            else if (menu.Name == RichMenuB.Name)
            {
                bkBrush = Brushes.Blue;
            }
            else if (menu.Name == RichMenuC.Name)
            {
                bkBrush = Brushes.Green;
            }
            else if (menu.Name == RichMenuD.Name)
            {
                bkBrush = Brushes.Yellow;
            }

            g.FillRectangle(bkBrush, new Rectangle(0, 0, menu.Size.Width, menu.Size.Height));
            using (var pen = new Pen(Color.DarkGray, 10.0f))
                using (var font = new Font(FontFamily.GenericSansSerif, 80))
                {
                    foreach (var area in menu.Areas)
                    {
                        var action = (PostbackTemplateAction)area.Action;

                        g.DrawRectangle(pen, area.Bounds.X, area.Bounds.Y, area.Bounds.Width, area.Bounds.Height);
                        g.DrawString(action.Label, font, Brushes.Black,
                                     new RectangleF(area.Bounds.X, area.Bounds.Y, area.Bounds.Width, area.Bounds.Height),
                                     new StringFormat()
                        {
                            Alignment     = StringAlignment.Center,
                            LineAlignment = StringAlignment.Center
                        });
                    }
                }

            return(bitmap);
        }
コード例 #26
0
        private async Task CreateMenuAsync(string menuName, System.Drawing.Image image, params ActionArea[] areas)
        {
            var menu = new RichMenu()
            {
                Name        = menuName,
                ChatBarText = CHAT_BAR_TEXT,
                Selected    = true,
                Size        = ImagemapSize.RichMenuShort,
                Areas       = areas
            };
            var menuId = await _client.CreateRichMenuAsync(menu);

            using (var stream = new System.IO.MemoryStream())
            {
                image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                stream.Position = 0;
                await _client.UploadRichMenuPngImageAsync(stream, menuId);
            }
        }
コード例 #27
0
            public void ShouldNotThrowExceptionWhenLengthOfValueIs20()
            {
                var richMenuArea = new RichMenuArea
                {
                    Action = new UriAction {
                        Label = "testLabel2", Url = new Uri("http://www.bing.com")
                    },
                    Bounds = new RichMenuBounds {
                        Height = 200, Width = 200, X = 100, Y = 0
                    }
                };

                var value = Enumerable.Repeat(richMenuArea, 20).ToArray();

                var richMenu = new RichMenu();

                richMenu.Areas = value;

                Assert.AreEqual(value, richMenu.Areas);
            }
コード例 #28
0
            public void ShouldThrowExceptionWhenLengthOfValueMoreThan20()
            {
                var richMenuArea = new RichMenuArea
                {
                    Action = new UriAction {
                        Label = "testLabel2", Url = new Uri("http://www.bing.com")
                    },
                    Bounds = new RichMenuBounds {
                        Height = 200, Width = 200, X = 100, Y = 0
                    }
                };

                var value = Enumerable.Repeat(richMenuArea, 21).ToArray();

                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The maximum number of areas is 20.", () =>
                {
                    richMenu.Areas = value;
                });
            }
コード例 #29
0
            public void ShouldThrowExceptionWhenValueIsEmpty()
            {
                var richMenuArea = new RichMenuArea
                {
                    Action = new UriAction {
                        Label = "testLabel2", Url = new Uri("http://www.bing.com")
                    },
                    Bounds = new RichMenuBounds {
                        Height = 200, Width = 200, X = 100, Y = 0
                    }
                };

                var value = new RichMenuArea[0];

                var richMenu = new RichMenu();

                ExceptionAssert.Throws <InvalidOperationException>("The minimum number of areas is 1.", () =>
                {
                    richMenu.Areas = value;
                });
            }
コード例 #30
0
        /// <summary>
        /// Creates a rich menu.
        /// https://developers.line.me/en/docs/messaging-api/reference/#create-rich-menu
        /// Note: You must upload a rich menu image and link the rich menu to a user for the rich menu to be displayed.You can create up to 10 rich menus for one bot.
        /// </summary>
        /// <param name="richMenu">RichMenu object</param>
        /// <returns>richMenuId</returns>
        public async Task <string> CreateRichMenu(RichMenu richMenu)
        {
            StringContent content = new StringContent(
                JsonConvert.SerializeObject(richMenu, settings),
                Encoding.UTF8, "application/json");
            var result = await client.PostAsync(richMenuEndpoint, content);

            if (result.IsSuccessStatusCode)
            {
                return(JObject.Parse(await result.Content.ReadAsStringAsync())["richMenuId"].ToString());
            }
            else
            {
                throw new Exception(await result.Content.ReadAsStringAsync());
            }
            //200 OK Request successful
            //400 Bad Request Problem with the request
            //401 Unauthorized Valid Channel access token is not specified
            //403 Forbidden Not authorized to use the API.Confirm that your account or plan is authorized to used the API.
            //429 Too Many Requests Exceeded the rate limit for API calls
            //500 Internal Server Error Error on the internal server
        }