-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Red Protocol (Chronocat), an alternative QQ notify me…
…thod.
- Loading branch information
Showing
9 changed files
with
195 additions
and
0 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
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
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,61 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EGSFreeGamesNotifier.Models.WebSocketContent { | ||
public class WSPacket { | ||
[JsonPropertyName("type")] | ||
public string Type { get; set; } | ||
[JsonPropertyName("payload")] | ||
public object Payload { get; set; } | ||
} | ||
|
||
#region Connect Classes | ||
public class ConnectPayload { | ||
[JsonPropertyName("token")] | ||
public string Token { get; set; } | ||
} | ||
#endregion | ||
|
||
#region Message Classes | ||
public class MessagePayload { | ||
[JsonPropertyName("peer")] | ||
public Peer Peer { get; set; } | ||
[JsonPropertyName("elements")] | ||
public List<object> Elements { get; set; } | ||
} | ||
|
||
public class Peer { | ||
[JsonPropertyName("chatType")] | ||
public int ChatType { get; set; } | ||
[JsonPropertyName("peerUin")] | ||
public string PeerUin { get; set; } | ||
} | ||
|
||
public class TextElementRoot { | ||
[JsonPropertyName("elementType")] | ||
public int ElementType { get; set; } = 1; | ||
[JsonPropertyName("textElement")] | ||
public TextElement TextElement { get; set; } | ||
} | ||
|
||
public class TextElement { | ||
[JsonPropertyName("content")] | ||
public string Content { get; set; } | ||
} | ||
|
||
//public class ReplyElementRoot { | ||
// [JsonPropertyName("elementType")] | ||
// public int ElementType { get; set; } = 7; | ||
// [JsonPropertyName("replyElement")] | ||
// public ReplyElement ReplyElement { get; set; } | ||
//} | ||
|
||
//public class ReplyElement { | ||
// [JsonPropertyName("replayMsgSeq")] | ||
// public string ReplayMsgSeq { get; set; } | ||
// [JsonPropertyName("sourceMsgIdInRecords")] | ||
// public string SourceMsgIdInRecords { get; set; } | ||
// [JsonPropertyName("senderUid")] | ||
// public string SenderUid { get; set; } | ||
//} | ||
#endregion | ||
} |
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
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,100 @@ | ||
using EGSFreeGamesNotifier.Models.Config; | ||
using EGSFreeGamesNotifier.Models.Record; | ||
using EGSFreeGamesNotifier.Models.WebSocketContent; | ||
using EGSFreeGamesNotifier.Strings; | ||
using Microsoft.Extensions.Logging; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Text.Json; | ||
using Websocket.Client; | ||
|
||
namespace EGSFreeGamesNotifier.Services.Notifier { | ||
internal class QQRed : INotifiable { | ||
|
||
private readonly ILogger<QQRed> _logger; | ||
|
||
#region debug strings | ||
private readonly string debugSendMessage = "Send notifications to QQ Red (Chronocat)"; | ||
private readonly string debugWSReconnection = "Reconnection happened, type: {0}"; | ||
private readonly string debugWSMessageRecieved = "Message received: {0}"; | ||
private readonly string debugWSDisconnected = "Disconnected: {0}"; | ||
#endregion | ||
|
||
public QQRed(ILogger<QQRed> logger) { | ||
_logger = logger; | ||
} | ||
|
||
private WebsocketClient GetWSClient(NotifyConfig config) { | ||
var url = new Uri(new StringBuilder().AppendFormat(NotifyFormatStrings.qqRedUrlFormat, config.RedAddress, config.RedPort).ToString()); | ||
|
||
#region new websocket client | ||
var client = new WebsocketClient(url); | ||
client.ReconnectionHappened.Subscribe(info => _logger.LogDebug(debugWSReconnection, info.Type)); | ||
client.MessageReceived.Subscribe(msg => _logger.LogDebug(debugWSMessageRecieved, msg)); | ||
client.DisconnectionHappened.Subscribe(msg => _logger.LogDebug(debugWSDisconnected, msg)); | ||
#endregion | ||
|
||
return client; | ||
} | ||
|
||
private static WSPacket GetConnectPacket(NotifyConfig config) { | ||
return new WSPacket() { | ||
Type = NotifyFormatStrings.qqRedWSConnectPacketType, | ||
Payload = new ConnectPayload() { | ||
Token = config.RedToken | ||
} | ||
}; | ||
} | ||
|
||
private static List<WSPacket> GetSendPacket(NotifyConfig config, List<NotifyRecord> records) { | ||
return records.Select(record => new WSPacket() { | ||
Type = NotifyFormatStrings.qqRedWSSendPacketType, | ||
Payload = new MessagePayload() { | ||
Peer = new Peer() { | ||
ChatType = 1, | ||
PeerUin = config.ToQQID | ||
}, | ||
Elements = new List<object>() { | ||
new TextElementRoot() { | ||
TextElement = new TextElement() { | ||
Content = record.ToQQMessage() | ||
} | ||
} | ||
} | ||
} | ||
}).ToList(); | ||
} | ||
|
||
public async Task SendMessage(NotifyConfig config, List<NotifyRecord> records) { | ||
try { | ||
_logger.LogDebug(debugSendMessage); | ||
|
||
var packets = GetSendPacket(config, records); | ||
|
||
using var client = GetWSClient(config); | ||
|
||
await client.Start(); | ||
|
||
await client.SendInstant(JsonSerializer.Serialize(GetConnectPacket(config))); | ||
|
||
foreach (var packet in packets) { | ||
await client.SendInstant(JsonSerializer.Serialize(packet)); | ||
await Task.Delay(600); | ||
} | ||
|
||
await client.Stop(WebSocketCloseStatus.NormalClosure, string.Empty); | ||
|
||
_logger.LogDebug($"Done: {debugSendMessage}"); | ||
} catch (Exception) { | ||
_logger.LogDebug($"Error: {debugSendMessage}"); | ||
throw; | ||
} finally { | ||
Dispose(); | ||
} | ||
} | ||
|
||
public void Dispose() { | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
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