feat(S3): unify event dispatch with SQLite dedup, interaction ACK and event_id replies

This commit is contained in:
2026-11-09 22:06:34 +08:00
parent 02c534b0ef
commit 968536e5b6
13 changed files with 706 additions and 61 deletions
+321
View File
@@ -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<Payload>(json)!;
private static QQEventDispatcher CreateDispatcher(
FakeDedup dedup, FakeAck ack, FakeSink sink) => new(
dedup, ack, sink, NullLogger<QQEventDispatcher>.Instance);
[Fact]
public async Task DedupStore_SecondMarkIsRejected()
{
using SqliteConnection connection = new("Data Source=:memory:");
await connection.OpenAsync();
ServiceCollection services = new();
services.AddLogging();
services.AddDbContext<BotDbContext>(options => options.UseSqlite(connection));
services.AddSingleton<IEventDeduplicationStore, EventDeduplicationStore>();
await using ServiceProvider provider = services.BuildServiceProvider();
using (IServiceScope scope = provider.CreateScope())
{
await scope.ServiceProvider.GetRequiredService<BotDbContext>().Database.EnsureCreatedAsync();
}
IEventDeduplicationStore store = provider.GetRequiredService<IEventDeduplicationStore>();
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<C2CMessage>(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<MessageSender>.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<MessageSender>.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<MessageSender>.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<string> Seen { get; } = [];
public List<string> Forgotten { get; } = [];
public int MarkedCount { get; private set; }
public Task<bool> 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<int> PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default)
=> Task.FromResult(0);
}
private sealed class FakeAck : IInteractionAckService
{
public List<string> InteractionIds { get; } = [];
public Task<bool> AckAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default)
{
InteractionIds.Add(interactionId);
return Task.FromResult(true);
}
}
private sealed class FakeSink : IBotMessageSink
{
public List<IBotMessage> 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<SentCall> 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<UploadMediaResult> UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default)
=> Task.FromResult(new UploadMediaResult());
public Task<UploadMediaResult> UploadGroupMediaAsync(string groupOpenid, int fileType, string url, CancellationToken cancellationToken = default)
=> Task.FromResult(new UploadMediaResult());
public Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default) => Task.FromResult("token");
public Task<string> RefreshTokenAsync(CancellationToken cancellationToken = default) => Task.FromResult("token");
public Task<bool> AckInteractionAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default)
=> Task.FromResult(true);
}
}
+1 -1
View File
@@ -91,7 +91,7 @@ public sealed class MessagingTests
keyboard.AppendButtons(3, buttons); keyboard.AppendButtons(3, buttons);
Assert.Equal(3, keyboard.Content!.Rows.Count); Assert.Equal(3, keyboard.Content!.Rows.Count);
Assert.Equal(3, keyboard.Content.Rows[0].Buttons.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] [Fact]
+7 -34
View File
@@ -1,6 +1,5 @@
using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using OshimaBot.Models; using OshimaBot.Models;
using OshimaBot.Security; using OshimaBot.Security;
@@ -13,8 +12,7 @@ namespace OshimaBot.Controllers
public sealed class QQBotController( public sealed class QQBotController(
IOptions<QQBotOptions> options, IOptions<QQBotOptions> options,
ILogger<QQBotController> logger, ILogger<QQBotController> logger,
BotMessageQueue queue, IQQEventDispatcher dispatcher) : ControllerBase
IMemoryCache memoryCache) : ControllerBase
{ {
private readonly QQBotOptions _options = options.Value; private readonly QQBotOptions _options = options.Value;
@@ -68,39 +66,14 @@ namespace OshimaBot.Controllers
private async Task<IActionResult> HandleEventAsync(Payload payload, CancellationToken cancellationToken) private async Task<IActionResult> 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}"; EventDispatchStatus.Invalid => BadRequest(result.Reason),
if (memoryCache.TryGetValue(cacheKey, out _)) return Ok(); // 失败时返回 5xx,让平台重推;占位记录已撤销,重推可重新处理。
memoryCache.Set(cacheKey, true, TimeSpan.FromMinutes(10)); EventDispatchStatus.Failed => StatusCode(StatusCodes.Status500InternalServerError, result.Reason),
} _ => Ok()
IBotMessage? message = payload.EventType switch
{
"C2C_MESSAGE_CREATE" => payload.Data.Deserialize<C2CMessage>(),
"GROUP_AT_MESSAGE_CREATE" => payload.Data.Deserialize<GroupAtMessage>(),
"INTERACTION_CREATE" => payload.Data.Deserialize<InteractionEvent>(),
_ => null
}; };
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] [RequireApiKey]
@@ -0,0 +1,39 @@
using OshimaBot.Services;
namespace OshimaBot.Messaging;
/// <summary>
/// INTERACTION_CREATE 必须回执 PUT /interactions/{id},否则客户端会一直 loading 直到超时。
/// 仅 type=11(消息按钮)/type=12(快捷菜单)需要,其它类型调用也不报错。
/// </summary>
public interface IInteractionAckService
{
Task<bool> AckAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default);
}
public sealed class InteractionAckService(
IQQBotService qqBot,
ILogger<InteractionAckService> logger) : IInteractionAckService
{
public async Task<bool> 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;
}
}
}
+11 -7
View File
@@ -6,7 +6,7 @@ namespace OshimaBot.Messaging;
/// <summary> /// <summary>
/// 出站消息统一入口:修正旧版 msgSeq 恒为 1 的缺陷(同一 msg_id 第二次回复会触发 40054005), /// 出站消息统一入口:修正旧版 msgSeq 恒为 1 的缺陷(同一 msg_id 第二次回复会触发 40054005),
/// 按 msg_id 在被动窗口内递增序号;第三方消息则直接写回结果对象。 /// 按 msg_id 在被动窗口内递增序号;互动事件改用互斥的 event_id;第三方消息直接写回结果对象。
/// </summary> /// </summary>
public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogger<MessageSender> logger) public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogger<MessageSender> logger)
{ {
@@ -22,18 +22,20 @@ public sealed class MessageSender(IQQBotService qqBot, IMemoryCache cache, ILogg
return; return;
} }
string? msgId = message.SendProactive ? null : message.Id; // event_id(互动事件)与 msg_id(消息事件)互斥;主动消息两者都不带。
int? msgSeq = string.IsNullOrWhiteSpace(msgId) ? null : NextSeq(msgId); 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 (reply.Markdown is not null && !string.IsNullOrWhiteSpace(reply.Markdown.Content))
{ {
if (message.IsGroup) 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 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; 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(); string content = message.IsGroup ? "\r\n" + reply.Text.Trim() : reply.Text.Trim();
if (message.IsGroup) 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 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);
} }
} }
+7
View File
@@ -41,6 +41,9 @@ namespace OshimaBot.Models
string AuthorOpenId { get; } string AuthorOpenId { get; }
long FunGameUID { get; set; } long FunGameUID { get; set; }
bool SendProactive { get; set; } bool SendProactive { get; set; }
/// <summary>互动事件的被动回复使用 event_id(与 msg_id 互斥);消息事件为 null。</summary>
string? EventId { get; set; }
} }
public sealed class ThirdPartyMessage : IBotMessage public sealed class ThirdPartyMessage : IBotMessage
@@ -54,6 +57,7 @@ namespace OshimaBot.Models
[JsonPropertyName("authoropenid")] public string AuthorOpenId { get; set; } = ""; [JsonPropertyName("authoropenid")] public string AuthorOpenId { get; set; } = "";
[JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public long FunGameUID { get; set; }
[JsonIgnore] public bool SendProactive { get; set; } [JsonIgnore] public bool SendProactive { get; set; }
[JsonIgnore] public string? EventId { get; set; }
[JsonIgnore] public bool IsCompleted { get; set; } [JsonIgnore] public bool IsCompleted { get; set; }
[JsonIgnore] public string Result { get; set; } = ""; [JsonIgnore] public string Result { get; set; } = "";
} }
@@ -66,6 +70,7 @@ namespace OshimaBot.Models
[JsonPropertyName("timestamp")] public string Timestamp { get; set; } = ""; [JsonPropertyName("timestamp")] public string Timestamp { get; set; } = "";
[JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public long FunGameUID { get; set; }
[JsonIgnore] public bool SendProactive { get; set; } [JsonIgnore] public bool SendProactive { get; set; }
[JsonIgnore] public string? EventId { get; set; }
public string Detail { get => Content; set => Content = value; } public string Detail { get => Content; set => Content = value; }
public string OpenId => Author.UserOpenId; public string OpenId => Author.UserOpenId;
public string AuthorOpenId => Author.UserOpenId; public string AuthorOpenId => Author.UserOpenId;
@@ -81,6 +86,7 @@ namespace OshimaBot.Models
[JsonPropertyName("group_openid")] public string GroupOpenId { get; set; } = ""; [JsonPropertyName("group_openid")] public string GroupOpenId { get; set; } = "";
[JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public long FunGameUID { get; set; }
[JsonIgnore] public bool SendProactive { get; set; } [JsonIgnore] public bool SendProactive { get; set; }
[JsonIgnore] public string? EventId { get; set; }
public string Detail { get => Content; set => Content = value; } public string Detail { get => Content; set => Content = value; }
public string OpenId => GroupOpenId; public string OpenId => GroupOpenId;
public string AuthorOpenId => Author.MemberOpenId; public string AuthorOpenId => Author.MemberOpenId;
@@ -97,6 +103,7 @@ namespace OshimaBot.Models
[JsonPropertyName("data")] public InteractionData Data { get; set; } = new(); [JsonPropertyName("data")] public InteractionData Data { get; set; } = new();
[JsonIgnore] public long FunGameUID { get; set; } [JsonIgnore] public long FunGameUID { get; set; }
[JsonIgnore] public bool SendProactive { get; set; } [JsonIgnore] public bool SendProactive { get; set; }
[JsonIgnore] public string? EventId { get; set; }
public string Detail { get => Data.Resolved?.ButtonData ?? ""; set { } } public string Detail { get => Data.Resolved?.ButtonData ?? ""; set { } }
public string OpenId => IsGroup ? GroupOpenId : UserOpenId; public string OpenId => IsGroup ? GroupOpenId : UserOpenId;
public string AuthorOpenId => UserOpenId; public string AuthorOpenId => UserOpenId;
+3
View File
@@ -67,6 +67,9 @@ namespace OshimaBot.Models
/// <summary>启动时自动应用 EF Core Migration。</summary> /// <summary>启动时自动应用 EF Core Migration。</summary>
public bool ApplyMigrationsOnStartup { get; set; } = true; public bool ApplyMigrationsOnStartup { get; set; } = true;
/// <summary>processed_events 幂等记录保留时长(小时),过期由后台任务每小时清理。</summary>
public int ProcessedEventRetentionHours { get; set; } = 24;
} }
public sealed class ServerOptions public sealed class ServerOptions
+5
View File
@@ -60,6 +60,11 @@ builder.Services.AddSingleton<FunGameQueryService>();
builder.Services.AddScoped<MessageSender>(); builder.Services.AddScoped<MessageSender>();
builder.Services.AddScoped<RainBotService>(); builder.Services.AddScoped<RainBotService>();
builder.Services.AddSingleton<BotMessageQueue>(); builder.Services.AddSingleton<BotMessageQueue>();
builder.Services.AddSingleton<IBotMessageSink>(sp => sp.GetRequiredService<BotMessageQueue>());
builder.Services.AddSingleton<IEventDeduplicationStore, EventDeduplicationStore>();
builder.Services.AddSingleton<IInteractionAckService, InteractionAckService>();
builder.Services.AddSingleton<IQQEventDispatcher, QQEventDispatcher>();
builder.Services.AddHostedService<ProcessedEventCleanupService>();
builder.Services.AddHostedService<BotMessageBackgroundService>(); builder.Services.AddHostedService<BotMessageBackgroundService>();
WebApplication app = builder.Build(); WebApplication app = builder.Build();
+1 -1
View File
@@ -3,7 +3,7 @@ using OshimaBot.Models;
namespace OshimaBot.Services namespace OshimaBot.Services
{ {
public sealed class BotMessageQueue public sealed class BotMessageQueue : IBotMessageSink
{ {
private readonly Channel<IBotMessage> _channel = Channel.CreateBounded<IBotMessage>(new BoundedChannelOptions(1_000) private readonly Channel<IBotMessage> _channel = Channel.CreateBounded<IBotMessage>(new BoundedChannelOptions(1_000)
{ {
@@ -0,0 +1,104 @@
using Microsoft.EntityFrameworkCore;
using OshimaBot.Data;
using OshimaBot.Data.Entities;
namespace OshimaBot.Services;
/// <summary>
/// 事件幂等:以 processed_events 的唯一主键 (EventId, MsgSeq) 判定首次到达,
/// 替代 S1 之前 Webhook 里进程内的 IMemoryCache(重启即失效、多实例不共享)。
/// </summary>
public interface IEventDeduplicationStore
{
Task<bool> 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<int> PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default);
}
public sealed class EventDeduplicationStore(
IServiceScopeFactory scopeFactory,
ILogger<EventDeduplicationStore> logger) : IEventDeduplicationStore
{
public async Task<bool> TryMarkAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default)
{
using IServiceScope scope = scopeFactory.CreateScope();
BotDbContext db = scope.ServiceProvider.GetRequiredService<BotDbContext>();
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<BotDbContext>();
DateTime now = DateTime.UtcNow;
await db.ProcessedEvents
.Where(x => x.EventId == eventId && x.MsgSeq == msgSeq)
.ExecuteUpdateAsync(setters => setters.SetProperty(x => x.ProcessedAtUtc, now), cancellationToken);
}
/// <summary>处理失败时撤销占位,允许平台重推后重新处理。</summary>
public async Task ForgetAsync(string eventId, int msgSeq = 0, CancellationToken cancellationToken = default)
{
using IServiceScope scope = scopeFactory.CreateScope();
BotDbContext db = scope.ServiceProvider.GetRequiredService<BotDbContext>();
await db.ProcessedEvents
.Where(x => x.EventId == eventId && x.MsgSeq == msgSeq)
.ExecuteDeleteAsync(cancellationToken);
}
public async Task<int> PurgeAsync(TimeSpan retention, CancellationToken cancellationToken = default)
{
using IServiceScope scope = scopeFactory.CreateScope();
BotDbContext db = scope.ServiceProvider.GetRequiredService<BotDbContext>();
DateTime cutoff = DateTime.UtcNow - retention;
return await db.ProcessedEvents
.Where(x => x.ReceivedAtUtc < cutoff)
.ExecuteDeleteAsync(cancellationToken);
}
}
/// <summary>定期清理幂等表,避免 processed_events 无限增长。</summary>
public sealed class ProcessedEventCleanupService(
IEventDeduplicationStore store,
Microsoft.Extensions.Options.IOptions<Models.DatabaseOptions> options,
ILogger<ProcessedEventCleanupService> 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, "清理过期事件幂等记录失败。");
}
}
}
}
+46 -17
View File
@@ -9,14 +9,17 @@ namespace OshimaBot.Services
{ {
public interface IQQBotService 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 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); 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); 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); Task SendGroupMarkdownAsync(string groupOpenid, MarkdownMessage markdown, KeyboardMessage? keyboard = null, string? msgId = null, int? msgSeq = null, CancellationToken cancellationToken = default, string? eventId = null);
Task<UploadMediaResult> UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default); Task<UploadMediaResult> UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default);
Task<UploadMediaResult> UploadGroupMediaAsync(string groupOpenid, int fileType, string url, CancellationToken cancellationToken = default); Task<UploadMediaResult> UploadGroupMediaAsync(string groupOpenid, int fileType, string url, CancellationToken cancellationToken = default);
Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default); Task<string> GetAccessTokenAsync(CancellationToken cancellationToken = default);
Task<string> RefreshTokenAsync(CancellationToken cancellationToken = default); Task<string> RefreshTokenAsync(CancellationToken cancellationToken = default);
/// <summary>回应 INTERACTION_CREATE,否则客户端会一直 loading。同一 interaction_id 只能回一次。</summary>
Task<bool> AckInteractionAsync(string interactionId, int code = 0, CancellationToken cancellationToken = default);
} }
public sealed class QQBotService( public sealed class QQBotService(
@@ -33,30 +36,30 @@ namespace OshimaBot.Services
private readonly HttpClient _httpClient = httpClientFactory.CreateClient(HttpClientName); private readonly HttpClient _httpClient = httpClientFactory.CreateClient(HttpClientName);
private readonly SemaphoreSlim _tokenLock = new(1, 1); 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) 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, cancellationToken); => 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) 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, cancellationToken); => 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) 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, cancellationToken); => 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) 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, cancellationToken); => 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<string, object?> body = new() Dictionary<string, object?> body = new()
{ {
["content"] = content, ["content"] = content,
["msg_type"] = msgType ["msg_type"] = msgType
}; };
AddMessageContext(body, media, msgId, msgSeq); AddMessageContext(body, media, msgId, msgSeq, eventId);
await PostAsync(path, body, cancellationToken); 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<string, object?> body = new() Dictionary<string, object?> body = new()
{ {
@@ -64,17 +67,43 @@ namespace OshimaBot.Services
["markdown"] = markdown ["markdown"] = markdown
}; };
if (keyboard is not null) body["keyboard"] = keyboard; if (keyboard is not null) body["keyboard"] = keyboard;
AddMessageContext(body, null, msgId, msgSeq); AddMessageContext(body, null, msgId, msgSeq, eventId);
await PostAsync(path, body, cancellationToken); await PostAsync(path, body, cancellationToken);
} }
private static void AddMessageContext(Dictionary<string, object?> body, object? media, string? msgId, int? msgSeq) /// <summary>event_id 与 msg_id 二选一:互动事件用 event_id,普通消息事件用 msg_id + 递增 msg_seq。</summary>
private static void AddMessageContext(Dictionary<string, object?> body, object? media, string? msgId, int? msgSeq, string? eventId)
{ {
if (media is not null) body["media"] = media; 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 (!string.IsNullOrWhiteSpace(msgId)) body["msg_id"] = msgId;
if (msgSeq.HasValue) body["msg_seq"] = msgSeq.Value; if (msgSeq.HasValue) body["msg_seq"] = msgSeq.Value;
} }
public async Task<bool> 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<UploadMediaResult> UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default) public Task<UploadMediaResult> UploadC2CMediaAsync(string openid, int fileType, string url, CancellationToken cancellationToken = default)
=> UploadMediaAsync($"/v2/users/{Uri.EscapeDataString(openid)}/files", fileType, url, cancellationToken); => UploadMediaAsync($"/v2/users/{Uri.EscapeDataString(openid)}/files", fileType, url, cancellationToken);
+159
View File
@@ -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);
}
/// <summary>消息投递抽象:Webhook 与 WebSocket 共用同一个出口,便于单测替换。</summary>
public interface IBotMessageSink
{
ValueTask QueueAsync(IBotMessage message, CancellationToken cancellationToken = default);
}
public interface IQQEventDispatcher
{
Task<EventDispatchResult> DispatchAsync(Payload payload, QQEventSource source, CancellationToken cancellationToken = default);
}
/// <summary>
/// 统一事件管线(S3):去重 → 互动回执 → 事件转 IBotMessage → 投递 BotMessageQueue。
/// Webhook 与 WebSocket 两条入口都走这里,指令层(RainBotService)零改动。
/// </summary>
public sealed class QQEventDispatcher(
IEventDeduplicationStore deduplication,
IInteractionAckService interactionAck,
IBotMessageSink sink,
ILogger<QQEventDispatcher> logger) : IQQEventDispatcher
{
private const string InteractionPrefix = "INTERACTION_CREATE:";
private static readonly HashSet<string> HandledEvents = new(StringComparer.Ordinal)
{
"C2C_MESSAGE_CREATE",
"GROUP_AT_MESSAGE_CREATE",
"INTERACTION_CREATE"
};
public async Task<EventDispatchResult> 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<C2CMessage>(),
"GROUP_AT_MESSAGE_CREATE" => payload.Data.Deserialize<GroupAtMessage>(),
"INTERACTION_CREATE" => payload.Data.Deserialize<InteractionEvent>(),
_ => null
};
}
catch (JsonException)
{
return null;
}
}
}
+2 -1
View File
@@ -22,7 +22,8 @@
}, },
"Database": { "Database": {
"ConnectionString": "Data Source=oshimobot.db;Default Timeout=30", "ConnectionString": "Data Source=oshimobot.db;Default Timeout=30",
"ApplyMigrationsOnStartup": true "ApplyMigrationsOnStartup": true,
"ProcessedEventRetentionHours": 24
}, },
"Server": { "Server": {
"ApiKey": "", "ApiKey": "",