diff --git a/OshimaBot.Tests/EventPipelineTests.cs b/OshimaBot.Tests/EventPipelineTests.cs new file mode 100644 index 0000000..4981653 --- /dev/null +++ b/OshimaBot.Tests/EventPipelineTests.cs @@ -0,0 +1,321 @@ +using System.Text.Json; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging.Abstractions; +using OshimaBot.Data; +using OshimaBot.Messaging; +using OshimaBot.Models; +using OshimaBot.Services; +using Xunit; + +namespace OshimaBot.Tests; + +public sealed class EventPipelineTests +{ + private const string C2CJson = """ + {"id":"EVT1","op":0,"s":7,"t":"C2C_MESSAGE_CREATE", + "d":{"id":"MSG1","author":{"user_openid":"U1","member_openid":""}, + "content":"查角色 1","timestamp":"2026-11-09 12:00:00"}} + """; + + private const string InteractionJson = """ + {"id":"INTERACTION_CREATE:int-1","op":0,"s":8,"t":"INTERACTION_CREATE", + "d":{"id":"int-1","chat_type":0,"timestamp":"2026-11-09 12:00:01","user_openid":"U1", + "data":{"resolved":{"button_id":"b1","button_data":"/签到","message_id":"M1"}}}} + """; + + private static Payload Parse(string json) => JsonSerializer.Deserialize(json)!; + + private static QQEventDispatcher CreateDispatcher( + FakeDedup dedup, FakeAck ack, FakeSink sink) => new( + dedup, ack, sink, NullLogger.Instance); + + [Fact] + public async Task DedupStore_SecondMarkIsRejected() + { + using SqliteConnection connection = new("Data Source=:memory:"); + await connection.OpenAsync(); + ServiceCollection services = new(); + services.AddLogging(); + services.AddDbContext(options => options.UseSqlite(connection)); + services.AddSingleton(); + await using ServiceProvider provider = services.BuildServiceProvider(); + using (IServiceScope scope = provider.CreateScope()) + { + await scope.ServiceProvider.GetRequiredService().Database.EnsureCreatedAsync(); + } + + IEventDeduplicationStore store = provider.GetRequiredService(); + Assert.True(await store.TryMarkAsync("EVT1")); + Assert.False(await store.TryMarkAsync("EVT1")); + Assert.True(await store.TryMarkAsync("EVT2")); + + await store.MarkProcessedAsync("EVT1"); + Assert.Equal(2, await store.PurgeAsync(TimeSpan.Zero)); + Assert.True(await store.TryMarkAsync("EVT1")); + } + + [Fact] + public async Task Dispatcher_AcceptsC2CEventWithMsgId() + { + FakeDedup dedup = new(); + FakeAck ack = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, ack, sink); + + EventDispatchResult result = await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.WebSocket); + + Assert.Equal(EventDispatchStatus.Accepted, result.Status); + IBotMessage message = Assert.Single(sink.Messages); + Assert.IsType(message); + Assert.Equal("MSG1", message.Id); + Assert.Equal("U1", message.OpenId); + Assert.Null(message.EventId); + Assert.Empty(ack.InteractionIds); + Assert.Equal(1, dedup.MarkedCount); + } + + [Fact] + public async Task Dispatcher_RejectsDuplicateAcrossBothSources() + { + FakeDedup dedup = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, new FakeAck(), sink); + + Assert.Equal(EventDispatchStatus.Accepted, + (await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.WebSocket)).Status); + Assert.Equal(EventDispatchStatus.Duplicate, + (await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.Webhook)).Status); + Assert.Single(sink.Messages); + } + + [Fact] + public async Task Dispatcher_IgnoresUnknownEventButStillMarksIt() + { + FakeDedup dedup = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, new FakeAck(), sink); + Payload payload = Parse("""{"id":"EVT9","op":0,"s":9,"t":"FRIEND_ADD","d":{}}"""); + + EventDispatchResult result = await dispatcher.DispatchAsync(payload, QQEventSource.WebSocket); + + Assert.Equal(EventDispatchStatus.Ignored, result.Status); + Assert.Empty(sink.Messages); + Assert.Equal(1, dedup.MarkedCount); + } + + [Fact] + public async Task Dispatcher_NonDispatchOpcodeIsIgnoredWithoutDedup() + { + FakeDedup dedup = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, new FakeAck(), sink); + Payload payload = Parse("""{"id":"EVT10","op":10,"d":{"heartbeat_interval":45000}}"""); + + EventDispatchResult result = await dispatcher.DispatchAsync(payload, QQEventSource.WebSocket); + + Assert.Equal(EventDispatchStatus.Ignored, result.Status); + Assert.Empty(dedup.Seen); + Assert.Empty(sink.Messages); + } + + [Fact] + public async Task Dispatcher_InteractionAcksWithDIdAndRepliesByEventId() + { + FakeDedup dedup = new(); + FakeAck ack = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, ack, sink); + + EventDispatchResult result = await dispatcher.DispatchAsync(Parse(InteractionJson), QQEventSource.WebSocket); + + Assert.Equal(EventDispatchStatus.Accepted, result.Status); + Assert.Equal("int-1", Assert.Single(ack.InteractionIds)); // 不带 INTERACTION_CREATE: 前缀 + IBotMessage message = Assert.Single(sink.Messages); + Assert.Equal("INTERACTION_CREATE:int-1", message.EventId); + Assert.Equal("/签到", message.Detail); + } + + [Fact] + public async Task Dispatcher_InvalidPayloadReturnsInvalid() + { + FakeDedup dedup = new(); + FakeSink sink = new(); + QQEventDispatcher dispatcher = CreateDispatcher(dedup, new FakeAck(), sink); + Payload payload = Parse("""{"id":"EVT11","op":0,"s":11,"t":"C2C_MESSAGE_CREATE","d":{}}"""); + + EventDispatchResult result = await dispatcher.DispatchAsync(payload, QQEventSource.Webhook); + + Assert.Equal(EventDispatchStatus.Invalid, result.Status); + Assert.Empty(sink.Messages); + } + + [Fact] + public async Task Dispatcher_SinkFailureForgetsDedupMarkerForRetry() + { + FakeDedup dedup = new(); + FakeSink sink = new() { Throw = true }; + QQEventDispatcher dispatcher = CreateDispatcher(dedup, new FakeAck(), sink); + + EventDispatchResult result = await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.WebSocket); + + Assert.Equal(EventDispatchStatus.Failed, result.Status); + Assert.Equal("EVT1", Assert.Single(dedup.Forgotten)); + sink.Throw = false; + Assert.Equal(EventDispatchStatus.Accepted, + (await dispatcher.DispatchAsync(Parse(C2CJson), QQEventSource.WebSocket)).Status); + } + + [Fact] + public async Task MessageSender_UsesEventIdWithoutMsgIdOrSeq() + { + RecordingQQBotService qqBot = new(); + MessageSender sender = new(qqBot, new MemoryCache(new MemoryCacheOptions()), NullLogger.Instance); + C2CMessage message = new() + { + Id = "MSG1", Content = "查角色 1", Author = new Author { UserOpenId = "U1" }, + EventId = "INTERACTION_CREATE:int-1" + }; + + await sender.SendAsync(message, BotReply.FromText("结果")); + + SentCall call = Assert.Single(qqBot.Calls); + Assert.Equal("c2c-text", call.Kind); + Assert.Equal("INTERACTION_CREATE:int-1", call.EventId); + Assert.Null(call.MsgId); + Assert.Null(call.MsgSeq); + } + + [Fact] + public async Task MessageSender_IncrementsMsgSeqPerMsgId() + { + RecordingQQBotService qqBot = new(); + MessageSender sender = new(qqBot, new MemoryCache(new MemoryCacheOptions()), NullLogger.Instance); + C2CMessage message = new() { Id = "MSG1", Content = "查角色 1", Author = new Author { UserOpenId = "U1" } }; + + await sender.SendAsync(message, BotReply.FromText("第一次")); + await sender.SendAsync(message, BotReply.FromText("第二次")); + await sender.SendAsync(new C2CMessage { Id = "MSG2", Content = "x", Author = new Author { UserOpenId = "U1" } }, + BotReply.FromText("另一条")); + + Assert.Equal(3, qqBot.Calls.Count); + Assert.Equal(1, qqBot.Calls[0].MsgSeq); + Assert.Equal(2, qqBot.Calls[1].MsgSeq); + Assert.Equal(1, qqBot.Calls[2].MsgSeq); + Assert.All(qqBot.Calls, call => Assert.Null(call.EventId)); + } + + [Fact] + public async Task MessageSender_ProactiveMessageHasNoPassiveIdentifiers() + { + RecordingQQBotService qqBot = new(); + MessageSender sender = new(qqBot, new MemoryCache(new MemoryCacheOptions()), NullLogger.Instance); + GroupAtMessage message = new() + { + Id = "MSG3", GroupOpenId = "G1", Content = "查物品 1", + Author = new Author { MemberOpenId = "M1" }, SendProactive = true + }; + + await sender.SendAsync(message, BotReply.FromText("主动推送")); + + SentCall call = Assert.Single(qqBot.Calls); + Assert.Equal("group-text", call.Kind); + Assert.Null(call.MsgId); + Assert.Null(call.MsgSeq); + Assert.Null(call.EventId); + } + + private sealed class FakeDedup : IEventDeduplicationStore + { + public HashSet Seen { get; } = []; + public List Forgotten { get; } = []; + public int MarkedCount { get; private set; } + + public Task TryMarkAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + => Task.FromResult(Seen.Add($"{eventId}:{msgSeq}")); + + public Task MarkProcessedAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + { + MarkedCount++; + return Task.CompletedTask; + } + + public Task ForgetAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + { + Seen.Remove($"{eventId}:{msgSeq}"); + Forgotten.Add(eventId); + return Task.CompletedTask; + } + + public Task PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default) + => Task.FromResult(0); + } + + private sealed class FakeAck : IInteractionAckService + { + public List InteractionIds { get; } = []; + + public Task AckAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default) + { + InteractionIds.Add(interactionId); + return Task.FromResult(true); + } + } + + private sealed class FakeSink : IBotMessageSink + { + public List Messages { get; } = []; + public bool Throw { get; set; } + + public ValueTask QueueAsync(IBotMessage message, CancellationToken cancellationToken = default) + { + if (Throw) throw new InvalidOperationException("投递失败"); + Messages.Add(message); + return ValueTask.CompletedTask; + } + } + + internal readonly record struct SentCall(string Kind, string OpenId, string? MsgId, int? MsgSeq, string? EventId); + + private sealed class RecordingQQBotService : IQQBotService + { + public List Calls { get; } = []; + + public Task SendC2CMessageAsync(string openid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + { + Calls.Add(new SentCall("c2c-text", openid, msgId, msgSeq, eventId)); + return Task.CompletedTask; + } + + public Task SendGroupMessageAsync(string groupOpenid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + { + Calls.Add(new SentCall("group-text", groupOpenid, msgId, msgSeq, eventId)); + return Task.CompletedTask; + } + + public Task SendC2CMarkdownAsync(string openid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + { + Calls.Add(new SentCall("c2c-md", openid, msgId, msgSeq, eventId)); + return Task.CompletedTask; + } + + public Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + { + Calls.Add(new SentCall("group-md", groupOpenid, msgId, msgSeq, eventId)); + return Task.CompletedTask; + } + + public Task UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default) + => Task.FromResult(new UploadMediaResult()); + + public Task UploadGroupMediaAsync(string groupOpenid, int fileType, string url, CancellationToken cancellationToken = default) + => Task.FromResult(new UploadMediaResult()); + + public Task GetAccessTokenAsync(CancellationToken cancellationToken = default) => Task.FromResult("token"); + public Task RefreshTokenAsync(CancellationToken cancellationToken = default) => Task.FromResult("token"); + public Task AckInteractionAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default) + => Task.FromResult(true); + } +} diff --git a/OshimaBot.Tests/MessagingTests.cs b/OshimaBot.Tests/MessagingTests.cs index d0074b3..37aff6f 100644 --- a/OshimaBot.Tests/MessagingTests.cs +++ b/OshimaBot.Tests/MessagingTests.cs @@ -91,7 +91,7 @@ public sealed class MessagingTests keyboard.AppendButtons(3, buttons); Assert.Equal(3, keyboard.Content!.Rows.Count); Assert.Equal(3, keyboard.Content.Rows[0].Buttons.Count); - Assert.Equal(1, keyboard.Content.Rows[2].Buttons.Count); + Assert.Single(keyboard.Content.Rows[2].Buttons); } [Fact] diff --git a/OshimaBot/Controllers/QQBotController.cs b/OshimaBot/Controllers/QQBotController.cs index bff989d..2282b3f 100644 --- a/OshimaBot/Controllers/QQBotController.cs +++ b/OshimaBot/Controllers/QQBotController.cs @@ -1,6 +1,5 @@ using System.Text.Json; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using OshimaBot.Models; using OshimaBot.Security; @@ -13,8 +12,7 @@ namespace OshimaBot.Controllers public sealed class QQBotController( IOptions options, ILogger logger, - BotMessageQueue queue, - IMemoryCache memoryCache) : ControllerBase + IQQEventDispatcher dispatcher) : ControllerBase { private readonly QQBotOptions _options = options.Value; @@ -68,39 +66,14 @@ namespace OshimaBot.Controllers private async Task HandleEventAsync(Payload payload, CancellationToken cancellationToken) { - if (!string.IsNullOrWhiteSpace(payload.Id)) + EventDispatchResult result = await dispatcher.DispatchAsync(payload, QQEventSource.Webhook, cancellationToken); + return result.Status switch { - string cacheKey = $"OshimaBot:QQBot:Event:{payload.Id}"; - if (memoryCache.TryGetValue(cacheKey, out _)) return Ok(); - memoryCache.Set(cacheKey, true, TimeSpan.FromMinutes(10)); - } - - IBotMessage? message = payload.EventType switch - { - "C2C_MESSAGE_CREATE" => payload.Data.Deserialize(), - "GROUP_AT_MESSAGE_CREATE" => payload.Data.Deserialize(), - "INTERACTION_CREATE" => payload.Data.Deserialize(), - _ => null + EventDispatchStatus.Invalid => BadRequest(result.Reason), + // 失败时返回 5xx,让平台重推;占位记录已撤销,重推可重新处理。 + EventDispatchStatus.Failed => StatusCode(StatusCodes.Status500InternalServerError, result.Reason), + _ => Ok() }; - - if (message is null) - { - if (payload.EventType is "C2C_MESSAGE_CREATE" or "GROUP_AT_MESSAGE_CREATE" or "INTERACTION_CREATE") - { - return BadRequest("无效的消息事件数据。"); - } - logger.LogDebug("忽略未定义的 QQ 事件:{EventType}", payload.EventType); - return Ok(); - } - - if (string.IsNullOrWhiteSpace(message.OpenId) || string.IsNullOrWhiteSpace(message.Detail)) - { - return Ok(); - } - - logger.LogInformation("收到 QQ {EventType} 消息:MessageId={MessageId}", payload.EventType, message.Id); - await queue.QueueAsync(message, cancellationToken); - return Ok(); } [RequireApiKey] diff --git a/OshimaBot/Messaging/InteractionAckService.cs b/OshimaBot/Messaging/InteractionAckService.cs new file mode 100644 index 0000000..7ea63e5 --- /dev/null +++ b/OshimaBot/Messaging/InteractionAckService.cs @@ -0,0 +1,39 @@ +using OshimaBot.Services; + +namespace OshimaBot.Messaging; + +/// +/// INTERACTION_CREATE 必须回执 PUT /interactions/{id},否则客户端会一直 loading 直到超时。 +/// 仅 type=11(消息按钮)/type=12(快捷菜单)需要,其它类型调用也不报错。 +/// +public interface IInteractionAckService +{ + Task AckAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default); +} + +public sealed class InteractionAckService( + IQQBotService qqBot, + ILogger logger) : IInteractionAckService +{ + public async Task AckAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(interactionId)) + { + logger.LogWarning("互动事件缺少 interaction_id,跳过回执。"); + return false; + } + + try + { + bool ok = await qqBot.AckInteractionAsync(interactionId, code, cancellationToken); + if (!ok) logger.LogWarning("互动事件回执未成功:InteractionId={InteractionId}", interactionId); + return ok; + } + catch (Exception exception) + { + // 回执失败不能阻断指令处理(例如 QQBot 尚未配置凭据的本地开发环境)。 + logger.LogWarning(exception, "互动事件回执异常:InteractionId={InteractionId}", interactionId); + return false; + } + } +} diff --git a/OshimaBot/Messaging/MessageSender.cs b/OshimaBot/Messaging/MessageSender.cs index a6ef014..c5b1dad 100644 --- a/OshimaBot/Messaging/MessageSender.cs +++ b/OshimaBot/Messaging/MessageSender.cs @@ -6,7 +6,7 @@ namespace OshimaBot.Messaging; /// /// 出站消息统一入口:修正旧版 msgSeq 恒为 1 的缺陷(同一 msg_id 第二次回复会触发 40054005), -/// 按 msg_id 在被动窗口内递增序号;第三方消息则直接写回结果对象。 +/// 按 msg_id 在被动窗口内递增序号;互动事件改用互斥的 event_id;第三方消息直接写回结果对象。 /// public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogger logger) { @@ -22,18 +22,20 @@ public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogg return; } - string? msgId = message.SendProactive ? null : message.Id; - int? msgSeq = string.IsNullOrWhiteSpace(msgId) ? null : NextSeq(msgId); + // event_id(互动事件)与 msg_id(消息事件)互斥;主动消息两者都不带。 + string? eventId = message.SendProactive ? null : message.EventId; + string? msgId = !string.IsNullOrWhiteSpace(eventId) || message.SendProactive ? null : message.Id; + int? msgSeq = string.IsNullOrWhiteSpace(msgId) ? null : NextSeq(msgId!); if (reply.Markdown is not null && !string.IsNullOrWhiteSpace(reply.Markdown.Content)) { if (message.IsGroup) { - await qqBot.SendGroupMarkdownAsync(message.OpenId, reply.Markdown, reply.Keyboard, msgId, msgSeq, cancellationToken); + await qqBot.SendGroupMarkdownAsync(message.OpenId, reply.Markdown, reply.Keyboard, msgId, msgSeq, cancellationToken, eventId); } else { - await qqBot.SendC2CMarkdownAsync(message.OpenId, reply.Markdown, reply.Keyboard, msgId, msgSeq, cancellationToken); + await qqBot.SendC2CMarkdownAsync(message.OpenId, reply.Markdown, reply.Keyboard, msgId, msgSeq, cancellationToken, eventId); } return; } @@ -43,11 +45,13 @@ public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogg string content = message.IsGroup ? "\r\n" + reply.Text.Trim() : reply.Text.Trim(); if (message.IsGroup) { - await qqBot.SendGroupMessageAsync(message.OpenId, content, msgId: msgId, msgSeq: msgSeq, cancellationToken: cancellationToken); + await qqBot.SendGroupMessageAsync(message.OpenId, content, msgId: msgId, msgSeq: msgSeq, + cancellationToken: cancellationToken, eventId: eventId); } else { - await qqBot.SendC2CMessageAsync(message.OpenId, content, msgId: msgId, msgSeq: msgSeq, cancellationToken: cancellationToken); + await qqBot.SendC2CMessageAsync(message.OpenId, content, msgId: msgId, msgSeq: msgSeq, + cancellationToken: cancellationToken, eventId: eventId); } } diff --git a/OshimaBot/Models/BotMessages.cs b/OshimaBot/Models/BotMessages.cs index d64892e..28f8c87 100644 --- a/OshimaBot/Models/BotMessages.cs +++ b/OshimaBot/Models/BotMessages.cs @@ -41,6 +41,9 @@ namespace OshimaBot.Models string AuthorOpenId { get; } long FunGameUID { get; set; } bool SendProactive { get; set; } + + /// 互动事件的被动回复使用 event_id(与 msg_id 互斥);消息事件为 null。 + string? EventId { get; set; } } public sealed class ThirdPartyMessage : IBotMessage @@ -54,6 +57,7 @@ namespace OshimaBot.Models [JsonPropertyName("authoropenid")] public string AuthorOpenId { get; set; } = ""; [JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public bool SendProactive { get; set; } + [JsonIgnore] public string? EventId { get; set; } [JsonIgnore] public bool IsCompleted { get; set; } [JsonIgnore] public string Result { get; set; } = ""; } @@ -66,6 +70,7 @@ namespace OshimaBot.Models [JsonPropertyName("timestamp")] public string Timestamp { get; set; } = ""; [JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public bool SendProactive { get; set; } + [JsonIgnore] public string? EventId { get; set; } public string Detail { get => Content; set => Content = value; } public string OpenId => Author.UserOpenId; public string AuthorOpenId => Author.UserOpenId; @@ -81,6 +86,7 @@ namespace OshimaBot.Models [JsonPropertyName("group_openid")] public string GroupOpenId { get; set; } = ""; [JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public bool SendProactive { get; set; } + [JsonIgnore] public string? EventId { get; set; } public string Detail { get => Content; set => Content = value; } public string OpenId => GroupOpenId; public string AuthorOpenId => Author.MemberOpenId; @@ -97,6 +103,7 @@ namespace OshimaBot.Models [JsonPropertyName("data")] public InteractionData Data { get; set; } = new(); [JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public bool SendProactive { get; set; } + [JsonIgnore] public string? EventId { get; set; } public string Detail { get => Data.Resolved?.ButtonData ?? ""; set { } } public string OpenId => IsGroup ? GroupOpenId : UserOpenId; public string AuthorOpenId => UserOpenId; diff --git a/OshimaBot/Models/BotOptions.cs b/OshimaBot/Models/BotOptions.cs index 04c4b24..4406118 100644 --- a/OshimaBot/Models/BotOptions.cs +++ b/OshimaBot/Models/BotOptions.cs @@ -67,6 +67,9 @@ namespace OshimaBot.Models /// 启动时自动应用 EF Core Migration。 public bool ApplyMigrationsOnStartup { get; set; } = true; + + /// processed_events 幂等记录保留时长(小时),过期由后台任务每小时清理。 + public int ProcessedEventRetentionHours { get; set; } = 24; } public sealed class ServerOptions diff --git a/OshimaBot/Program.cs b/OshimaBot/Program.cs index 47475c2..2908880 100644 --- a/OshimaBot/Program.cs +++ b/OshimaBot/Program.cs @@ -60,6 +60,11 @@ builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(sp => sp.GetRequiredService()); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddHostedService(); builder.Services.AddHostedService(); WebApplication app = builder.Build(); diff --git a/OshimaBot/Services/BotMessageQueue.cs b/OshimaBot/Services/BotMessageQueue.cs index bda6f21..bc76f68 100644 --- a/OshimaBot/Services/BotMessageQueue.cs +++ b/OshimaBot/Services/BotMessageQueue.cs @@ -3,7 +3,7 @@ using OshimaBot.Models; namespace OshimaBot.Services { - public sealed class BotMessageQueue + public sealed class BotMessageQueue : IBotMessageSink { private readonly Channel _channel = Channel.CreateBounded(new BoundedChannelOptions(1_000) { diff --git a/OshimaBot/Services/EventDeduplicationStore.cs b/OshimaBot/Services/EventDeduplicationStore.cs new file mode 100644 index 0000000..f13d8e9 --- /dev/null +++ b/OshimaBot/Services/EventDeduplicationStore.cs @@ -0,0 +1,104 @@ +using Microsoft.EntityFrameworkCore; +using OshimaBot.Data; +using OshimaBot.Data.Entities; + +namespace OshimaBot.Services; + +/// +/// 事件幂等:以 processed_events 的唯一主键 (EventId, MsgSeq) 判定首次到达, +/// 替代 S1 之前 Webhook 里进程内的 IMemoryCache(重启即失效、多实例不共享)。 +/// +public interface IEventDeduplicationStore +{ + Task TryMarkAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default); + Task MarkProcessedAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default); + Task ForgetAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default); + Task PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default); +} + +public sealed class EventDeduplicationStore( + IServiceScopeFactory scopeFactory, + ILogger logger) : IEventDeduplicationStore +{ + public async Task TryMarkAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + db.ProcessedEvents.Add(new ProcessedEvent + { + EventId = eventId, + MsgSeq = msgSeq, + ReceivedAtUtc = DateTime.UtcNow + }); + try + { + await db.SaveChangesAsync(cancellationToken); + return true; + } + catch (DbUpdateException) + { + // SQLite 唯一键冲突:同一事件重复推送(平台重试 / webhook+WS 双通道)。 + logger.LogDebug("重复 QQ 事件,已跳过:EventId={EventId}", eventId); + return false; + } + } + + public async Task MarkProcessedAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + DateTime now = DateTime.UtcNow; + await db.ProcessedEvents + .Where(x => x.EventId == eventId && x.MsgSeq == msgSeq) + .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.ProcessedAtUtc, now), cancellationToken); + } + + /// 处理失败时撤销占位,允许平台重推后重新处理。 + public async Task ForgetAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default) + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + await db.ProcessedEvents + .Where(x => x.EventId == eventId && x.MsgSeq == msgSeq) + .ExecuteDeleteAsync(cancellationToken); + } + + public async Task PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default) + { + using IServiceScope scope = scopeFactory.CreateScope(); + BotDbContext db = scope.ServiceProvider.GetRequiredService(); + DateTime cutoff = DateTime.UtcNow - retention; + return await db.ProcessedEvents + .Where(x => x.ReceivedAtUtc < cutoff) + .ExecuteDeleteAsync(cancellationToken); + } +} + +/// 定期清理幂等表,避免 processed_events 无限增长。 +public sealed class ProcessedEventCleanupService( + IEventDeduplicationStore store, + Microsoft.Extensions.Options.IOptions options, + ILogger logger) : BackgroundService +{ + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + TimeSpan retention = TimeSpan.FromHours(Math.Max(1, options.Value.ProcessedEventRetentionHours)); + using PeriodicTimer timer = new(TimeSpan.FromHours(1)); + while (await timer.WaitForNextTickAsync(stoppingToken)) + { + try + { + int removed = await store.PurgeAsync(retention, stoppingToken); + if (removed > 0) logger.LogInformation("已清理过期事件幂等记录:{Count} 条", removed); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + break; + } + catch (Exception exception) + { + logger.LogWarning(exception, "清理过期事件幂等记录失败。"); + } + } + } +} diff --git a/OshimaBot/Services/QQBotService.cs b/OshimaBot/Services/QQBotService.cs index a1cdddb..89a09ff 100644 --- a/OshimaBot/Services/QQBotService.cs +++ b/OshimaBot/Services/QQBotService.cs @@ -9,14 +9,17 @@ namespace OshimaBot.Services { public interface IQQBotService { - Task SendC2CMessageAsync(string openid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default); - Task SendGroupMessageAsync(string groupOpenid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default); - Task SendC2CMarkdownAsync(string openid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default); - Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default); + Task SendC2CMessageAsync(string openid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null); + Task SendGroupMessageAsync(string groupOpenid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null); + Task SendC2CMarkdownAsync(string openid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null); + Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null); Task UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default); Task UploadGroupMediaAsync(string groupOpenid, int fileType, string url, CancellationToken cancellationToken = default); Task GetAccessTokenAsync(CancellationToken cancellationToken = default); Task RefreshTokenAsync(CancellationToken cancellationToken = default); + + /// 回应 INTERACTION_CREATE,否则客户端会一直 loading。同一 interaction_id 只能回一次。 + Task AckInteractionAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default); } public sealed class QQBotService( @@ -33,30 +36,30 @@ namespace OshimaBot.Services private readonly HttpClient _httpClient = httpClientFactory.CreateClient(HttpClientName); private readonly SemaphoreSlim _tokenLock = new(1, 1); - public Task SendC2CMessageAsync(string openid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default) - => SendMessageAsync($"/v2/users/{Uri.EscapeDataString(openid)}/messages", content, msgType, media, msgId, msgSeq, cancellationToken); + public Task SendC2CMessageAsync(string openid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + => SendMessageAsync($"/v2/users/{Uri.EscapeDataString(openid)}/messages", content, msgType, media, msgId, msgSeq, eventId, cancellationToken); - public Task SendGroupMessageAsync(string groupOpenid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default) - => SendMessageAsync($"/v2/groups/{Uri.EscapeDataString(groupOpenid)}/messages", content, msgType, media, msgId, msgSeq, cancellationToken); + public Task SendGroupMessageAsync(string groupOpenid, string content, int msgType = 0, object? media = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + => SendMessageAsync($"/v2/groups/{Uri.EscapeDataString(groupOpenid)}/messages", content, msgType, media, msgId, msgSeq, eventId, cancellationToken); - public Task SendC2CMarkdownAsync(string openid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default) - => SendMarkdownMessageAsync($"/v2/users/{Uri.EscapeDataString(openid)}/messages", markdown, keyboard, msgId, msgSeq, cancellationToken); + public Task SendC2CMarkdownAsync(string openid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + => SendMarkdownMessageAsync($"/v2/users/{Uri.EscapeDataString(openid)}/messages", markdown, keyboard, msgId, msgSeq, eventId, cancellationToken); - public Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default) - => SendMarkdownMessageAsync($"/v2/groups/{Uri.EscapeDataString(groupOpenid)}/messages", markdown, keyboard, msgId, msgSeq, cancellationToken); + public Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null) + => SendMarkdownMessageAsync($"/v2/groups/{Uri.EscapeDataString(groupOpenid)}/messages", markdown, keyboard, msgId, msgSeq, eventId, cancellationToken); - private async Task SendMessageAsync(string path, string content, int msgType, object? media, string? msgId, int? msgSeq, CancellationToken cancellationToken) + private async Task SendMessageAsync(string path, string content, int msgType, object? media, string? msgId, int? msgSeq, string? eventId, CancellationToken cancellationToken) { Dictionary body = new() { ["content"] = content, ["msg_type"] = msgType }; - AddMessageContext(body, media, msgId, msgSeq); + AddMessageContext(body, media, msgId, msgSeq, eventId); await PostAsync(path, body, cancellationToken); } - private async Task SendMarkdownMessageAsync(string path, MarkdownMessage markdown, KeyboardMessage? keyboard, string? msgId, int? msgSeq, CancellationToken cancellationToken) + private async Task SendMarkdownMessageAsync(string path, MarkdownMessage markdown, KeyboardMessage? keyboard, string? msgId, int? msgSeq, string? eventId, CancellationToken cancellationToken) { Dictionary body = new() { @@ -64,17 +67,43 @@ namespace OshimaBot.Services ["markdown"] = markdown }; if (keyboard is not null) body["keyboard"] = keyboard; - AddMessageContext(body, null, msgId, msgSeq); + AddMessageContext(body, null, msgId, msgSeq, eventId); await PostAsync(path, body, cancellationToken); } - private static void AddMessageContext(Dictionary body, object? media, string? msgId, int? msgSeq) + /// event_id 与 msg_id 二选一:互动事件用 event_id,普通消息事件用 msg_id + 递增 msg_seq。 + private static void AddMessageContext(Dictionary body, object? media, string? msgId, int? msgSeq, string? eventId) { if (media is not null) body["media"] = media; + if (!string.IsNullOrWhiteSpace(eventId)) + { + body["event_id"] = eventId; + return; + } if (!string.IsNullOrWhiteSpace(msgId)) body["msg_id"] = msgId; if (msgSeq.HasValue) body["msg_seq"] = msgSeq.Value; } + public async Task AckInteractionAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(interactionId)) return false; + + EnsureConfigured(); + string accessToken = await GetAccessTokenAsync(cancellationToken); + using HttpRequestMessage request = new(HttpMethod.Put, + $"{_options.ResolvedApiBaseAddress}/interactions/{Uri.EscapeDataString(interactionId)}"); + request.Headers.Authorization = new AuthenticationHeaderValue("QQBot", accessToken); + request.Content = new StringContent(JsonSerializer.Serialize(new { code }, JsonOptions), Encoding.UTF8, "application/json"); + + using HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken); + if (response.IsSuccessStatusCode) return true; + + string error = await response.Content.ReadAsStringAsync(cancellationToken); + logger.LogWarning("互动事件回执失败:InteractionId={InteractionId}, Status={StatusCode}, {Response}", + interactionId, (int)response.StatusCode, error); + return false; + } + public Task UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default) => UploadMediaAsync($"/v2/users/{Uri.EscapeDataString(openid)}/files", fileType, url, cancellationToken); diff --git a/OshimaBot/Services/QQEventDispatcher.cs b/OshimaBot/Services/QQEventDispatcher.cs new file mode 100644 index 0000000..c46d78f --- /dev/null +++ b/OshimaBot/Services/QQEventDispatcher.cs @@ -0,0 +1,159 @@ +using System.Text.Json; +using OshimaBot.Messaging; +using OshimaBot.Models; + +namespace OshimaBot.Services; + +public enum QQEventSource +{ + Webhook, + WebSocket +} + +public enum EventDispatchStatus +{ + Accepted, + Duplicate, + Ignored, + Invalid, + Failed +} + +public readonly record struct EventDispatchResult(EventDispatchStatus Status, string? Reason = null) +{ + public static EventDispatchResult Accepted() => new(EventDispatchStatus.Accepted); + public static EventDispatchResult Duplicate() => new(EventDispatchStatus.Duplicate, "事件已处理过。"); + public static EventDispatchResult Ignored(string? reason = null) => new(EventDispatchStatus.Ignored, reason); + public static EventDispatchResult Invalid(string reason) => new(EventDispatchStatus.Invalid, reason); + public static EventDispatchResult Failed(string reason) => new(EventDispatchStatus.Failed, reason); +} + +/// 消息投递抽象:Webhook 与 WebSocket 共用同一个出口,便于单测替换。 +public interface IBotMessageSink +{ + ValueTask QueueAsync(IBotMessage message, CancellationToken cancellationToken = default); +} + +public interface IQQEventDispatcher +{ + Task DispatchAsync(Payload payload, QQEventSource source, CancellationToken cancellationToken = default); +} + +/// +/// 统一事件管线(S3):去重 → 互动回执 → 事件转 IBotMessage → 投递 BotMessageQueue。 +/// Webhook 与 WebSocket 两条入口都走这里,指令层(RainBotService)零改动。 +/// +public sealed class QQEventDispatcher( + IEventDeduplicationStore deduplication, + IInteractionAckService interactionAck, + IBotMessageSink sink, + ILogger logger) : IQQEventDispatcher +{ + private const string InteractionPrefix = "INTERACTION_CREATE:"; + private static readonly HashSet HandledEvents = new(StringComparer.Ordinal) + { + "C2C_MESSAGE_CREATE", + "GROUP_AT_MESSAGE_CREATE", + "INTERACTION_CREATE" + }; + + public async Task DispatchAsync(Payload payload, QQEventSource source, CancellationToken cancellationToken = default) + { + if (payload is null) return EventDispatchResult.Invalid("Payload 为空。"); + if (payload.Op != 0) return EventDispatchResult.Ignored("非 Dispatch(op=0) Payload。"); + + string eventKey = ResolveEventKey(payload, source); + bool firstSeen = await deduplication.TryMarkAsync(eventKey, 0, cancellationToken); + if (!firstSeen) + { + logger.LogDebug("忽略重复 QQ 事件:Source={Source}, EventId={EventId}", source, eventKey); + return EventDispatchResult.Duplicate(); + } + + try + { + if (!HandledEvents.Contains(payload.EventType)) + { + await deduplication.MarkProcessedAsync(eventKey, 0, cancellationToken); + logger.LogDebug("忽略未订阅的 QQ 事件:{EventType}", payload.EventType); + return EventDispatchResult.Ignored($"未处理的事件类型 {payload.EventType}。"); + } + + if (string.Equals(payload.EventType, "INTERACTION_CREATE", StringComparison.Ordinal)) + { + // 官方:interaction_id 取 d.id,不带 "INTERACTION_CREATE:" 前缀。 + await interactionAck.AckAsync(ExtractInteractionId(payload), 0, cancellationToken); + } + + IBotMessage? message = ParseMessage(payload); + if (message is null || string.IsNullOrWhiteSpace(message.OpenId) || string.IsNullOrWhiteSpace(message.Detail)) + { + await deduplication.MarkProcessedAsync(eventKey, 0, cancellationToken); + return EventDispatchResult.Invalid("事件消息数据无效。"); + } + + // 互动事件只能用 event_id 被动回复;消息事件用 d.id 作为 msg_id。 + message.EventId = string.Equals(payload.EventType, "INTERACTION_CREATE", StringComparison.Ordinal) + && !string.IsNullOrWhiteSpace(payload.Id) + ? payload.Id + : null; + + await sink.QueueAsync(message, cancellationToken); + await deduplication.MarkProcessedAsync(eventKey, 0, cancellationToken); + logger.LogInformation("已接收 QQ {EventType} 事件:Source={Source}, EventId={EventId}, MessageId={MessageId}", + payload.EventType, source, eventKey, message.Id); + return EventDispatchResult.Accepted(); + } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) + { + // 撤销占位,让平台重推时有机会重新处理。 + await deduplication.ForgetAsync(eventKey, 0, CancellationToken.None); + logger.LogError(exception, "QQ 事件分发失败:Source={Source}, EventId={EventId}", source, eventKey); + return EventDispatchResult.Failed("事件分发失败。"); + } + } + + private static string ResolveEventKey(Payload payload, QQEventSource source) + { + if (!string.IsNullOrWhiteSpace(payload.Id)) return payload.Id; + // 官方 Payload 顶层 id 为事件 ID;WS 下行必然带 s,作为兜底键。 + if (payload.SequenceNumber > 0) return $"{source}:seq:{payload.SequenceNumber}"; + return $"{source}:noid:{Guid.NewGuid():N}"; + } + + private static string ExtractInteractionId(Payload payload) + { + if (payload.Data.ValueKind == JsonValueKind.Object + && payload.Data.TryGetProperty("id", out JsonElement id) + && id.ValueKind == JsonValueKind.String) + { + string? value = id.GetString(); + if (!string.IsNullOrWhiteSpace(value)) return value!; + } + return payload.Id.StartsWith(InteractionPrefix, StringComparison.Ordinal) + ? payload.Id[InteractionPrefix.Length..] + : payload.Id; + } + + private static IBotMessage? ParseMessage(Payload payload) + { + try + { + return payload.EventType switch + { + "C2C_MESSAGE_CREATE" => payload.Data.Deserialize(), + "GROUP_AT_MESSAGE_CREATE" => payload.Data.Deserialize(), + "INTERACTION_CREATE" => payload.Data.Deserialize(), + _ => null + }; + } + catch (JsonException) + { + return null; + } + } +} diff --git a/OshimaBot/appsettings.json b/OshimaBot/appsettings.json index b8b0e57..f15567a 100644 --- a/OshimaBot/appsettings.json +++ b/OshimaBot/appsettings.json @@ -22,7 +22,8 @@ }, "Database": { "ConnectionString": "Data Source=oshimobot.db;Default Timeout=30", - "ApplyMigrationsOnStartup": true + "ApplyMigrationsOnStartup": true, + "ProcessedEventRetentionHours": 24 }, "Server": { "ApiKey": "",