Skip to content

Commit

Permalink
mod insight model
Browse files Browse the repository at this point in the history
  • Loading branch information
10hic31 committed Jul 28, 2023
1 parent 60d5975 commit 34fc2a5
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 81 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ but I recommend that you should use a LineMessagingClient class instead of each
|---|---|---|
|[Get number of message deliveries](https://developers.line.biz/en/reference/messaging-api/#get-number-of-delivery-messages)|GetNumberOfMessageDeliveriesAsync|✔|
|[Get number of followers](https://developers.line.biz/en/reference/messaging-api/#get-number-of-followers)|GetNumberOfFollowersAsync|✔|
|[Get friend demographics](https://developers.line.biz/en/reference/messaging-api/#get-demographic)|GetFriendsDemographicsAsync||
|[Get user interaction statistics](https://developers.line.biz/en/reference/messaging-api/#get-message-event)|GetUserInteractionStatisticsAsync||
|[Get statistics per unit](https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit)|GetStatisticsPerUnitAsync||
|[Get friend demographics](https://developers.line.biz/en/reference/messaging-api/#get-demographic)|GetFriendsDemographicsAsync||
|[Get user interaction statistics](https://developers.line.biz/en/reference/messaging-api/#get-message-event)|GetUserInteractionStatisticsAsync||
|[Get statistics per unit](https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit)|GetStatisticsPerUnitAsync||
### 2-1-3. LineRichMenuClient class
* [Rich menu](https://developers.line.biz/en/reference/messaging-api/#rich-menu)
Expand Down
6 changes: 3 additions & 3 deletions README_kor.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Install-Package LineDevelopers
|---|---|---|
|[Get number of message deliveries](https://developers.line.biz/en/reference/messaging-api/#get-number-of-delivery-messages)|GetNumberOfMessageDeliveriesAsync|✔|
|[Get number of followers](https://developers.line.biz/en/reference/messaging-api/#get-number-of-followers)|GetNumberOfFollowersAsync|✔|
|[Get friend demographics](https://developers.line.biz/en/reference/messaging-api/#get-demographic)|GetFriendsDemographicsAsync||
|[Get user interaction statistics](https://developers.line.biz/en/reference/messaging-api/#get-message-event)|GetUserInteractionStatisticsAsync||
|[Get statistics per unit](https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit)|GetStatisticsPerUnitAsync||
|[Get friend demographics](https://developers.line.biz/en/reference/messaging-api/#get-demographic)|GetFriendsDemographicsAsync||
|[Get user interaction statistics](https://developers.line.biz/en/reference/messaging-api/#get-message-event)|GetUserInteractionStatisticsAsync||
|[Get statistics per unit](https://developers.line.biz/en/reference/messaging-api/#get-statistics-per-unit)|GetStatisticsPerUnitAsync||
### 2-1-3. LineRichMenuClient class
* [Rich menu](https://developers.line.biz/en/reference/messaging-api/#rich-menu)
Expand Down
108 changes: 65 additions & 43 deletions Src/LineDevelopers.ConsoleTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,69 +1,91 @@
using System.Text.Json;
using System.Net.Http.Headers;
using System.Text.Json;
using Line;
using Line.Login;
using Line.Message;

var json = File.ReadAllText(@"c:\temp\test.json");
var config = JsonSerializer.Deserialize<TestConfig>(json);

using (var client = new LineMessagingClient("your channelacesstoken"))
{
await client.Message.SendPushMessageAsync("user id", new TextMessage("hello world"));
}


using (var client = new LineMessagingClient("your channelacesstoken"))
{
await client.Message.SendPushMessageAsync("user id", new TextMessage("hello world"),
xLineRetryKey: Guid.NewGuid().ToString());
}

using (var client = new LineMessagingClient("your channelacesstoken"))
using (var client = new LineMessagingClient(config.ChannelAccessToken))
{
await client.Message.SendPushMessageAsync("user id", new TextMessage("hello world"),
xLineRetryKey: Guid.NewGuid().ToString(),
getResponseHeaders: (o) =>
{
IEnumerable<string> xLineRequestId;
IEnumerable<string> xLineAcceptedRequestId;
if (o.TryGetValues("X-Line-Request-Id", out xLineRequestId))
string requestId = String.Empty;

var messages = new List<IMessage>()
{
new TextMessage("first"),
new StickerMessage("6632","11825375")
{
QuickReply = new QuickReply()
{
Console.WriteLine(xLineRequestId.First());
Items = new List<QuickReplyButtonObject>()
{
new QuickReplyButtonObject()
{
Action = new UriAction()
{
Label = "test label",
Uri = "http://lunasoft.co.kr"
}
}
}
}
}
};

if (o.TryGetValues("X-Line-Accepted-Request-Id", out xLineAcceptedRequestId))
await client.Message.SendBroadcastMessageAsync(messages,
getResponseHeaders: async (o) =>
{
Console.WriteLine(xLineAcceptedRequestId.First());
}
});
}
IEnumerable<string> xLineRequestId;
if (o.TryGetValues("X-Line-Request-Id", out xLineRequestId))
{
requestId = xLineRequestId.First();
Console.WriteLine(requestId);
}
});

try
{
await Task.Delay(60000);

using (var test = new LineMessagingClient(config.ChannelAccessToken))
{
await test.Message.SendPushMessageAsync("", new TextMessage("fdasfds"),
getResponseHeaders: (o) =>
{
IEnumerable<string> xLineRequestId;
IEnumerable<string> xLineAcceptedRequestId;
var result = await client.Insight.GetUserInteractionStatisticsAsync(requestId);

if (o.TryGetValues("X-Line-Request-Id", out xLineRequestId))
{
Console.WriteLine(xLineRequestId.First());
}
Console.WriteLine(result.Overview.RequestId);

if (o.TryGetValues("X-Line-Accepted-Request-Id", out xLineAcceptedRequestId))
{
Console.WriteLine(xLineAcceptedRequestId.First());
}
});
}
catch (LineException ex)
{
Console.WriteLine(ex.Message);
}
}



//using (var test = new LineMessagingClient(config.ChannelAccessToken))
//{
// await test.Message.SendPushMessageAsync("", new TextMessage("fdasfds"),
// getResponseHeaders: (o) =>
// {
// IEnumerable<string> xLineRequestId;
// IEnumerable<string> xLineAcceptedRequestId;

// if (o.TryGetValues("X-Line-Request-Id", out xLineRequestId))
// {
// Console.WriteLine(xLineRequestId.First());
// }

// if (o.TryGetValues("X-Line-Accepted-Request-Id", out xLineAcceptedRequestId))
// {
// Console.WriteLine(xLineAcceptedRequestId.First());
// }
// });
//}




//using (LineLoginClient client = new LineLoginClient())
//{
Expand Down
28 changes: 27 additions & 1 deletion Src/LineDevelopers.Tests/LineInsightClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,39 @@ public async Task GetNumberOfMessageDeliveriesAsync()
}

[Test]
public void GetUserInteractionStatisticsAsyncTest()
public void GetUserInteractionStatisticsAsyncErrorTest()
{
var ex = ThrowsAsync<LineException>(() => _client.Insight.GetUserInteractionStatisticsAsync("NOT EXISTS REQ ID"));

That("Not Found", Is.EqualTo(ex.Message));
}

[Test]
public void GetUserInteractionStatisticsAsyncTest()
{
DoesNotThrowAsync(async () =>
{
string requestId = String.Empty;
await _client.Message.SendBroadcastMessageAsync(new TextMessage("static test"),
getResponseHeaders: async (o) =>
{
IEnumerable<string> xLineRequestId;
if (o.TryGetValues("X-Line-Request-Id", out xLineRequestId))
{
requestId = xLineRequestId.First();
}
});
await Task.Delay(1000);
var result = await _client.Insight.GetUserInteractionStatisticsAsync(requestId);
That(result.Overview.RequestId, Is.EqualTo(requestId));
});
}

[Test]
public void GetStatisticsPerUnitAsyncTest()
{
Expand Down
13 changes: 1 addition & 12 deletions Src/LineDevelopers.Webhook.Tests/Controllers/LineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,14 @@ protected override async Task OnMemberLeaveEventAsync(MemberLeaveEventObject mem

protected override async Task OnMessageEventAsync(MessageEventObject messageEventObject)
{
string key = "";

await SomeOtherMethod("ddd", "ddd");


await SomeOtherMethod("ddd", "ddd", (o) => {
key = o;
});

var aaa = key;


IMessage message;

switch (messageEventObject.Message)
{
case TextObject:
var text = (TextObject)messageEventObject.Message;
message = new TextMessage(text.Text);

break;
case StickerObject:
var sticker = (StickerObject)messageEventObject.Message;
Expand Down
8 changes: 6 additions & 2 deletions Src/LineDevelopers/LineDevelopers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.1.10</AssemblyVersion>
<AssemblyVersion>1.1.20</AssemblyVersion>
<PackageProjectUrl>https://github.com/charles96/LineDevelopers.NET</PackageProjectUrl>
<Title>LineDevelopers.NET</Title>
<Authors>Charles Hong</Authors>
Expand All @@ -13,7 +13,7 @@
<PackageTags>LINE;LINE Messaging;LINE Developers;Chatbot;LIFF</PackageTags>
<RepositoryType>git</RepositoryType>
<RootNamespace>Line</RootNamespace>
<Version>1.1.10</Version>
<Version>1.1.20</Version>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand All @@ -29,4 +29,8 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Message\Content\" />
</ItemGroup>

</Project>
26 changes: 22 additions & 4 deletions Src/LineDevelopers/LineHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public LineHttpClient(double timeout = 100d)
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://api.line.me/");
_httpClient.DefaultRequestVersion = new Version(2, 0);
_httpClient.Timeout = TimeSpan.FromSeconds(timeout);

_jsonSerializerOptions = new JsonSerializerOptions();
Expand Down Expand Up @@ -70,6 +71,7 @@ protected async Task<TResult> GetAsync<TResult>(string endpoint, HttpContent htt

using (var request = new HttpRequestMessage(HttpMethod.Get, endpoint))
{
request.Version = new Version(2, 0);
request.Content = httpContent;

using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false))
Expand All @@ -87,11 +89,13 @@ protected async Task<TResult> GetAsync<TResult>(string endpoint, string? headerN
{
TResult result;

using (var body = new HttpRequestMessage(HttpMethod.Get, endpoint))
using (var request = new HttpRequestMessage(HttpMethod.Get, endpoint))
{
if (!String.IsNullOrWhiteSpace(headerName)) body.Headers.Add(headerName, headerValue);
request.Version = new Version(2, 0);

using (var response = await _httpClient.SendAsync(body).ConfigureAwait(false))
if (!String.IsNullOrWhiteSpace(headerName)) request.Headers.Add(headerName, headerValue);

using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false))
{
GetHeaders(response, getResponseHeaders);
await this.EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);
Expand All @@ -111,12 +115,24 @@ protected async Task<Stream> GetStreamAsync(string endpoint, Action<HttpResponse
return await response.Content?.ReadAsStreamAsync();
}

protected async Task<Stream> GetStreamAsync(string endpoint, Action<HttpContentHeaders,HttpResponseHeaders>? getResponseHeaders = null)
{
var response = await _httpClient.GetAsync(endpoint).ConfigureAwait(false);

if (getResponseHeaders != null) getResponseHeaders(response.Content.Headers, response.Headers);

await this.EnsureSuccessStatusCodeAsync(response).ConfigureAwait(false);

return await response.Content?.ReadAsStreamAsync();
}

protected async Task PostAsync(string endpoint, StreamContent streamContent, MediaType mediaType, Action<HttpResponseHeaders>? getResponseHeaders = null)
{
using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
{
streamContent.Headers.Add("Content-Type", mediaType == MediaType.Jpg ? "image/jpeg" : "image/png");


request.Version = new Version(2, 0);
request.Content = streamContent;

using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false))
Expand Down Expand Up @@ -177,7 +193,9 @@ protected async Task PostAsJsonAsync<TRequest>(string endpoint, TRequest request
using (var body = new HttpRequestMessage(HttpMethod.Post, endpoint))
{
var json = JsonSerializer.Serialize<TRequest>(request, _jsonSerializerOptions);

body.Content = new StringContent(json, Encoding.UTF8, "application/json");
body.Version = new Version(2, 0);

if (!String.IsNullOrWhiteSpace(headerName)) body.Headers.Add(headerName, headerValue);

Expand Down
39 changes: 32 additions & 7 deletions Src/LineDevelopers/Message/Insights/StatisticsMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,49 @@ namespace Line.Message
public class StatisticsMessage
{
[JsonPropertyName("seq")]
public int seq { get; set; }
public int Seq { get; set; }

[JsonPropertyName("impression")]
public int impression { get; set; }
public int Impression { get; set; }

[JsonPropertyName("mediaPlayed")]
public int mediaPlayed { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MediaPlayed { get; set; }

[JsonPropertyName("mediaPlayed25Percent")]
public int mediaPlayed25Percent { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MediaPlayed25Percent { get; set; }

[JsonPropertyName("mediaPlayed50Percent")]
public int mediaPlayed50Percent { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MediaPlayed50Percent { get; set; }

[JsonPropertyName("mediaPlayed75Percent")]
public int mediaPlayed75Percent { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MediaPlayed75Percent { get; set; }

[JsonPropertyName("mediaPlayed100Percent")]
public int mediaPlayed100Percent { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? MediaPlayed100Percent { get; set; }

[JsonPropertyName("uniqueMediaPlayed")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? UniqueMediaPlayed { get; set; }

[JsonPropertyName("uniqueMediaPlayed25Percent")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? UniqueMediaPlayed25Percent { get; set; }

[JsonPropertyName("uniqueMediaPlayed50Percent")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? UniqueMediaPlayed50Percent { get; set; }

[JsonPropertyName("uniqueMediaPlayed75Percent")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? UniqueMediaPlayed75Percent { get; set; }

[JsonPropertyName("uniqueMediaPlayed100Percent")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? UniqueMediaPlayed100Percent { get; set; }
}
}
Loading

0 comments on commit 34fc2a5

Please sign in to comment.