/
feedCard.go
63 lines (53 loc) · 1.64 KB
/
feedCard.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
package dingtalk
import (
"log"
"github.com/CatchZeng/dingtalk/pkg/dingtalk"
"github.com/spf13/cobra"
)
var feedCardCmd = &cobra.Command{
Use: "feedCard",
Short: "send feedCard message with DingTalk robot",
Long: `send feedCard message with DingTalk robot`,
Args: cobra.MinimumNArgs(0),
Run: runFeedCardCmd,
}
func runFeedCardCmd(_ *cobra.Command, args []string) {
if len(feedCardVars.titles) < 1 || len(feedCardVars.picURLs) < 1 || len(feedCardVars.messageURLs) < 1 {
log.Fatal("titles & picURLs & messageURLs can not be empty")
return
}
if len(feedCardVars.titles) == len(feedCardVars.picURLs) && len(feedCardVars.picURLs) == len(feedCardVars.messageURLs) {
client, err := newClient()
if err != nil {
log.Fatal(err.Error())
return
}
msg := dingtalk.NewFeedCardMessage()
for i := 0; i < len(feedCardVars.titles); i++ {
msg.AppendLink(feedCardVars.titles[i], feedCardVars.messageURLs[i], feedCardVars.picURLs[i])
}
req, _, err := client.Send(msg)
if debug {
log.Print(req)
}
if err != nil {
log.Fatal(err.Error())
return
}
} else {
log.Fatal("titles & picURLs & messageURLs count must be equal")
}
}
// FeedCardVars struct
type FeedCardVars struct {
titles []string
picURLs []string
messageURLs []string
}
var feedCardVars FeedCardVars
func init() {
rootCmd.AddCommand(feedCardCmd)
feedCardCmd.Flags().StringSliceVarP(&feedCardVars.titles, "titles", "i", []string{}, "titles")
feedCardCmd.Flags().StringSliceVarP(&feedCardVars.picURLs, "picURLs", "p", []string{}, "picURLs")
feedCardCmd.Flags().StringSliceVarP(&feedCardVars.messageURLs, "messageURLs", "u", []string{}, "messageURLs")
}