Example #1
0
 public async Task Push_GroupIsNull_ThrowsException()
 {
     ILineBot bot = TestConfiguration.CreateBot();
     await ExceptionAssert.ThrowsArgumentNullExceptionAsync("group", async() =>
     {
         await bot.Push((IGroup)null, new TextMessage()
         {
             Text = "Test"
         });
     });
 }
Example #2
0
        public async Task Push_ErrorResponse_ThrowsException()
        {
            TestHttpClient httpClient = TestHttpClient.ThatReturnsAnError();

            ILineBot bot = TestConfiguration.CreateBot(httpClient);

            await ExceptionAssert.ThrowsUnknownError(async() =>
            {
                await bot.Push("id", new TextMessage("Test"));
            });
        }
Example #3
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 Push_WithUser_CallsApi()
        {
            TestHttpClient httpClient = TestHttpClient.Create();

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

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

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

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

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

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

            Assert.AreEqual("/message/push", httpClient.RequestPath);
            Assert.AreEqual(postedData, httpClient.PostedData);
        }
        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 <ValidResult <dynamic> > Push([FromBody] JsonElement data)
        {
            var result = new ValidResult <dynamic>();

            var value = this.cryptographyService.Decrypt(data.GetProperty(CryptographyService.PROPERTY_NAME).GetString());

            if (!value.IsValid)
            {
                return(value);
            }

            string eventSourceId = value.Result.eventSourceId;
            string message       = value.Result.message;

            await lineBot.Push(eventSourceId, new TextMessage()
            {
                Text = message
            });

            return(result);
        }
        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"));
                        }
                    }
                }
            }
        }