-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
442 lines (408 loc) · 12.6 KB
/
Program.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using Makabaka.Configurations;
using Makabaka.Models.API.Responses;
using Makabaka.Models.EventArgs;
using Makabaka.Models.Messages;
using Makabaka.Services;
using Newtonsoft.Json;
using Serilog;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace Makabaka.Test
{
internal partial class Program
{
private const string ConfigPath = "config.json";
static async Task Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console()
.CreateLogger(); // 配置日志
// 加载配置
ForwardWebSocketServiceConfig? config = null;
if (File.Exists(ConfigPath))
{
config = JsonConvert.DeserializeObject<ForwardWebSocketServiceConfig>(File.ReadAllText(ConfigPath));
}
config ??= new();
File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(config, Formatting.Indented));
// 初始化服务
var service = ServiceFactory.CreateForwardWebSocketService(config);
service.OnGroupMessage += OnGroupMessage;
service.OnPrivateMessage += OnPrivateMessage;
service.OnGroupRequest += OnGroupRequest;
service.OnGroupFileUpload += OnGroupFileUpload;
// 启动服务
await service.StartAsync();
Console.ReadLine();
await service.StopAsync();
}
private static async void OnGroupFileUpload(object? sender, GroupFileUploadEventArgs e)
{
await e.ReplyAsync(new TextSegment($"接收到文件事件:{e.File.Name}"));
}
private static async void OnGroupRequest(object? sender, GroupRequestEventArgs e)
{
await e.AcceptAsync(); // 有群请求,直接同意
}
private static async void OnPrivateMessage(object? sender, PrivateMessageEventArgs e)
{
if (e.Message == "测试")
{
await e.ReplyAsync(new TextSegment("耶!"));
}
else if (e.Message == "回复测试")
{
await e.ReplyAsync([new ReplySegment(e.MessageId), new TextSegment("回复测试!")]);
}
else if (e.Message == "获取好友历史消息记录测试")
{
GetFriendMsgHistoryRes res = await e.Context.GetFriendMsgHistoryAsync(e.UserId, e.MessageId, 5);
var sb = new StringBuilder();
foreach (var message in res.Messages)
{
sb.Append('[');
sb.Append(message.Sender.UserId);
sb.Append(']');
sb.Append(message.Sender.NickName);
sb.Append(": ");
sb.Append(message.Message);
sb.AppendLine();
}
await e.ReplyAsync(new TextSegment(sb.ToString()));
}
else if (e.Message == "私聊发送文件测试")
{
await e.Context.UploadPrivateFileAsync(e.UserId, "Fleck.dll", "Fleck.dll");
}
else if (e.Message == "好友戳一戳测试")
{
await e.Context.FriendPokeAsync(e.UserId);
}
}
private static async void OnGroupMessage(object? sender, GroupMessageEventArgs e)
{
if (e.Message == "测试")
{
await e.ReplyAsync(new TextSegment("耶!"));
}
else if (e.Message == "私聊测试")
{
await e.Context.SendPrivateMessageAsync(e.UserId, new TextSegment("私聊测试!"));
}
else if (e.Message == "表情测试")
{
await e.ReplyAsync(new FaceSegment(0));
}
else if (e.Message == "At测试")
{
await e.ReplyAsync([new AtSegment(e.Sender.UserId), new TextSegment(" 测试!")]);
}
else if (e.Message == "图片测试")
{
//await e.Reply(new ImageSegment("base64://iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAKElEQVQ4EWPk5+RmIBcwkasRpG9UM4mhNxpgowFGMARGEwnBIEJVAAAdBgBNAZf+QAAAAABJRU5ErkJggg=="));
await e.ReplyAsync(ImageSegment.FromLocalFile("test.png"));
}
else if (e.Message == "视频测试")
{
// 注意:这里的uri路径是相对于Lagrange.Onebot程序所在的路径,不是Makabaka程序所在的路径
// 也就是说,你要把test.mp4放到Lagrange.Onebot程序同目录下,才能发
await e.ReplyAsync(new VideoSegment("/home/Lagrange.OneBot/test.mp4"));
}
else if (e.Message == "转发测试")
{
string resid = await e.Context.SendForwardMessageAsync([new NodeSegment(e.SelfId, string.Empty, new TextSegment("转发测试!"))]);
await e.ReplyAsync(new ForwardSegment(resid));
}
else if (e.Message == "回复测试")
{
await e.ReplyAsync([new ReplySegment(e.MessageId), new TextSegment("回复测试!")]);
}
else if (e.Message == "MarkDown测试")
{
var md = new MarkDownSegment(JsonConvert.SerializeObject(new
{
#region 内容太长,折叠
content =
$@"# 一号标题
## 二号标题
正文
**加粗**
__下划线加粗__
_斜体_
*星号斜体*
***加粗斜体***
~~删除线~~
欢迎来到:[🔗腾讯网](https://www.qq.com)
文档可以访问<https://doc.qq.com>
![text #208px #320px](https://resource5-1255303497.cos.ap-guangzhou.myqcloud.com/abcmouse_word_watch/markdown/building.png)
# 有序列表
1. 新人降落桃源岛的欢迎仪式
2. 阳光准则助力建设有温度的频道
3. 岛民分享吹水纳凉
# 无序列表
- 新人降落桃源岛的欢迎仪式
- 阳光准则助力建设有温度的频道
- 岛民分享吹水纳凉
# 有序列表标题
1. 嵌套一层
- 列表前是普通文本,则需要在列表前用空行隔开,否则无法识别
- 如果是段落标签比如标题,则无需用空行隔开
2. 嵌套二层
1. 我是有序列表,二级列表前面需要空4个空格
2. 无序列表和有序列表可以相互嵌套,但是不建议无限制嵌套。
> 青青子衿,悠悠我心,但为君故,沉吟至今
> 四月维夏,六月徂暑。先祖匪人,胡宁忍予
> 秋日凄凄,百卉具腓。乱离瘼矣,爰其适归?
诗经《小雅》
这是段落1
***
这是段落2
第一行
第二行
\u200B
\u200B
第三行
[/回车指令](mqqapi://aio/inlinecmd?command={HttpUtility.UrlEncode("/回车指令")}&reply=false&enter=true)"
#endregion
}));
string resid = await e.Context.SendForwardMessageAsync([new NodeSegment(e.SelfId, string.Empty, md)]);
await e.ReplyAsync(new LongMsgSegment(resid));
}
else if (e.Message == "MarkDown测试2")
{
var md = new MarkDownSegment(JsonConvert.SerializeObject(new
{
#region 内容太长,折叠
content =
$@"# 一号标题
## 二号标题
正文
**加粗**
__下划线加粗__
_斜体_
*星号斜体*
***加粗斜体***
~~删除线~~
欢迎来到:[🔗腾讯网](https://www.qq.com)
文档可以访问<https://doc.qq.com>
![text #208px #320px](https://resource5-1255303497.cos.ap-guangzhou.myqcloud.com/abcmouse_word_watch/markdown/building.png)
# 有序列表
1. 新人降落桃源岛的欢迎仪式
2. 阳光准则助力建设有温度的频道
3. 岛民分享吹水纳凉
# 无序列表
- 新人降落桃源岛的欢迎仪式
- 阳光准则助力建设有温度的频道
- 岛民分享吹水纳凉
# 有序列表标题
1. 嵌套一层
- 列表前是普通文本,则需要在列表前用空行隔开,否则无法识别
- 如果是段落标签比如标题,则无需用空行隔开
2. 嵌套二层
1. 我是有序列表,二级列表前面需要空4个空格
2. 无序列表和有序列表可以相互嵌套,但是不建议无限制嵌套。
> 青青子衿,悠悠我心,但为君故,沉吟至今
> 四月维夏,六月徂暑。先祖匪人,胡宁忍予
> 秋日凄凄,百卉具腓。乱离瘼矣,爰其适归?
诗经《小雅》
这是段落1
***
这是段落2
第一行
第二行
\u200B
\u200B
第三行
[/回车指令](mqqapi://aio/inlinecmd?command={HttpUtility.UrlEncode("/回车指令")}&reply=false&enter=true)"
#endregion
}));
await e.ReplyAsync(md);
}
else if (e.Message == "按钮测试")
{
var md = new MarkDownSegment(JsonConvert.SerializeObject(new
{
content = "按钮测试!",
}));
var kb = new KeyboardSegment(new KeyboardData
{
#region 内容太长,折叠
Content = new KeyboardContent
{
Rows =
[
new KeyboardRow
{
Buttons =
[
new KeyboardButton
{
Id = "1",
RenderData = new KeyboardRenderData
{
Label = "⬅️上一页",
VisitedLabel = "⬅️上一页",
},
Action = new KeyboardAction
{
Type = 1,
Permission = new KeyboardActionPermission
{
Type = 2,
},
Data = "data",
UnsupportTips = "兼容文本",
},
},
new KeyboardButton
{
Id = "2",
RenderData = new KeyboardRenderData
{
Label = "➡️下一页",
VisitedLabel = "➡️下一页",
},
Action = new KeyboardAction
{
Type = 1,
Permission = new KeyboardActionPermission
{
Type = 2,
},
Data = "data",
UnsupportTips = "兼容文本",
},
},
]
},
new KeyboardRow
{
Buttons =
[
new KeyboardButton
{
Id = "3",
RenderData = new KeyboardRenderData
{
Label = "📅 打卡(5)",
VisitedLabel = "📅 打卡(5)",
},
Action = new KeyboardAction
{
Type = 1,
Permission = new KeyboardActionPermission
{
Type = 2,
},
Data = "data",
UnsupportTips = "兼容文本",
},
},
]
},
]
}
#endregion
});
string resid = await e.Context.SendForwardMessageAsync([new NodeSegment(e.SelfId, string.Empty, [md, kb])]);
await e.ReplyAsync(new LongMsgSegment(resid));
}
else if (e.Message == "获取收藏表情测试")
{
List<string> faces = await e.Context.FetchCustomFaceAsync();
await e.ReplyAsync(new TextSegment(string.Join("\n", faces)));
}
else if (e.Message == "获取群组历史消息记录测试")
{
GetGroupMsgHistoryRes res = await e.Context.GetGroupMsgHistoryAsync(e.GroupId, e.MessageId, 5);
var sb = new StringBuilder();
foreach (var message in res.Messages)
{
sb.Append('[');
sb.Append(message.UserId);
sb.Append("]: ");
sb.Append(message.Message);
sb.AppendLine();
}
await e.ReplyAsync(new TextSegment(sb.ToString()));
}
else if (e.Message == "上传群文件测试")
{
await e.Context.UploadGroupFileAsync(e.GroupId, "Fleck.dll", "Fleck.dll");
}
else if (e.Message == "获取群根目录文件列表测试")
{
GetGroupRootFilesRes res = await e.Context.GetGroupRootFilesAsync(e.GroupId);
var sb = new StringBuilder();
sb.AppendLine("文件夹列表:");
foreach (var folder in res.Folders)
{
sb.Append('[');
sb.Append(folder.FolderId);
sb.Append(']');
sb.AppendLine(folder.FolderName);
}
sb.AppendLine("文件列表:");
foreach (var file in res.Files)
{
sb.Append('[');
sb.Append(file.FileId);
sb.Append(']');
sb.Append('[');
sb.Append(file.Busid);
sb.Append(']');
sb.AppendLine(file.FileName);
}
await e.ReplyAsync(new TextSegment(sb.ToString()));
}
else if (e.Message == "群组戳一戳测试")
{
await e.Context.GroupPokeAsync(e.GroupId, e.UserId);
}
var match = GetGroupFilesByFolderRegex().Match(e.Message);
if (match.Success)
{
var folderId = match.Groups["folder"].Value;
GetGroupFilesByFolderRes res = await e.Context.GetGroupFilesByFolderAsync(e.GroupId, folderId);
var sb = new StringBuilder();
sb.Append("子目录");
sb.Append(folderId);
sb.AppendLine("中");
sb.AppendLine("文件夹列表:");
foreach (var folder in res.Folders)
{
sb.Append('[');
sb.Append(folder.FolderId);
sb.Append(']');
sb.AppendLine(folder.FolderName);
}
sb.AppendLine("文件列表:");
foreach (var file in res.Files)
{
sb.Append('[');
sb.Append(file.FileId);
sb.Append(']');
sb.Append('[');
sb.Append(file.Busid);
sb.Append(']');
sb.AppendLine(file.FileName);
}
await e.ReplyAsync(new TextSegment(sb.ToString()));
}
match = GetGroupFileUrlRegex().Match(e.Message);
if (match.Success)
{
var fileId = match.Groups["file"].Value;
var busId = int.Parse(match.Groups["busid"].Value);
GetGroupFileUrlRes res = await e.Context.GetGroupFileUrlAsync(e.GroupId, fileId, busId);
await e.ReplyAsync(new TextSegment(res.Url));
}
}
[GeneratedRegex(@"获取群子目录文件列表测试 (?<folder>\S+)", RegexOptions.Compiled)]
private static partial Regex GetGroupFilesByFolderRegex();
[GeneratedRegex(@"获取群文件资源链接测试 (?<file>\S+) (?<busid>[-0-9]+)", RegexOptions.Compiled)]
private static partial Regex GetGroupFileUrlRegex();
}
}