-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinbound.go
More file actions
913 lines (848 loc) · 25.7 KB
/
Copy pathinbound.go
File metadata and controls
913 lines (848 loc) · 25.7 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/gin-gonic/gin"
)
const maxInboundBodyBytes = 1024 * 1024
type inboundPayload struct {
Subject string
From string
Body string
}
type collectedTextField struct {
Key string
Label string
Value string
}
func setupPublicAPI(r *gin.Engine) {
r.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
r.Any("/hook/:secret", handleInboundHook)
r.Any("/hook/:secret/*extra", handleInboundHook)
r.Any("/webhook/:secret", handleInboundHook)
r.Any("/webhook/:secret/*extra", handleInboundHook)
}
func handleInboundHook(c *gin.Context) {
project, err := getInboundProjectBySecret(c.Param("secret"))
if err != nil {
addLog(fmt.Sprintf("接收入口查询失败: %v", err), "error")
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
return
}
if project == nil || !project.Enabled {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
bodyBytes, err := io.ReadAll(http.MaxBytesReader(c.Writer, c.Request.Body, maxInboundBodyBytes))
if err != nil {
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "request body too large"})
return
}
payload := parseInboundPayload(c.Request, bodyBytes)
now := time.Now()
msg := Message{
ID: fmt.Sprintf("msg_%d", now.UnixNano()),
SourceEmail: project.Name,
AccountID: project.ID,
Subject: payload.Subject,
From: payload.From,
To: "all2webhook",
Date: now,
Body: payload.Body,
BodyHTML: "",
Status: "pending",
CreatedAt: now,
}
if err := saveMessage(&msg); err != nil {
addLog(fmt.Sprintf("保存外部通知失败 [%s]: %v", project.Name, err), "error")
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to save message"})
return
}
addLog(fmt.Sprintf("已接收外部通知 [%s]: %s", project.Name, displaySubject(msg.Subject)), "success")
c.JSON(http.StatusAccepted, gin.H{"success": true, "message_id": msg.ID})
}
func parseInboundPayload(req *http.Request, body []byte) inboundPayload {
event := strings.TrimSpace(req.Header.Get("X-GitHub-Event"))
contentType := strings.ToLower(req.Header.Get("Content-Type"))
from := firstNonEmpty(req.Header.Get("X-Sender"), req.Header.Get("X-From"), req.Header.Get("X-Source"), req.Header.Get("X-GitHub-Delivery"), req.RemoteAddr, "external")
if parsed := parseBarkGETPayload(req); parsed != nil {
return *parsed
}
if strings.Contains(contentType, "application/json") || json.Valid(body) {
if payload, ok := parseJSONInboundPayload(body, event, from); ok {
return payload
}
}
if strings.Contains(contentType, "application/x-www-form-urlencoded") {
values, err := url.ParseQuery(string(body))
if err == nil {
if rawPayload := strings.TrimSpace(values.Get("payload")); rawPayload != "" {
if payload, ok := parseJSONInboundPayload([]byte(rawPayload), event, from); ok {
return payload
}
}
subject := firstNonEmpty(values.Get("title"), values.Get("subject"), values.Get("summary"), values.Get("text"))
text := firstNonEmpty(values.Get("message"), values.Get("body"), values.Get("desp"), values.Get("content"), values.Get("text"))
if text != "" {
return inboundPayload{
Subject: firstNonEmpty(subject, firstLine(text), "表单通知"),
From: firstNonEmpty(values.Get("sender"), values.Get("sender_name"), values.Get("username"), values.Get("from"), values.Get("source"), from),
Body: text,
}
}
}
}
bodyText := strings.TrimSpace(string(body))
if bodyText == "" {
bodyText = "(空请求体)"
}
return inboundPayload{
Subject: firstNonEmpty(req.Header.Get("X-Title"), req.Header.Get("X-Subject"), "外部通知"),
From: from,
Body: buildRawRequestBody(req, bodyText),
}
}
func parseJSONInboundPayload(body []byte, event string, from string) (inboundPayload, bool) {
var data interface{}
if err := json.Unmarshal(body, &data); err != nil {
return inboundPayload{}, false
}
if parsed := parseKnownJSONWebhook(data, event); parsed != nil {
parsed.From = firstNonEmpty(parsed.From, from)
return *parsed, true
}
subject, _ := extractJSONSummary(data)
if event != "" {
subject = firstNonEmpty(formatGitHubSubject(event, data), subject)
from = "github"
}
return inboundPayload{
Subject: firstNonEmpty(subject, "外部通知"),
From: from,
Body: formatJSONBody(data, body),
}, true
}
func parseBarkGETPayload(req *http.Request) *inboundPayload {
if req.Method != http.MethodGet {
return nil
}
query := req.URL.Query()
subject := firstNonEmpty(query.Get("title"), query.Get("subject"))
body := firstNonEmpty(query.Get("body"), query.Get("message"), query.Get("text"), query.Get("content"), query.Get("desp"))
group := strings.TrimSpace(query.Get("group"))
segments := extractBarkPathSegments(req.URL.EscapedPath())
switch {
case len(segments) >= 3:
group = firstNonEmpty(group, segments[0])
subject = firstNonEmpty(subject, segments[1])
body = firstNonEmpty(body, strings.Join(segments[2:], "/"))
case len(segments) == 2:
subject = firstNonEmpty(subject, segments[0])
body = firstNonEmpty(body, segments[1])
case len(segments) == 1:
body = firstNonEmpty(body, segments[0])
}
if subject == "" && body == "" {
return nil
}
lines := []string{body}
for _, key := range []string{"subtitle", "url", "sound", "icon", "level", "badge", "copy", "autoCopy"} {
if value := strings.TrimSpace(query.Get(key)); value != "" {
lines = append(lines, fmt.Sprintf("%s:%s", key, value))
}
}
if group != "" {
lines = append(lines, fmt.Sprintf("group:%s", group))
}
return &inboundPayload{
Subject: firstNonEmpty(subject, firstLine(body), "Bark 通知"),
From: firstNonEmpty(query.Get("source"), query.Get("from"), "bark"),
Body: strings.Join(nonEmptyLines(lines), "\n\n"),
}
}
func extractBarkPathSegments(escapedPath string) []string {
path := strings.Trim(escapedPath, "/")
if path == "" {
return nil
}
parts := strings.Split(path, "/")
if len(parts) <= 2 {
return nil
}
if parts[0] != "hook" && parts[0] != "webhook" {
return nil
}
segments := make([]string, 0, len(parts)-2)
for _, part := range parts[2:] {
if part == "" {
continue
}
text, err := url.PathUnescape(part)
if err != nil {
text = part
}
text = strings.TrimSpace(text)
if text != "" {
segments = append(segments, text)
}
}
return segments
}
func parseKnownJSONWebhook(data interface{}, event string) *inboundPayload {
obj, ok := data.(map[string]interface{})
if !ok {
return nil
}
if payload := parseDiscordWebhook(obj); payload != nil {
return payload
}
if payload := parseFeishuWebhook(obj); payload != nil {
return payload
}
if payload := parseDingTalkWebhook(obj); payload != nil {
return payload
}
if payload := parseWeComWebhook(obj); payload != nil {
return payload
}
// 部分通知服务会沿用 X-GitHub-Event 传递自定义事件名,但正文实际是
// 飞书、钉钉等消息格式。仅在没有识别出明确的平台正文结构时,才按
// GitHub Webhook 解析,避免事件头覆盖正文的真实格式。
if event != "" {
return &inboundPayload{
Subject: firstNonEmpty(formatGitHubSubject(event, data), "GitHub 通知"),
From: firstNonEmpty(extractGitHubSender(obj), "github"),
Body: formatGitHubBody(event, obj),
}
}
if payload := parseSimpleWebhook(obj); payload != nil {
return payload
}
if payload := parseGenericNestedWebhook(obj); payload != nil {
return payload
}
return nil
}
func parseDiscordWebhook(obj map[string]interface{}) *inboundPayload {
content := firstJSONText(obj, "content")
embeds := jsonArray(obj["embeds"])
if content == "" && len(embeds) == 0 {
return nil
}
lines := []string{}
if content != "" {
lines = append(lines, content)
}
subject := firstNonEmpty(firstLine(content), "Discord 通知")
for _, embed := range embeds {
title := firstJSONText(embed, "title")
description := firstJSONText(embed, "description")
if subject == "Discord 通知" && title != "" {
subject = title
}
appendSection(&lines, title, description)
if urlText := firstJSONText(embed, "url"); urlText != "" {
lines = append(lines, fmt.Sprintf("链接:%s", urlText))
}
for _, field := range jsonArray(embed["fields"]) {
name := firstJSONText(field, "name")
value := firstJSONText(field, "value")
appendSection(&lines, name, value)
}
if footer := nestedJSONText(embed, "footer", "text"); footer != "" {
lines = append(lines, fmt.Sprintf("备注:%s", footer))
}
}
return &inboundPayload{
Subject: subject,
From: firstNonEmpty(firstJSONText(obj, "username"), "discord"),
Body: strings.Join(nonEmptyLines(lines), "\n\n"),
}
}
func parseFeishuWebhook(obj map[string]interface{}) *inboundPayload {
msgType := firstJSONText(obj, "msg_type")
if msgType == "" {
return nil
}
content := jsonObject(obj["content"])
lines := []string{}
subject := "飞书通知"
switch msgType {
case "text":
text := firstJSONText(content, "text")
subject = firstNonEmpty(firstLine(text), subject)
lines = append(lines, text)
case "post":
post := jsonObject(content["post"])
zhCN := jsonObject(post["zh_cn"])
subject = firstNonEmpty(firstJSONText(zhCN, "title"), subject)
lines = append(lines, extractFeishuPostContent(zhCN)...)
case "interactive":
card := jsonObject(obj["card"])
header := jsonObject(card["header"])
titleObj := jsonObject(header["title"])
subject = firstNonEmpty(firstJSONText(titleObj, "content"), subject)
lines = append(lines, extractFeishuCardElements(jsonArray(card["elements"]))...)
default:
text := firstJSONText(content, "text", "content")
subject = firstNonEmpty(firstLine(text), subject)
lines = append(lines, text)
}
if len(nonEmptyLines(lines)) == 0 {
return nil
}
return &inboundPayload{Subject: subject, From: firstNonEmpty(
firstJSONText(obj, "sender_name", "username", "from", "source"),
nestedJSONText(obj, "sender", "name"),
nestedJSONText(obj, "sender", "username"),
"feishu",
), Body: strings.Join(nonEmptyLines(lines), "\n\n")}
}
func parseDingTalkWebhook(obj map[string]interface{}) *inboundPayload {
msgType := firstJSONText(obj, "msgtype")
if msgType == "" {
return nil
}
lines := []string{}
subject := "钉钉通知"
switch msgType {
case "text":
textObj := jsonObject(obj["text"])
text := firstJSONText(textObj, "content")
if text == "" {
return nil
}
subject = firstNonEmpty(firstLine(text), subject)
lines = append(lines, text)
case "markdown":
markdown := jsonObject(obj["markdown"])
if firstJSONText(markdown, "title", "text") == "" {
return nil
}
subject = firstNonEmpty(firstJSONText(markdown, "title"), subject)
lines = append(lines, firstJSONText(markdown, "text"))
case "link":
link := jsonObject(obj["link"])
subject = firstNonEmpty(firstJSONText(link, "title"), subject)
appendSection(&lines, firstJSONText(link, "title"), firstJSONText(link, "text"))
if messageURL := firstJSONText(link, "messageUrl"); messageURL != "" {
lines = append(lines, fmt.Sprintf("链接:%s", messageURL))
}
case "actionCard":
card := jsonObject(obj["actionCard"])
subject = firstNonEmpty(firstJSONText(card, "title"), subject)
lines = append(lines, firstJSONText(card, "text"))
default:
return nil
}
return &inboundPayload{Subject: subject, From: firstNonEmpty(
firstJSONText(obj, "sender", "senderName", "sender_name", "username", "from", "source"),
"dingtalk",
), Body: strings.Join(nonEmptyLines(lines), "\n\n")}
}
func parseWeComWebhook(obj map[string]interface{}) *inboundPayload {
msgType := firstJSONText(obj, "msgtype")
if msgType == "" {
return nil
}
lines := []string{}
subject := "企业微信通知"
switch msgType {
case "text":
textObj := jsonObject(obj["text"])
text := firstJSONText(textObj, "content")
if text == "" {
return nil
}
subject = firstNonEmpty(firstLine(text), subject)
lines = append(lines, text)
case "markdown":
markdown := jsonObject(obj["markdown"])
text := firstJSONText(markdown, "content")
if text == "" {
return nil
}
subject = firstNonEmpty(firstMarkdownHeading(text), firstLine(text), subject)
lines = append(lines, text)
case "news":
news := jsonObject(obj["news"])
for _, article := range jsonArray(news["articles"]) {
title := firstJSONText(article, "title")
description := firstJSONText(article, "description")
if subject == "企业微信通知" && title != "" {
subject = title
}
appendSection(&lines, title, description)
if urlText := firstJSONText(article, "url"); urlText != "" {
lines = append(lines, fmt.Sprintf("链接:%s", urlText))
}
}
default:
return nil
}
return &inboundPayload{Subject: subject, From: firstNonEmpty(
firstJSONText(obj, "sender", "senderName", "sender_name", "username", "from", "source"),
nestedJSONText(obj, "sender", "name"),
nestedJSONText(obj, "sender", "username"),
"wecom",
), Body: strings.Join(nonEmptyLines(lines), "\n\n")}
}
func parseSimpleWebhook(obj map[string]interface{}) *inboundPayload {
title := firstJSONText(obj, "title", "subject", "summary")
text := firstJSONText(obj, "message", "body", "desp", "description", "content", "text")
if title == "" && text == "" {
return nil
}
lines := []string{text}
for _, key := range []string{"subtitle", "url", "group", "priority", "level", "event"} {
if value := firstJSONText(obj, key); value != "" {
lines = append(lines, fmt.Sprintf("%s:%s", key, value))
}
}
return &inboundPayload{
Subject: firstNonEmpty(title, firstLine(text), "外部通知"),
From: firstNonEmpty(firstJSONText(obj, "sender", "sender_name", "username", "user_name", "author", "from", "source", "app"), "webhook"),
Body: strings.Join(nonEmptyLines(lines), "\n\n"),
}
}
func parseGenericNestedWebhook(obj map[string]interface{}) *inboundPayload {
fields := collectTextFields(obj, "")
if len(fields) == 0 {
return nil
}
subject := firstCollectedValue(fields, "title", "subject", "summary", "name", "event")
body := firstCollectedValue(fields, "message", "body", "desp", "description", "content", "text", "markdown")
lines := []string{}
if body != "" {
lines = append(lines, body)
}
for _, field := range fields {
if field.Value == "" || field.Value == subject || field.Value == body {
continue
}
if isNoisyJSONKey(field.Key) {
continue
}
lines = append(lines, fmt.Sprintf("%s:%s", field.Label, field.Value))
if len(lines) >= 10 {
break
}
}
if len(nonEmptyLines(lines)) == 0 {
return nil
}
return &inboundPayload{
Subject: firstNonEmpty(subject, firstLine(body), "外部通知"),
From: firstNonEmpty(firstCollectedValue(fields, "sender", "sender_name", "username", "user_name", "author", "from", "source", "app", "service"), "webhook"),
Body: strings.Join(nonEmptyLines(lines), "\n\n"),
}
}
func extractGitHubSender(obj map[string]interface{}) string {
return firstNonEmpty(
nestedJSONText(obj, "sender", "login"),
nestedJSONText(obj, "sender", "name"),
nestedJSONText(obj, "pusher", "name"),
nestedJSONText(jsonObject(obj["head_commit"]), "author", "name"),
)
}
func extractJSONSummary(value interface{}) (string, string) {
obj, ok := value.(map[string]interface{})
if !ok {
return "外部通知", compactJSONString(value)
}
titleKeys := []string{"title", "subject", "summary", "event", "name"}
textKeys := []string{"message", "text", "content", "body", "description", "markdown"}
title := firstJSONText(obj, titleKeys...)
text := firstJSONText(obj, textKeys...)
if gotifyTitle := firstJSONText(obj, "title"); gotifyTitle != "" {
title = gotifyTitle
}
if barkTitle := firstJSONText(obj, "title"); barkTitle != "" {
title = barkTitle
}
if feishuMsgType := firstJSONText(obj, "msg_type"); feishuMsgType != "" && title == "" {
title = "飞书通知"
}
if title == "" && text != "" {
title = firstLine(text)
}
if text == "" {
text = compactJSONString(value)
}
return firstNonEmpty(title, "外部通知"), text
}
func formatGitHubSubject(event string, data interface{}) string {
obj, ok := data.(map[string]interface{})
if !ok {
return "GitHub " + event
}
repo := nestedJSONText(obj, "repository", "full_name")
action := firstJSONText(obj, "action")
switch event {
case "push":
ref := firstJSONText(obj, "ref")
return strings.TrimSpace(fmt.Sprintf("GitHub push %s %s", repo, ref))
case "pull_request":
number := jsonNumberText(obj["number"])
title := nestedJSONText(obj, "pull_request", "title")
return strings.TrimSpace(fmt.Sprintf("GitHub PR #%s %s %s", number, action, title))
case "issues":
number := jsonNumberText(obj["number"])
title := nestedJSONText(obj, "issue", "title")
return strings.TrimSpace(fmt.Sprintf("GitHub Issue #%s %s %s", number, action, title))
default:
return strings.TrimSpace(fmt.Sprintf("GitHub %s %s %s", event, repo, action))
}
}
func formatGitHubBody(event string, obj map[string]interface{}) string {
lines := []string{}
repo := nestedJSONText(obj, "repository", "full_name")
if repo != "" {
lines = append(lines, fmt.Sprintf("仓库:%s", repo))
}
action := firstJSONText(obj, "action")
if action != "" {
lines = append(lines, fmt.Sprintf("动作:%s", action))
}
switch event {
case "push":
lines = append(lines, fmt.Sprintf("分支:%s", firstJSONText(obj, "ref")))
if pusher := nestedJSONText(obj, "pusher", "name"); pusher != "" {
lines = append(lines, fmt.Sprintf("推送者:%s", pusher))
}
for _, commit := range jsonArray(obj["commits"]) {
message := firstJSONText(commit, "message")
author := nestedJSONText(commit, "author", "name")
urlText := firstJSONText(commit, "url")
line := strings.TrimSpace(fmt.Sprintf("- %s", firstLine(message)))
if author != "" {
line += fmt.Sprintf(" (%s)", author)
}
if urlText != "" {
line += fmt.Sprintf("\n %s", urlText)
}
lines = append(lines, line)
}
case "pull_request":
pr := jsonObject(obj["pull_request"])
appendSection(&lines, nestedJSONText(pr, "user", "login"), firstJSONText(pr, "title"))
if body := firstJSONText(pr, "body"); body != "" {
lines = append(lines, body)
}
if urlText := firstJSONText(pr, "html_url"); urlText != "" {
lines = append(lines, fmt.Sprintf("链接:%s", urlText))
}
case "issues":
issue := jsonObject(obj["issue"])
appendSection(&lines, nestedJSONText(issue, "user", "login"), firstJSONText(issue, "title"))
if body := firstJSONText(issue, "body"); body != "" {
lines = append(lines, body)
}
if urlText := firstJSONText(issue, "html_url"); urlText != "" {
lines = append(lines, fmt.Sprintf("链接:%s", urlText))
}
default:
if sender := nestedJSONText(obj, "sender", "login"); sender != "" {
lines = append(lines, fmt.Sprintf("触发者:%s", sender))
}
}
if len(nonEmptyLines(lines)) == 0 {
return compactJSONString(obj)
}
return strings.Join(nonEmptyLines(lines), "\n\n")
}
func formatJSONBody(data interface{}, original []byte) string {
_, text := extractJSONSummary(data)
var out bytes.Buffer
if strings.TrimSpace(text) != "" {
out.WriteString(text)
out.WriteString("\n\n")
}
out.WriteString("```json\n")
formatted := compactJSONString(data)
if strings.TrimSpace(formatted) == "" {
formatted = string(original)
}
out.WriteString(formatted)
out.WriteString("\n```")
return out.String()
}
func extractFeishuPostContent(post map[string]interface{}) []string {
lines := []string{}
for _, row := range jsonNestedArrays(post["content"]) {
parts := []string{}
for _, item := range row {
text := firstJSONText(item, "text")
if text == "" {
text = firstJSONText(item, "href", "user_name")
}
if text != "" {
parts = append(parts, text)
}
}
if len(parts) > 0 {
lines = append(lines, strings.Join(parts, ""))
}
}
return lines
}
func extractFeishuCardElements(elements []map[string]interface{}) []string {
lines := []string{}
for _, element := range elements {
tag := firstJSONText(element, "tag")
switch tag {
case "div", "markdown":
textObj := jsonObject(element["text"])
lines = append(lines, firstJSONText(textObj, "content"))
case "note":
parts := []string{}
for _, item := range jsonArray(element["elements"]) {
if text := firstJSONText(item, "content", "text"); text != "" {
parts = append(parts, text)
}
}
if len(parts) > 0 {
lines = append(lines, strings.Join(parts, " "))
}
case "hr":
continue
default:
if text := firstJSONText(element, "content", "text"); text != "" {
lines = append(lines, text)
}
}
}
return lines
}
func buildRawRequestBody(req *http.Request, bodyText string) string {
headers := []string{}
for _, name := range []string{"User-Agent", "Content-Type", "X-GitHub-Event", "X-GitHub-Delivery"} {
if value := req.Header.Get(name); value != "" {
headers = append(headers, fmt.Sprintf("%s: %s", name, value))
}
}
sort.Strings(headers)
if len(headers) == 0 {
return bodyText
}
return strings.Join(headers, "\n") + "\n\n" + bodyText
}
func firstJSONText(obj map[string]interface{}, keys ...string) string {
for _, key := range keys {
if value, ok := obj[key]; ok {
if text := jsonValueText(value); text != "" {
return text
}
}
}
return ""
}
func nestedJSONText(obj map[string]interface{}, parent string, child string) string {
nested, ok := obj[parent].(map[string]interface{})
if !ok {
return ""
}
return jsonValueText(nested[child])
}
func jsonObject(value interface{}) map[string]interface{} {
obj, _ := value.(map[string]interface{})
if obj == nil {
return map[string]interface{}{}
}
return obj
}
func jsonArray(value interface{}) []map[string]interface{} {
values, ok := value.([]interface{})
if !ok {
return nil
}
result := make([]map[string]interface{}, 0, len(values))
for _, value := range values {
if obj, ok := value.(map[string]interface{}); ok {
result = append(result, obj)
}
}
return result
}
func jsonNestedArrays(value interface{}) [][]map[string]interface{} {
rows, ok := value.([]interface{})
if !ok {
return nil
}
result := make([][]map[string]interface{}, 0, len(rows))
for _, rowValue := range rows {
items, ok := rowValue.([]interface{})
if !ok {
continue
}
row := make([]map[string]interface{}, 0, len(items))
for _, item := range items {
if obj, ok := item.(map[string]interface{}); ok {
row = append(row, obj)
}
}
result = append(result, row)
}
return result
}
func jsonValueText(value interface{}) string {
switch v := value.(type) {
case string:
return strings.TrimSpace(v)
case float64:
return jsonNumberText(v)
case bool:
if v {
return "true"
}
return "false"
default:
return ""
}
}
func jsonNumberText(value interface{}) string {
switch v := value.(type) {
case float64:
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.0f", v), "0"), ".")
case int:
return fmt.Sprintf("%d", v)
case string:
return v
default:
return ""
}
}
func compactJSONString(value interface{}) string {
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return ""
}
return string(data)
}
func firstLine(text string) string {
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
if line != "" {
return line
}
}
return ""
}
func firstMarkdownHeading(text string) string {
for _, line := range strings.Split(text, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "#") {
return strings.TrimSpace(strings.TrimLeft(line, "#"))
}
}
return ""
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return strings.TrimSpace(value)
}
}
return ""
}
func appendSection(lines *[]string, title string, body string) {
title = strings.TrimSpace(title)
body = strings.TrimSpace(body)
switch {
case title != "" && body != "":
*lines = append(*lines, fmt.Sprintf("**%s**\n%s", title, body))
case title != "":
*lines = append(*lines, title)
case body != "":
*lines = append(*lines, body)
}
}
func nonEmptyLines(lines []string) []string {
result := make([]string, 0, len(lines))
for _, line := range lines {
line = strings.TrimSpace(line)
if line != "" {
result = append(result, line)
}
}
return result
}
func collectTextFields(value interface{}, path string) []collectedTextField {
result := []collectedTextField{}
switch v := value.(type) {
case map[string]interface{}:
for key, item := range v {
childPath := key
if path != "" {
childPath = path + "." + key
}
result = append(result, collectTextFields(item, childPath)...)
if len(result) >= 40 {
return result
}
}
case []interface{}:
for i, item := range v {
childPath := fmt.Sprintf("%s[%d]", path, i)
result = append(result, collectTextFields(item, childPath)...)
if len(result) >= 40 {
return result
}
}
default:
text := jsonValueText(v)
if text != "" {
result = append(result, collectedTextField{
Key: lastJSONPathKey(path),
Label: readableJSONPath(path),
Value: text,
})
}
}
return result
}
func firstCollectedValue(fields []collectedTextField, keys ...string) string {
for _, key := range keys {
for _, field := range fields {
if strings.EqualFold(field.Key, key) && strings.TrimSpace(field.Value) != "" {
return strings.TrimSpace(field.Value)
}
}
}
return ""
}
func lastJSONPathKey(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return ""
}
if idx := strings.LastIndex(path, "."); idx >= 0 {
path = path[idx+1:]
}
if idx := strings.Index(path, "["); idx >= 0 {
path = path[:idx]
}
return path
}
func readableJSONPath(path string) string {
path = strings.ReplaceAll(path, ".", " / ")
path = strings.ReplaceAll(path, "_", " ")
return path
}
func isNoisyJSONKey(key string) bool {
switch strings.ToLower(strings.TrimSpace(key)) {
case "icon", "icon_url", "avatar_url", "thumbnail", "image", "image_url", "picurl", "picture":
return true
default:
return false
}
}