-
Notifications
You must be signed in to change notification settings - Fork 3
/
message.go
137 lines (117 loc) · 3.06 KB
/
message.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
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
package dingbot
import (
"encoding/json"
"github.com/pkg/errors"
)
// Message dingTalk all message type
type Message struct {
MsgType string `json:"msgtype"`
Text *text `json:"text,omitempty"`
Link *link `json:"link,omitempty"`
Markdown *markdown `json:"markdown,omitempty"`
ActionCard *actionCard `json:"actionCard,omitempty"`
FeedCard *feedCard `json:"feedCard,omitempty"`
AtMobiles *at `json:"at,omitempty"`
}
// String message to string
func (th Message) String() string {
b, _ := json.Marshal(th)
return string(b)
}
// At someone
func (th *Message) At(atAll bool, mobiles ...string) (err error) {
switch th.MsgType {
case "":
err = errors.New("msgtype is empty")
return
}
// todo check mobile
th.AtMobiles = &at{
IsAtAll: atAll,
AtMobiles: mobiles,
}
return
}
// buttons actionCard buttons
type buttons struct {
Title string `json:"title"`
ActionURL string `json:"actionURL"`
}
// actionCard message
// todo
type actionCard struct {
Title string `json:"title"` // 必填
Text string `json:"text"` // 必填
BtnOrientation string `json:"btnOrientation"` // 0-按钮竖直排列,1-按钮横向排列
SingleTitle string `json:"singleTitle"`
SingleURL string `json:"singleURL"`
Buttons []buttons `json:"btns"`
}
// Links ...
type Links struct {
Title string `json:"title"` // 必填
MessageURL string `json:"messageURL"` // 必填
PicURL string `json:"picURL"` // 必填
}
// feedCard message
// todo
type feedCard struct {
Links []Links `json:"links"`
}
// markdown message
type markdown struct {
Title string `json:"title"` // 必填
Text string `json:"text"` // 必填
}
// link message
type link struct {
Text string `json:"text"` // 必填
Title string `json:"title"` // 必填
MessageURL string `json:"messageUrl"` // 必填
PicURL string `json:"picUrl"`
}
// text message
type text struct {
Content string `json:"content"` // 必填
}
// at user
type at struct {
AtMobiles []string `json:"atMobiles"`
IsAtAll bool `json:"isAtAll"`
}
// TextMessage dingTalk text message
func TextMessage(content string) (msg Message) {
msg.MsgType = "text"
msg.Text = &text{Content: content}
return
}
// MarkdownMessage dingTalk markdown message
func MarkdownMessage(title, content string) (msg Message) {
msg.MsgType = "markdown"
msg.Markdown = &markdown{Text: content, Title: title}
return
}
// LinkMessage dingTalk link message
func LinkMessage(title, text, messageURL, picURL string) (msg Message) {
msg.MsgType = "link"
msg.Link = &link{
Text: title,
Title: text,
MessageURL: messageURL,
PicURL: picURL,
}
return
}
// FeedCardLink create feedCard link
func FeedCardLink(title, messageURL, picURL string) (l Links) {
l.Title = title
l.MessageURL = messageURL
l.PicURL = picURL
return
}
// FeedCardMessage dingTalk feedCard message
func FeedCardMessage(links ...Links) (msg Message) {
msg.MsgType = "feedCard"
msg.FeedCard = &feedCard{Links: links}
return
}