-
Notifications
You must be signed in to change notification settings - Fork 57
/
dingtalk_test.go
184 lines (165 loc) · 5.52 KB
/
dingtalk_test.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
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
package dingtalk
import (
"context"
"net/http"
"testing"
"time"
)
var dingToken = []string{"b9230b8c762cb3a6f5dd977ad975c687e23fdefc6c762fe94a0f36bca73fb654"}
//var dingToken = []string{"0471c091acf1d9dfc553e57525f57139859858b905836b8f45b228e6d6e3a289"} // onlyOne
var dingTalkCli = InitDingTalk(dingToken, ".")
var dingTalkCliWithSecret = InitDingTalkWithSecret("47f656c278ff59b59e39486f98e5e7e1d1a3ef0bca22f9c01b37a90b81fe91aa", "SEC0fdc400a2b7e2f70f0b4d787a8f0623fe9742225bee747b4d5852162d24a63ed") // 加签
var testImg = "https://golang.google.cn/lib/godoc/images/footer-gopher.jpg"
var testUrl = "https://golang.google.cn/"
var testPhone = "1318282596*"
func init() {
dingTalkCli = InitDingTalk(dingToken, ".")
}
func TestDingTalkInitWithTimeout(t *testing.T) {
dingTalkCliWithOpt := InitDingTalk(dingToken, ".", WithInitSendTimeout(time.Second*3))
err := dingTalkCliWithOpt.SendTextMessage("增加sendTimeout,自定义超时时间", WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestTextMsgWithSecret expected be nil, but %v got", err)
}
}
func TestTextMsgWithSecret(t *testing.T) {
err := dingTalkCliWithSecret.SendTextMessage("加签测试", WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestTextMsgWithSecret expected be nil, but %v got", err)
}
}
func TestTextMsg(t *testing.T) {
err := dingTalkCli.SendTextMessage("Text 测试", WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestTextMsg expected be nil, but %v got", err)
}
}
func TestTextMsgWithCtx(t *testing.T) {
err := dingTalkCli.SendTextMessage("Text No Context 兼容测试", WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestTextMsg SendTextMessage expected be nil, but %v got", err)
}
err = dingTalkCli.SendTextMessageWithCtx(context.Background(), "Text With Context 测试", WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestTextMsg SendTextMessageWithCtx expected be nil, but %v got", err)
}
}
func TestLinkMsg(t *testing.T) {
err := dingTalkCli.SendLinkMessage("Link title", "Link test.", testImg, testUrl)
if err != nil {
t.Errorf("TestLinkMsg expected be nil, but %v got", err)
}
}
func TestMarkDownMsg(t *testing.T) {
err := dingTalkCli.SendMarkDownMessage("Markdown title", "### Link test\n --- \n- <font color=#ff0000 size=6>红色文字</font> \n - content2.", WithAtAll())
if err != nil {
t.Errorf("TestMarkDownMsg expected be nil, but %v got", err)
}
}
func TestSendDTMDMessage(t *testing.T) {
// 有序map
dtmdOrderMap := DingMap().
Set("dtmdOrderMap1", "dtmdValue1").
Set("dtmdOrderMap2", "dtmdValue2").
Set("dtmdOrderMap3", "dtmdValue3")
err := dingTalkCli.SendDTMDMessage("DTMD title", dtmdOrderMap)
if err != nil {
t.Errorf("TestSendDTMDMessage expected be nil, but %v got", err)
}
}
func TestSendMarkDownMessageByList(t *testing.T) {
msg := []string{
"### Link test",
"---",
"- <font color=#ff0000 size=2>红色文字</font>",
"- content2",
}
err := dingTalkCli.SendMarkDownMessageBySlice("Markdown title", msg, WithAtMobiles([]string{testPhone}))
if err != nil {
t.Errorf("TestSendMarkDownMessageByList expected be nil, but %v got", err)
}
}
func TestActionCardMultiMsg(t *testing.T) {
Btns := []ActionCardMultiBtnModel{{
Title: "test1",
ActionURL: testUrl,
}, {
Title: "test2",
ActionURL: testUrl,
},
}
//err := dingTalkCli.SendActionCardMessage("ActionCard title", "ActionCard text.", WithCardSingleTitle("title"), WithCardSingleURL(testUrl))
err := dingTalkCli.SendActionCardMessage("ActionCard title", "- ActionCard text.", WithCardBtns(Btns), WithCardBtnVertical())
if err != nil {
t.Errorf("TestActionCardMultiMsg expected be nil, but %v got", err)
}
}
func TestActionCardMultiMsgBySlice(t *testing.T) {
Btns := []ActionCardMultiBtnModel{{
Title: "test1",
ActionURL: testUrl,
}, {
Title: "test2",
ActionURL: testUrl,
},
}
dm := DingMap()
dm.Set("颜色测试", H2)
dm.Set("失败:$$ 同行不同色 $$", RED)
dm.Set("---", "")
dm.Set("金色", GOLD)
dm.Set("成功", GREEN)
dm.Set("警告", BLUE)
dm.Set("普通文字", N)
//err := dingTalkCli.SendActionCardMessage("ActionCard title", "ActionCard text.", WithCardSingleTitle("title"), WithCardSingleURL(testUrl))
err := dingTalkCli.SendActionCardMessageBySlice("ActionCard title", dm.Slice(), WithCardBtns(Btns), WithCardBtnVertical())
if err != nil {
t.Errorf("TestActionCardMultiMsgBySlice expected be nil, but %v got", err)
}
}
func TestFeedCardMsg(t *testing.T) {
links := []FeedCardLinkModel{
{
Title: "FeedCard1.",
MessageURL: testUrl,
PicURL: testImg,
},
{
Title: "FeedCard2",
MessageURL: testUrl,
PicURL: testImg,
},
{
Title: "FeedCard3",
MessageURL: testUrl,
PicURL: testImg,
},
}
err := dingTalkCli.SendFeedCardMessage(links)
if err != nil {
t.Errorf("TestFeedCardMsg expected be nil, but %v got", err)
}
}
func TestDingMap(t *testing.T) {
dm := DingMap()
dm.Set("颜色测试", H2)
dm.Set("失败:$$ 同行不同色 $$", RED)
dm.Set("---", "")
dm.Set("金色", GOLD)
dm.Set("成功", GREEN)
dm.Set("警告", BLUE)
dm.Set("普通文字", N)
err := dingTalkCli.SendMarkDownMessageBySlice("color test", dm.Slice())
if err != nil {
t.Errorf("TestDingMap expected be nil, but %v got", err)
}
}
func TestOutGoing(t *testing.T) {
outgoingFunc := func(args []string) []byte {
// do what you want to
return NewTextMsg("hello").Marshaler()
}
RegisterCommand("hello", outgoingFunc, 1, true)
http.Handle("/outgoing", &OutGoingHandler{})
_ = http.ListenAndServe(":8000", nil)
}