-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
sender_lark.go
66 lines (55 loc) · 1.81 KB
/
sender_lark.go
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
package sender
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/ArtalkJS/Artalk/internal/i18n"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/ArtalkJS/Artalk/internal/utils"
)
// 飞书发送
func SendLark(webhookURL string, title string, content string, isCard bool) {
if title != "" {
content = title + "\n\n" + content
}
var data string
if isCard {
texts := strings.Split(strings.TrimSpace(content), "\n")
link := texts[len(texts)-1]
if utils.ValidateURL(link) {
content = strings.Join(texts[1:len(texts)-1], "\n")
} else {
content = strings.Join(texts[1:], "\n")
link = ""
}
data = getLarkCardJson(texts[0], content, link)
} else {
data = getLarkTextJson(content)
}
result, err := http.Post(webhookURL, "application/json", strings.NewReader(data))
if err != nil {
log.Error("[飞书] Failed to send msg:", err)
return
}
if result.StatusCode != 200 {
body, _ := io.ReadAll(result.Body)
log.Error("[飞书] Failed to send msg:", string(body), " ", string(data))
}
defer result.Body.Close()
}
func getLarkTextJson(text string) string {
return fmt.Sprintf(`{"msg_type":"text","content":{"text":%s}}`, strconv.Quote(text))
}
func getLarkCardJson(title string, content string, replyLink string) string {
title = strings.TrimSpace(title)
content = strings.TrimSpace(content)
buttonCode := ""
if replyLink != "" {
buttonCode = fmt.Sprintf(`,{"tag":"action","actions":[{"tag":"button","text":{"content":%s,"tag":"lark_md"},"url":%s,"type":"default","value":{}}]}`,
strconv.Quote(i18n.T("Reply")), strconv.Quote(replyLink))
}
return fmt.Sprintf(`{"msg_type":"interactive","card":{"header":{"title":{"content":%s,"tag":"plain_text"}},"elements":[{"tag":"div","text":{"content":%s,"tag":"lark_md"}}%s]}}`,
strconv.Quote(title), strconv.Quote(content), buttonCode)
}