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);
            }
        }
Пример #2
0
 public async Task Reply_EnumerableMessagesIsNull_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("messages", async() =>
     {
         await bot.Reply("token", (IEnumerable <ISendMessage>)null);
     });
 }
Пример #3
0
 public async Task Reply_NoMessages_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentEmptyExceptionAsync("messages", async() =>
     {
         await bot.Reply("token", new TextMessage[] { });
     });
 }
Пример #4
0
 public async Task Reply_WithTokenAndEnumerableMessagesIsNull_CallsApi()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("messages", async() =>
     {
         await bot.Reply(new TestMessage(), (IEnumerable <ISendMessage>)null);
     });
 }
Пример #5
0
        public async Task Reply_TooManyMessages_ThrowsException()
        {
            ILineBot bot = TestConfiguration.CreateBot();

            await ExceptionAssert.ThrowsAsync <InvalidOperationException>("The maximum number of messages is 5.", async() =>
            {
                await bot.Reply("id", new ISendMessage[6]);
            });
        }
Пример #6
0
        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"));
            });
        }
Пример #7
0
 public async Task Reply_TokenIsNull_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("token", async() =>
     {
         await bot.Reply((IReplyToken)null, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
Пример #8
0
 public async Task Reply_ReplyTokenIsEmpty_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentEmptyExceptionAsync("replyToken", async() =>
     {
         await bot.Reply(string.Empty, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
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);
        }
        public async Task Handle(ILineBot lineBot, ILineEvent lineEvent)
        {
            if (string.IsNullOrEmpty(lineEvent.Message.Text))
            {
                return;
            }

            if (lineEvent.ReplyToken == "00000000000000000000000000000000" || lineEvent.ReplyToken == "ffffffffffffffffffffffffffffffff")
            {
                return;
            }

            var eventSourceId = EventSource.GetEventSourceId(lineEvent);

            if (lineEvent.Message.Text.Replace(" ", "").Equals("GetEventSourceId", StringComparison.InvariantCultureIgnoreCase))
            {
                await lineBot.Reply(lineEvent.ReplyToken, new TextMessage(eventSourceId));
            }
            else
            {
                var splits = lineEvent.Message.Text.Split(' ');
                if (splits.Length > 1)
                {
                    var keyWord = splits[0];
                    var handles = this.handleRepository.FetchBy(eventSourceId, keyWord);
                    foreach (var handle in handles)
                    {
                        var message = splits[1];

                        var encryptValue = this.cryptographyService.Encrypt(handle.PublicKey, JsonSerializer.Serialize(new { date = DateTime.Now, eventSourceId, message }));

                        try
                        {
                            var client = httpClientFactory.CreateClient();

                            var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(handle.Url));
                            requestMessage.Content = new StringContent(JsonSerializer.Serialize(new { encryptValue }), Encoding.UTF8, "application/json");

                            var response = client.SendAsync(requestMessage).GetAwaiter().GetResult();

                            if (response.IsSuccessStatusCode)
                            {
                                using var stream = response.Content.ReadAsStreamAsync().Result;
                                using (var streamReader = new StreamReader(stream))
                                {
                                    var value    = streamReader.ReadToEnd();
                                    var document = JsonSerializer.Deserialize <Document>(value, new JsonSerializerOptions()
                                    {
                                        PropertyNameCaseInsensitive = true
                                    });
                                    await lineBot.Push(document.EventSourceId, new TextMessage(document.Value));
                                }
                            }
                            else
                            {
                                await lineBot.Push(eventSourceId, new TextMessage(
                                                       $"Message:{lineEvent.Message.Text} \n" +
                                                       $"IsSuccessStatusCode:{response.IsSuccessStatusCode} \n" +
                                                       $"StatusCode:{response.StatusCode}"));
                            }
                        }
                        catch (Exception exception)
                        {
                            await lineBot.Push(eventSourceId, new TextMessage(
                                                   $"Message:{lineEvent.Message.Text} \n" +
                                                   $"ErrorMessage:{exception.ToString()} \n"));
                        }
                    }
                }
            }
        }