Example #1
0
 public async Task Push_ToIsEmpty_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentEmptyExceptionAsync("to", async() =>
     {
         await bot.Push(string.Empty, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
            public async Task ThrowsExceptionWhenResponseIsError()
            {
                TestHttpClient httpClient = TestHttpClient.ThatReturnsAnError();

                ILineBot bot = TestConfiguration.CreateBot(httpClient);

                await ExceptionAssert.ThrowsUnknownError(async() =>
                {
                    await bot.LeaveRoom("test");
                });
            }
        public async Task GetProfile_ErrorResponse_ThrowsException()
        {
            TestHttpClient httpClient = TestHttpClient.ThatReturnsAnError();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);

            await ExceptionAssert.ThrowsUnknownError(async() =>
            {
                await bot.GetProfile("test");
            });
        }
        public async Task Reply_ErrorResponse_ThrowsException()
        {
            TestHttpClient httpClient = TestHttpClient.ThatReturnsAnError();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);

            await ExceptionAssert.ThrowsUnknownError(async() =>
            {
                await bot.Reply("token", new TextMessage("Test"));
            });
        }
 public async Task Reply_TokenIsNull_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("token", async() =>
     {
         await bot.Reply((IReplyToken)null, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
 public async Task Reply_ReplyTokenIsEmpty_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentEmptyExceptionAsync("replyToken", async() =>
     {
         await bot.Reply(string.Empty, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
Example #7
0
 public async Task Push_UserIsNull_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("user", async() =>
     {
         await bot.Push((IUser)null, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
Example #8
0
        public async Task Reply_WithToken_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Reply(new TestMessage(), new TestTextMessage());

            string postedData = @"{""replyToken"":""testReplyToken"",""messages"":[{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/reply", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task Push_WithRoom_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push(new TestRoom(), new TextMessage("FooBar"));

            string postedData = @"{""to"":""testRoom"",""messages"":[{""type"":""text"",""text"":""FooBar""}]}";

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
Example #10
0
        public async Task Multicast_WithUser_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new TestUser[] { new TestUser() }, new TextMessage("FooBar"));

            string postedData = @"{""to"":[""testUser""],""messages"":[{""type"":""text"",""text"":""FooBar""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
Example #11
0
        public async Task Multicast_CorrectInput_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new string[] { "id1", "id2" }, new TextMessage("Test"));

            string postedData = @"{""to"":[""id1"",""id2""],""messages"":[{""type"":""text"",""text"":""Test""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task GetEvents_InvalidRequest_ReplyTokenReturnsNull()
        {
            ILineBot bot = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Invalid);

            IEnumerable<ILineEvent> events = await bot.GetEvents(request);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.IsNull(lineEvent.ReplyToken);
        }
Example #13
0
 public ModuleManager(
     ILineConfiguration configuration,
     ILineBot bot,
     LineBotService service)
 {
     Configuration = configuration;
     Bot           = bot;
     Service       = service;
     if (SuperUsers == null)
     {
         SuperUsers = new List <string>();
     }
 }
        public async Task GetEvents_InvalidRequest_BeaconIsNull()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(InvalidJson);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.IsNull(lineEvent.Beacon);
        }
Example #15
0
            public async Task ReturnsNullWhenResponseIsEmpty()
            {
                TestHttpClient httpClient = TestHttpClient.Create();

                ILineBot bot = TestConfiguration.CreateBot(httpClient);

                byte[] data = await bot.GetMessageContent("test");

                Assert.AreEqual(HttpMethod.Get, httpClient.RequestMethod);
                Assert.AreEqual("/message/test/content", httpClient.RequestPath);

                Assert.IsNull(data);
            }
Example #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lineBot"></param>
        /// <param name="lineEvent"></param>
        /// <returns></returns>
        public async Task Handle(ILineBot lineBot, ILineEvent lineEvent)
        {
            if (lineEvent.Beacon.BeaconType == BeaconType.Unknown)
            {
                return;
            }

            var eventSourceId = EventSource.GetEventSourceId(lineEvent);

            if (string.IsNullOrEmpty(eventSourceId))
            {
                return;
            }
        }
Example #17
0
        public async Task Multicast_WithEnumerable_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            IEnumerable <TextMessage> messages = Enumerable.Repeat(new TextMessage("FooBar"), 2);

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Multicast(new string[] { "id" }, messages);

            string postedData = @"{""to"":[""id""],""messages"":[{""type"":""text"",""text"":""FooBar""},{""type"":""text"",""text"":""FooBar""}]}";

            Assert.AreEqual("/message/multicast", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
Example #18
0
        public async Task Push_WithUserAndEnumerable_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            IEnumerable <TextMessage> messages = Enumerable.Repeat(new TextMessage("FooBar"), 2);

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push(new TestUser(), messages);

            string postedData = @"{""to"":""testUser"",""messages"":[{""type"":""text"",""text"":""FooBar""},{""type"":""text"",""text"":""FooBar""}]}";

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task GetEvents_InvalidMessageType_MessageTypeIsUnknown()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Messages.Invalid);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.IsNotNull(lineEvent.Message);
            Assert.AreEqual(MessageType.Unknown, lineEvent.Message.MessageType);
        }
Example #20
0
        public async Task Push_WithEnumerable_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            IEnumerable <TestTextMessage> messages = Enumerable.Repeat(new TestTextMessage(), 2);

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Reply("token", messages);

            string postedData = @"{""replyToken"":""token"",""messages"":[{""type"":""text"",""text"":""TestTextMessage""},{""type"":""text"",""text"":""TestTextMessage""}]}";

            Assert.AreEqual("/message/reply", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task GetEvents_RequestWithoutBeacon_BeaconIsNull()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(BeaconEventWithoutBeaconJson);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.IsNotNull(events);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.AreEqual(LineEventType.Beacon, lineEvent.EventType);
            Assert.IsNull(lineEvent.Beacon);
        }
Example #22
0
        public async Task Push_CorrectInput_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);
            await bot.Push("id", new TextMessage()
            {
                Text = "Test"
            });

            string postedData = @"{""to"":""id"",""messages"":[{""type"":""text"",""text"":""Test""}]}";

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        public async Task GetEvents_RequestWithoutPostback_PostbackIsNull()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.WithoutPostback);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.IsNotNull(events);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.AreEqual(LineEventType.Postback, lineEvent.EventType);
            Assert.IsNull(lineEvent.Postback);
        }
        public async Task GetProfile_CorrectResponse_ReturnsUserProfile()
        {
            TestHttpClient httpClient = TestHttpClient.Create(JsonDocuments.UserProfile);

            ILineBot     bot     = TestConfiguration.CreateBot(httpClient);
            IUserProfile profile = await bot.GetProfile("test");

            Assert.AreEqual(HttpMethod.Get, httpClient.RequestMethod);
            Assert.AreEqual("/profile/test", httpClient.RequestPath);

            Assert.IsNotNull(profile);
            Assert.AreEqual("LINE taro", profile.DisplayName);
            Assert.AreEqual(new Uri("http://obs.line-apps.com/..."), profile.PictureUrl);
            Assert.AreEqual("Hello, LINE!", profile.StatusMessage);
            Assert.AreEqual("Uxxxxxxxxxxxxxx...", profile.UserId);
        }
        public async Task Handle(ILineBot lineBot, ILineEvent evt)
        {
            if (string.IsNullOrEmpty(evt.Message.Text))
            {
                return;
            }

            // The Webhook URL verification uses these invalid token.
            if (evt.ReplyToken == "00000000000000000000000000000000" || evt.ReplyToken == "ffffffffffffffffffffffffffffffff")
            {
                return;
            }

            if (evt.Message.Text.ToLowerInvariant().Contains("ping"))
            {
                var response = new TextMessage($"pong");

                await lineBot.Reply(evt.ReplyToken, response);
            }
            else if (evt.Message.Text.ToLowerInvariant().Contains("user"))
            {
                var userName = evt.Source.User.Id;
                try
                {
                    var user = await lineBot.GetProfile(evt.Source.User);

                    userName = $"{user.DisplayName} ({user.UserId})";
                }
                catch (LineBotException)
                {
                }

                var response = new TextMessage($"You are: {userName}");

                await lineBot.Reply(evt.ReplyToken, response);
            }
            else if (evt.Message.Text.ToLowerInvariant().Contains("logo"))
            {
                var logoUrl = this.configuration.ResourcesUrl + "/Images/Line.Bot.SDK.png";

                Console.WriteLine(logoUrl);
                var response = new ImageMessage(logoUrl, logoUrl);

                await lineBot.Reply(evt.ReplyToken, response);
            }
        }
Example #26
0
        public async Task Handle(ILineBot lineBot, ILineEvent evt)
        {
            string userName = "******";

            try
            {
                var user = await lineBot.GetProfile(evt.Source.User);

                userName = $"{user.DisplayName} ({user.UserId})";
            }
            catch (LineBotException)
            {
            }

            var response = new TextMessage($"Welcome, {userName} !");

            await lineBot.Reply(evt.ReplyToken, response);
        }
Example #27
0
            public async Task ReturnsDataWhenWithCorrectMessageId()
            {
                byte[] input = new byte[12] {
                    68, 105, 114, 107, 32, 76, 101, 109, 115, 116, 114, 97
                };

                TestHttpClient httpClient = TestHttpClient.ThatReturnsData(input);

                ILineBot bot = TestConfiguration.CreateBot(httpClient);

                byte[] data = await bot.GetMessageContent("test");

                Assert.AreEqual(HttpMethod.Get, httpClient.RequestMethod);
                Assert.AreEqual("/message/test/content", httpClient.RequestPath);

                Assert.IsNotNull(data);
                CollectionAssert.AreEqual(data, input);
            }
Example #28
0
        public async Task GetEvents_ValidRequest_IsLeaveEvent()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(LeaveEventJson);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.IsNotNull(events);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.AreEqual(LineEventType.Leave, lineEvent.EventType);

            IEventSource source = lineEvent.Source;

            Assert.IsNotNull(source);
            Assert.AreEqual(EventSourceType.Group, source.SourceType);
            Assert.AreEqual("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", source.Group.Id);
        }
Example #29
0
        public async Task GetEvents_ValidRequest_IsUnfollowEvent()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Unfollow);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.IsNotNull(events);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.AreEqual(LineEventType.Unfollow, lineEvent.EventType);

            IEventSource source = lineEvent.Source;

            Assert.IsNotNull(source);
            Assert.AreEqual(EventSourceType.User, source.SourceType);
            Assert.AreEqual("U206d25c2ea6bd87c17655609a1c37cb8", source.User.Id);
        }
Example #30
0
        public async Task GetEvents_ValidRequest_ReturnsFollowEvent()
        {
            ILineBot        bot     = TestConfiguration.CreateBot();
            TestHttpRequest request = new TestHttpRequest(JsonDocuments.Events.Join);

            IEnumerable <ILineEvent> events = await bot.GetEvents(request);

            Assert.IsNotNull(events);
            Assert.AreEqual(1, events.Count());

            ILineEvent lineEvent = events.First();

            Assert.AreEqual(LineEventType.Join, lineEvent.EventType);

            IEventSource source = lineEvent.Source;

            Assert.IsNotNull(source);
            Assert.AreEqual(EventSourceType.Group, source.SourceType);
            Assert.AreEqual("cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", source.Group.Id);

            Assert.AreEqual("nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", lineEvent.ReplyToken);
        }