forked from xen0n/go-workwx
/
cmd_send_message.go
99 lines (89 loc) · 2.02 KB
/
cmd_send_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
package commands
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/whyiyhw/go-workwx"
)
func cmdSendMessage(c *cli.Context) error {
cfg := mustGetConfig(c)
isSafe := c.Bool(flagSafe)
toUsers := c.StringSlice(flagToUser)
toParties := c.StringSlice(flagToParty)
toTags := c.StringSlice(flagToTag)
toChat := c.String(flagToChat)
content := c.Args().Get(0)
msgtype := c.String(flagMessageType)
mediaID := c.String(flagMediaID)
thumbMediaID := c.String(flagThumbMediaID)
description := c.String(flagDescription)
title := c.String(flagTitle)
author := c.String(flagAuthor)
url := c.String(flagURL)
picURL := c.String(flagPicURL)
buttonText := c.String(flagButtonText)
sourceContentURL := c.String(flagSourceContentURL)
digest := c.String(flagDigest)
app := cfg.MakeWorkwxApp()
recipient := workwx.Recipient{
UserIDs: toUsers,
PartyIDs: toParties,
TagIDs: toTags,
ChatID: toChat,
}
if msgtype == "" {
// default to text
msgtype = string(workwx.MessageTypeText)
}
var err error
switch msgtype {
case string(workwx.MessageTypeText):
err = app.SendTextMessage(&recipient, content, isSafe)
case string(workwx.MessageTypeImage):
err = app.SendImageMessage(&recipient, mediaID, isSafe)
case string(workwx.MessageTypeVoice):
err = app.SendVoiceMessage(&recipient, mediaID, isSafe)
case string(workwx.MessageTypeVideo):
err = app.SendVideoMessage(
&recipient,
mediaID,
description,
title,
isSafe,
)
case "file":
err = app.SendFileMessage(&recipient, mediaID, isSafe)
case "textcard":
err = app.SendTextCardMessage(
&recipient,
title,
description,
url,
buttonText,
isSafe,
)
case "news":
err = app.SendNewsMessage(
&recipient,
title,
description,
url,
picURL,
isSafe,
)
case "mpnews":
err = app.SendMPNewsMessage(
&recipient,
title,
thumbMediaID,
author,
sourceContentURL,
content,
digest,
isSafe,
)
default:
fmt.Printf("unrecognized message type: %s\n", msgtype)
panic("unrecognized message type")
}
return err
}