-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmessage.go
185 lines (167 loc) · 5.46 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
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
185
package wework
import (
"encoding/json"
"github.com/go-laoji/wecom-go-sdk/v2/internal"
)
type Message struct {
ToUser string `json:"touser,omitempty" validate:"omitempty,required_without=ToParty ToTag"`
ToParty string `json:"toparty,omitempty" validate:"omitempty,required_without=ToUser ToTag"`
ToTag string `json:"totag,omitempty" validate:"omitempty,required_without=ToParty ToUser"`
EnableIDTrans int `json:"enable_id_trans,omitempty"`
EnableDuplicateCheck int `json:"enable_duplicate_check,omitempty"`
DuplicateCheckInterval int `json:"duplicate_check_interval,omitempty"`
}
type TextMessage struct {
Message
Safe int `json:"safe,omitempty" validate:"omitempty,oneof=0 1"`
Text Text `json:"text" validate:"required"`
}
type ImageMessage struct {
Message
Safe int `json:"safe,omitempty" validate:"omitempty,oneof=0 1"`
Image MultiMedia `json:"image" validate:"required"`
}
type MultiMedia struct {
MediaId string `json:"media_id" validate:"required"`
}
type VoiceMessage struct {
Message
Safe int `json:"safe,omitempty"`
Voice MultiMedia `json:"voice" validate:"required"`
}
type VideoMessage struct {
Message
Safe int `json:"safe,omitempty" validate:"omitempty,oneof=0 1"`
Video Video `json:"video" validate:"required"`
}
type FileMessage struct {
Message
Safe int `json:"safe,omitempty"`
File MultiMedia `json:"file" validate:"required"`
}
type TextCard struct {
Title string `json:"title" validate:"required"`
Description string `json:"description" validate:"required"`
Url string `json:"url" validate:"required"`
BtnTxt string `json:"btntxt"`
}
type TextCardMessage struct {
Message
TextCard TextCard `json:"textcard" validate:"required"`
}
type Article struct {
Title string `json:"title"`
Description string `json:"description"`
Url string `json:"url"`
PicUrl string `json:"picurl"`
AppId string `json:"appid" validate:"required_without=Url,required_with=PagePath"`
PagePath string `json:"pagepath" validate:"required_with=AppId"`
}
type News struct {
Articles []Article `json:"articles" validate:"required,max=8"`
}
type NewsMessage struct {
Message
News News `json:"news" validate:"required"`
}
type MpArticle struct {
Title string `json:"title" validate:"required"`
ThumbMediaId string `json:"thumb_media_id" validate:"required"`
Author string `json:"author,omitempty"`
ContentSourceUrl string `json:"content_source_url,omitempty"`
Content string `json:"content" validate:"required"`
Digest string `json:"digest,omitempty"`
}
type MpNews struct {
Articles []MpArticle `json:"articles" validate:"required"`
}
type MpNewsMessage struct {
Message
Safe int `json:"safe,omitempty" validate:"omitempty,oneof=0 1 2"`
MpNews MpNews `json:"mpnews" validate:"required"`
}
type MarkDownMessage struct {
Message
MarkDown Text `json:"markdown" validate:"required"`
}
type MiniProgramNotice struct {
Appid string `json:"appid" validate:"required"`
Page string `json:"page"`
Title string `json:"title" validate:"required"`
Description string `json:"description"`
EmphasisFirstItem bool `json:"emphasis_first_item"`
ContentItem []struct {
Key string `json:"key" validate:"required"`
Value string `json:"value" validate:"required"`
} `json:"content_item"`
}
type MiniProgramMessage struct {
Message
MiniProgramNotice MiniProgramNotice `json:"miniprogram_notice"`
}
type MessageSendResponse struct {
internal.BizResponse
InvalidUser string `json:"invaliduser"`
InvalidParty string `json:"invalidparty"`
InvalidTag string `json:"invalidtag"`
MsgId string `json:"msgid"`
ResponseCode string `json:"response_code"`
//仅消息类型为“按钮交互型”,“投票选择型”和“多项选择型”的模板卡片消息返回
//应用可使用response_code调用更新模版卡片消息接口,24小时内有效,且只能使用一次
}
// MessageSend 发送应用消息
// https://open.work.weixin.qq.com/api/doc/90001/90143/90372
func (ww *weWork) MessageSend(corpId uint, msg interface{}) (resp MessageSendResponse) {
if ok := validate.Struct(msg); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
h := H{}
buf, _ := json.Marshal(msg)
json.Unmarshal(buf, &h)
h["agentid"] = ww.GetAgentId(corpId)
switch msg.(type) {
case TextMessage:
h["msgtype"] = "text"
case ImageMessage:
h["msgtype"] = "image"
case VoiceMessage:
h["msgtype"] = "voice"
case VideoMessage:
h["msgtype"] = "video"
case FileMessage:
h["msgtype"] = "file"
case TextCardMessage:
h["msgtype"] = "textcard"
case NewsMessage:
h["msgtype"] = "news"
case MpNewsMessage:
h["msgtype"] = "mpnews"
case MarkDownMessage:
h["msgtype"] = "markdown"
case MiniProgramMessage:
h["msgtype"] = "miniprogram_notice"
case TemplateCardMessage:
h["msgtype"] = "template_card"
}
_, err := ww.getRequest(corpId).SetBody(h).SetResult(&resp).
Post("/cgi-bin/message/send")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}
// MessageReCall 撤回应用消息
// https://open.work.weixin.qq.com/api/doc/90000/90135/94867
func (ww *weWork) MessageReCall(corpId uint, msgId string) (resp internal.BizResponse) {
h := H{"msgid": msgId}
_, err := ww.getRequest(corpId).SetBody(h).SetResult(&resp).
Post("/cgi-bin/message/recall")
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
}
return
}