-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OneBot] Implemented LocationSegment.cs.cs
- Loading branch information
Linwenxuan
authored and
Linwenxuan
committed
Feb 11, 2024
1 parent
facf661
commit c3538ff
Showing
5 changed files
with
162 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Common; | ||
#pragma warning disable CS8618 | ||
|
||
[Serializable] | ||
public class LightApp | ||
{ | ||
[JsonPropertyName("app")] public string App { get; set; } | ||
|
||
[JsonPropertyName("config")] public Config Config { get; set; } | ||
|
||
[JsonPropertyName("desc")] public string Desc { get; set; } | ||
|
||
[JsonPropertyName("from")] public long From { get; set; } | ||
|
||
[JsonPropertyName("meta")] public Meta Meta { get; set; } | ||
|
||
[JsonPropertyName("prompt")] public string Prompt { get; set; } | ||
|
||
[JsonPropertyName("ver")] public string Ver { get; set; } | ||
|
||
[JsonPropertyName("view")] public string View { get; set; } | ||
} | ||
|
||
[Serializable] | ||
public class Config | ||
{ | ||
[JsonPropertyName("autosize")] public bool Autosize { get; set; } | ||
|
||
[JsonPropertyName("ctime")] public long Ctime { get; set; } | ||
|
||
[JsonPropertyName("forward")] public bool Forward { get; set; } | ||
|
||
[JsonPropertyName("token")] public string Token { get; set; } | ||
|
||
[JsonPropertyName("type")] public string Type { get; set; } | ||
} | ||
|
||
[Serializable] | ||
public class Meta | ||
{ | ||
[JsonPropertyName("Location.Search")] public LocationSearch LocationSearch { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Lagrange.OneBot.Core.Entity.Common; | ||
#pragma warning disable CS8618 | ||
|
||
[Serializable] | ||
public class LocationSearch | ||
{ | ||
[JsonPropertyName("address")] public string Address { get; set; } | ||
|
||
[JsonPropertyName("enum_relation_type")] public long EnumRelationType { get; set; } | ||
|
||
[JsonPropertyName("from")] public string From { get; set; } | ||
|
||
[JsonPropertyName("id")] public string Id { get; set; } | ||
|
||
[JsonPropertyName("lat")] public string Lat { get; set; } | ||
|
||
[JsonPropertyName("lng")] public string Lng { get; set; } | ||
|
||
[JsonPropertyName("name")] public string Name { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Lagrange.Core.Message; | ||
using Lagrange.Core.Message.Entity; | ||
using Lagrange.OneBot.Core.Entity.Common; | ||
|
||
namespace Lagrange.OneBot.Core.Message.Entity; | ||
|
||
[Serializable] | ||
public partial class LocationSegment(float latitude, float longitude) | ||
{ | ||
public LocationSegment() : this(0f, 0f) { } | ||
|
||
[JsonPropertyName("lat")] [CQProperty] public float Latitude { get; set; } = latitude; | ||
|
||
[JsonPropertyName("lon")] [CQProperty] public float Longitude { get; set; } = longitude; | ||
|
||
[JsonPropertyName("title")] public string Title { get; set; } = string.Empty; | ||
|
||
[JsonPropertyName("content")] public string Content { get; set; } = string.Empty; | ||
} | ||
|
||
[SegmentSubscriber(typeof(LightAppEntity), "location")] | ||
public partial class LocationSegment : SegmentBase | ||
{ | ||
public override void Build(MessageBuilder builder, SegmentBase segment) | ||
{ | ||
if (segment is not LocationSegment location) return; | ||
|
||
var json = new LightApp | ||
{ | ||
App = "com.tencent.map", | ||
Config = new Config | ||
{ | ||
Autosize = false, | ||
Ctime = DateTimeOffset.UtcNow.Second, | ||
Forward = true, | ||
Token = "626399d3453d0693fe19e12cd3747c56", | ||
Type = "normal" | ||
}, | ||
Desc = "", | ||
From = 1, | ||
Meta = new Meta | ||
{ | ||
LocationSearch = new LocationSearch | ||
{ | ||
EnumRelationType = 1, | ||
From = "plusPanel", | ||
Id = "", | ||
Lat = location.Latitude.ToString("F5"), | ||
Lng = location.Longitude.ToString("F5"), | ||
Name = location.Title, | ||
Address = location.Content | ||
} | ||
}, | ||
Prompt = $"[Location]{location.Content}", | ||
Ver = "1.1.2.21", | ||
View = "LocationShare" | ||
}; | ||
|
||
builder.LightApp(JsonSerializer.Serialize(json)); | ||
} | ||
|
||
public override SegmentBase? FromEntity(MessageChain chain, IMessageEntity entity) | ||
{ | ||
if (entity is not LightAppEntity lightApp) throw new ArgumentException("Invalid entity type."); | ||
|
||
if (JsonSerializer.Deserialize<LightApp>(lightApp.Payload) is { App: "com.tencent.map" } app) | ||
{ | ||
return new LocationSegment | ||
{ | ||
Latitude = float.Parse(app.Meta.LocationSearch.Lat), | ||
Longitude = float.Parse(app.Meta.LocationSearch.Lng), | ||
Content = app.Meta.LocationSearch.Address, | ||
Title = app.Meta.LocationSearch.Name, | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters