Skip to content

Commit f2184ef

Browse files
committed
feat(飞书Bot): Enhance message handling with rich message support and improve token refresh logic
1 parent b831ea6 commit f2184ef

4 files changed

Lines changed: 43 additions & 12 deletions

File tree

‎src/KoalaWiki/Services/Feishu/Feishu/FeiShuClient.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public async ValueTask SendMessages(SendMessageInput input, string receiveIdType
1919
var result =
2020
await client.PostAsJsonAsync(
2121
"https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=" + receiveIdType,
22-
input);
22+
input,JsonSerializerOptions.Web);
2323

2424
var response = await result.Content.ReadFromJsonAsync<FeiShuResult>();
25-
25+
2626
if (response.code == 0)
2727
{
2828
return;

‎src/KoalaWiki/Services/Feishu/Feishu/FeishuStore.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public async Task StartAsync(CancellationToken cancellationToken)
9494
{
9595
_logger.LogInformation("飞书token定时刷新服务启动");
9696

97+
if (string.IsNullOrEmpty(FeishuOptions.AppId))
98+
{
99+
return;
100+
}
101+
97102
// 立即刷新一次token
98103
await RefreshTokenInternalAsync();
99104

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
using System.Text.Json.Serialization;
22

3-
namespace ImageAgent.Feishu;
3+
namespace KoalaWiki.Services.Feishu.Feishu;
44

55
public class SendMessageInput(string content, string msgType, string receiveId)
66
{
7-
[JsonPropertyName("content")]
8-
public string content { get; set; } = content;
7+
[JsonPropertyName("content")] public string content { get; set; } = content;
98

10-
[JsonPropertyName("msg_type")]
11-
public string msg_type { get; set; } = msgType;
9+
[JsonPropertyName("msg_type")] public string msg_type { get; set; } = msgType;
1210

13-
[JsonPropertyName("receive_id")]
14-
public string receive_id { get; set; } = receiveId;
11+
[JsonPropertyName("receive_id")] public string receive_id { get; set; } = receiveId;
1512
}

‎src/KoalaWiki/Services/Feishu/FeishuBotService.cs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ await context.Response.WriteAsJsonAsync(new
201201

202202
logger.LogInformation("启动异步任务处理群聊消息");
203203
await Task.Factory.StartNew(async () =>
204-
await Handler(input.Event.message.content, input.Event.message.message_id, chatId, warehouse,
204+
await Handler(input.Event.message.message_id, input.Event.message.content, chatId, warehouse,
205205
document, "chat_id"));
206206
return;
207207
}
@@ -229,7 +229,7 @@ private async Task Handler(string messageId, string content, string sessionId,
229229
logger.LogInformation("消息解析成功,IsText: {IsText}", userInput?.IsText);
230230

231231
var history = new ChatHistory();
232-
232+
233233
// 解析仓库的目录结构
234234
var path = document.GitPath;
235235

@@ -376,7 +376,11 @@ private async Task Handler(string messageId, string content, string sessionId,
376376
sb = new StringBuilder(Regex.Replace(sb.ToString(), @"<thinking>.*?<\/thinking>", "",
377377
RegexOptions.IgnoreCase | RegexOptions.Singleline));
378378

379-
await SendMessages(sessionId, sb.ToString(), type);
379+
// 解析sb的#标题
380+
var titleMatch = Regex.Match(sb.ToString(), @"#\s*(.+)");
381+
var title = titleMatch.Success ? titleMatch.Groups[1].Value.Trim() : "OpenDeepWiki 回复";
382+
383+
await SendRichMessage(sessionId, title, sb.ToString(), type);
380384
logger.LogInformation("已发送回复消息给用户: {SessionId}", sessionId);
381385
}
382386
catch (Exception exception)
@@ -397,6 +401,31 @@ await feiShuClient.SendMessages(
397401
sessionId), receiveIdType);
398402
}
399403

404+
public async Task SendRichMessage(string sessionId, string title, string text,
405+
string receiveIdType = "open_id")
406+
{
407+
await feiShuClient.SendMessages(
408+
new SendMessageInput(JsonSerializer.Serialize(new
409+
{
410+
zh_cn = new
411+
{
412+
title,
413+
content = new object[]
414+
{
415+
new object[]
416+
{
417+
new
418+
{
419+
tag = "text",
420+
text,
421+
}
422+
}
423+
}
424+
}
425+
}, JsonSerializerOptions.Web), "post",
426+
sessionId), receiveIdType);
427+
}
428+
400429
public async Task SendImageMessages(string sessionId, byte[] bytes, string receiveIdType = "open_id")
401430
{
402431
await feiShuClient.SendImageAsync(bytes, sessionId, receiveIdType);

0 commit comments

Comments
 (0)