-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_type.go
102 lines (89 loc) · 2.05 KB
/
msg_type.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
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
package dingding
import "fmt"
const (
MsgTypeTextTmpl = `{"msgtype":"text","text":{"content":"%s"}}`
MsgTypeMarkdownTmpl = `{"msgtype":"markdown","markdown":{"title":"%s","text":"%s"}}`
MsgTypeMarkdownAtTmpl = `{"msgtype":"markdown","markdown":{"title":"%s","text":"%s"},"at":{"isAtAll": false, "atMobiles":[]}}`
)
type RobotConfig struct {
Token string
Secret string
}
type DingMsg struct {
MsgType string `json:"msgtype"`
Text Text `json:"text"`
At At `json:"at"`
}
type Text struct {
Content string `json:"content"`
}
type At struct {
AtMobiles []string `json:"atMobiles"`
AtUserIds []int `json:"atUserIds"`
IsAtAll bool `json:"isAtAll"`
}
type Link struct {
Text string `json:"text"`
Title string `json:"title"`
PicUrl string `json:"picUrl"`
MessageUrl string `json:"messageUrl"`
}
type ActionCard struct {
Title string `json:"title"`
Text string `json:"text"`
BtnOrientation string `json:"btnOrientation"`
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleURL"`
}
type FeedCard struct {
Links []struct {
Title string `json:"title"`
MessageURL string `json:"messageURL"`
PicURL string `json:"picURL"`
} `json:"links"`
}
type MsgType int
const (
_ MsgType = iota
MsgTypeText
MsgTypeMarkdown
MsgTypeLink
MsgTypeActionCard
MsgTypeFeedCard
)
func (c MsgType) String() string {
switch c {
case MsgTypeText:
return "text"
case MsgTypeMarkdown:
return "markdown"
case MsgTypeLink:
return "link"
case MsgTypeActionCard:
return "actionCard"
case MsgTypeFeedCard:
return "feedCard"
default:
return ""
}
}
func (c MsgType) Tmpl() string {
switch c {
case MsgTypeText:
return MsgTypeTextTmpl
case MsgTypeMarkdown:
return MsgTypeMarkdownTmpl
default:
return MsgTypeTextTmpl
}
}
func (c MsgType) Body(title, content string) string {
switch c {
case MsgTypeText:
return fmt.Sprintf(c.Tmpl(), content)
case MsgTypeMarkdown:
return fmt.Sprintf(c.Tmpl(), title, content)
default:
return fmt.Sprintf(c.Tmpl(), content)
}
}