This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_app_chat.go
141 lines (130 loc) · 4.05 KB
/
message_app_chat.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
package wecom
import (
"encoding/json"
"fmt"
"github.com/go-laoji/wecom-app-sdk/internal"
)
//企业微信支持企业自建应用通过接口创建群聊并发送消息到群,让重要的消息可更及时推送给群成员,方便协同处理。
//应用消息仅限于发送到通过接口创建的内部群聊,不支持添加企业外部联系人进群。此接口暂时仅支持企业内接入使用。
type AppChatCreateRequest struct {
Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"`
UserList []string `json:"userlist" validate:"required,min=2,max=2000"`
ChatId string `json:"chatid,omitempty"`
}
type AppChatCreateResponse struct {
internal.BizResponse
ChatId string `json:"chatid"`
}
// AppChatCreate 创建群聊会话
// https://open.work.weixin.qq.com/api/doc/90000/90135/90245
func (app weCom) AppChatCreate(request AppChatCreateRequest) (resp AppChatCreateResponse) {
if ok := validate.Struct(request); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
body, err := internal.HttpPost(fmt.Sprintf("/cgi-bin/appchat/create?%s", queryParams.Encode()), request)
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
type AppChatUpdateRequest struct {
ChatId string `json:"chatid" validate:"required"`
Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"`
AddUserList []string `json:"add_user_list,omitempty"`
DelUserList []string `json:"del_user_list,omitempty"`
}
// AppChatUpdate 修改群聊会话
// https://open.work.weixin.qq.com/api/doc/90000/90135/90246
func (app weCom) AppChatUpdate(request AppChatUpdateRequest) (resp internal.BizResponse) {
if ok := validate.Struct(request); ok != nil {
resp.ErrCode = 500
resp.ErrorMsg = ok.Error()
return
}
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
body, err := internal.HttpPost(fmt.Sprintf("/cgi-bin/appchat/update?%s", queryParams.Encode()), request)
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
type AppChatGetResponse struct {
internal.BizResponse
ChatInfo struct {
ChatId string `json:"chatid"`
Name string `json:"name"`
Owner string `json:"owner"`
UserList []string `json:"userlist"`
} `json:"chat_info"`
}
// AppChatGet 获取群聊会话
// https://open.work.weixin.qq.com/api/doc/90000/90135/90247
func (app weCom) AppChatGet(chatId string) (resp AppChatGetResponse) {
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
queryParams.Add("chatid", chatId)
body, err := internal.HttpGet(fmt.Sprintf("/cgi-bin/appchat/update?%s", queryParams.Encode()))
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}
// AppChatSend 应用推送消息
// https://open.work.weixin.qq.com/api/doc/90000/90135/90248
func (app weCom) AppChatSend(msg interface{}, chatId string) (resp internal.BizResponse) {
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["chatid"] = chatId
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"
default:
resp.ErrCode = 500
resp.ErrorMsg = "不支持的消息类型"
return
}
queryParams := app.buildBasicTokenQuery(app.getAppAccessToken())
body, err := internal.HttpPost(fmt.Sprintf("/cgi-bin/appchat/send?%s", queryParams.Encode()), h)
if err != nil {
resp.ErrCode = 500
resp.ErrorMsg = err.Error()
} else {
json.Unmarshal(body, &resp)
}
return
}