forked from Deleeete/klbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatQYKModule.cs
More file actions
43 lines (39 loc) · 1.67 KB
/
Copy pathChatQYKModule.cs
File metadata and controls
43 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using klbotlib.Modules.ModuleUtils;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace klbotlib.Modules;
///聊天bot模块
public partial class ChatQYKModule : SingleTypeModule<MessagePackage>
{
private const string Url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=";
private static readonly HttpHelper s_helper = new();
/// <inheritdoc/>
public sealed override bool IsTransparent => false;
/// <inheritdoc/>
public sealed override bool UseSignature => false;
/// <inheritdoc/>
public sealed override bool IsAsync => false;
/// <inheritdoc/>
public sealed override string FriendlyName => "聊天模块";
/// <inheritdoc/>
public sealed override async Task<Message?> Processor(MessageContext context, MessagePackage msg)
{
if (msg.Count != 2 || !msg.ContainsTargetId(HostBot.SelfId))
return null;
string jreply = await s_helper.GetStringAsync(Url + msg.AsPlain());
ChatterBotReply? reply = JsonSerializer.Deserialize<ChatterBotReply>(jreply);
return reply?.FormattedContent();
}
private partial record ChatterBotReply
{
public int result = 0;
public string content = string.Empty;
public static Regex trashPat = TrashPattern();
public static Regex facePat = FacePattern();
public string FormattedContent() => trashPat.Replace(facePat.Replace(content.Replace("{br}", "\r\n"), ""), "");
[GeneratedRegex(@"{r\+}", RegexOptions.Compiled)]
private static partial Regex TrashPattern();
[GeneratedRegex(@"{face:[\d]+}", RegexOptions.Compiled)]
private static partial Regex FacePattern();
}
}