-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot_test.go
137 lines (116 loc) · 4.89 KB
/
robot_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
package dingtalk_test
import (
"bytes"
"testing"
"github.com/shockerli/dingtalk"
)
var robot *dingtalk.RobotCustom
func init() {
robot = dingtalk.NewRobotCustom()
robot.SetWebhook("your_robot_webhook")
robot.SetSecret("your_secret")
}
func TestRobotCustom_SendText(t *testing.T) {
// 根据Secret验证
if err := robot.SendText("TEST: Text"); err != nil {
t.Errorf("SendText() error = %v", err)
}
// AtAll
if err := robot.SendText("TEST: Text&AtAll", robot.AtAll()); err != nil {
t.Errorf("SendText() && AtAll() error = %v", err)
}
// AtMobiles
if err := robot.SendText("TEST: Text&AtMobiles", robot.AtMobiles("19900001111")); err != nil {
t.Errorf("SendText() && AtMobiles() error = %v", err)
}
}
func TestRobotCustom_SendLink(t *testing.T) {
if err := robot.SendLink(
"TEST: Link",
"link content",
"https://github.com/shockerli",
"https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg",
); err != nil {
t.Errorf("SendLink() error = %v", err)
}
}
func TestRobotCustom_SendMarkdown(t *testing.T) {
var markdown = `
## 安装
> go get github.com/shockerli/dingtalk
## 使用
> var robot = dingtalk.NewRobotCustom()
> robot.SetAccessToken("your_access_token")
> robot.SetSecret("your_secret") // 可选
`
if err := robot.SendMarkdown("TEST: Markdown", markdown); err != nil {
t.Errorf("SendMarkdown() error = %v", err)
}
if err := robot.SendMarkdown("TEST: Markdown&AtAll", markdown, robot.AtAll()); err != nil {
t.Errorf("SendMarkdown() && AtAll() error = %v", err)
}
if err := robot.SendMarkdown("TEST: Markdown&AtMobiles", markdown, robot.AtMobiles("19900001111")); err != nil {
t.Errorf("SendMarkdown() && AtMobiles() error = %v", err)
}
}
func TestRobotCustom_SendActionCard(t *testing.T) {
if err := robot.SendActionCard(
"TEST: ActionCard&SingleCard",
"SingleCard content",
robot.SingleCard("阅读全文", "https://github.com/shockerli"),
); err != nil {
t.Errorf("SendActionCard() && SingleCard() error = %v", err)
}
if err := robot.SendActionCard(
"TEST: ActionCard&MultiCard",
"MultiCard content",
robot.MultiCard("内容不错", "https://github.com/shockerli"),
robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
); err != nil {
t.Errorf("SendActionCard() && MultiCard() error = %v", err)
}
if err := robot.SendActionCard(
"TEST: ActionCard&BtnOrientation",
"BtnOrientation content",
robot.MultiCard("内容不错", "https://github.com/shockerli"),
robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
robot.BtnOrientation("0"),
); err != nil {
t.Errorf("SendActionCard() && BtnOrientation() error = %v", err)
}
if err := robot.SendActionCard(
"TEST: ActionCard&Image",
"\n\nSingleCard content with image",
robot.SingleCard("阅读全文", "https://github.com/shockerli"),
); err != nil {
t.Errorf("SendActionCard() && SingleCard() error = %v", err)
}
if err := robot.SendActionCard(
"TEST: ActionCard&HideAvatar",
"24565\n\n\n\nSingleCard content with image",
robot.SingleCard("阅读全文", "https://github.com/shockerli"),
robot.HideAvatar("1"),
); err != nil {
t.Errorf("SendActionCard() && HideAvatar() error = %v", err)
}
}
func TestRobotCustom_SendFeedCard(t *testing.T) {
if err := robot.SendFeedCard(
robot.FeedCard("3月15日起,Chromium 不能再调用谷歌 API", "https://bodhi.fedoraproject.org/updates/FEDORA-2021-48866282e5%29", "https://www.wangbase.com/blogimg/asset/202101/bg2021012506.jpg"),
robot.FeedCard("考古学家在英国发现两枚11世纪北宋时期的中国硬币", "https://www.caitlingreen.org/2020/12/another-medieval-chinese-coin-from-england.html", "https://www.wangbase.com/blogimg/asset/202101/bg2021012208.jpg"),
); err != nil {
t.Errorf("SendFeedCard() error = %v", err)
}
}
func TestRobotCustom_Outgoing(t *testing.T) {
// replace with the available callback body
var callbackBody = `{"conversationId":"ciddz7nmHDaX/7Niz+Gb5VVrw==","sceneGroupCode":"project","atUsers":[{"dingtalkId":"$:LWCP_v1:$0sIVIuw1HvQQ5gRAtFWzypo0+T1TgPOP"},{"dingtalkId":"$:LWCP_v1:$I3cyfTzrws4nCbY289cXbKCVcdd1wize"}],"chatbotUserId":"$:LWCP_v1:$I3cyfTzrws4nCbY289cXbKCVcdd1wize","msgId":"msgaKcioIqERkELm2T8TlE9CA==","senderNick":"Jioby","isAdmin":false,"sessionWebhookExpiredTime":1612178396066,"createAt":1612172996026,"conversationType":"2","senderId":"$:LWCP_v1:$deZJcSfMzexC2YK+oLkk1g==","conversationTitle":"xxx","isInAtList":true,"sessionWebhook":"https://oapi.dingtalk.com/robot/sendBySession?session=eb18e18e8669b0a3cd7dff1388fe5e6a","text":{"content":" 哈哈哈"},"msgtype":"text"}`
og, err := robot.ParseOutgoing(bytes.NewBufferString(callbackBody))
if err != nil {
t.Errorf("ParseOutgoing() error = %v", err)
}
err = robot.SendText("callback", robot.WithOutgoing(og))
if err != nil {
t.Errorf("WithOutgoing() error= %v", err)
}
}