1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-07 06:02:26 +08:00
Files
wechat/work/externalcontact/add_msg_template.go
2023-01-09 17:43:54 +08:00

91 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package externalcontact
import (
"encoding/json"
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
addMsgTemplateUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template"
)
type ChatType string
const (
ChatTypeSingle ChatType = "single"
ChatTypeGroup ChatType = "group"
)
// ReqMessage 企业群发参数
type ReqMessage struct {
ChatType ChatType `json:"chat_type"` //群发任务的类型默认为single表示发送给客户group表示发送给客户群
ExternalUserid []string `json:"external_userid"` // 客户的外部联系人id列表仅在chat_type为single时有效不可与sender同时为空最多可传入1万个客户
Sender string `json:"sender"` //发送企业群发消息的成员userid当类型为发送给客户群时必填
Text struct {
Content string `json:"content"`
} `json:"text"`
Attachments []struct {
Msgtype string `json:"msgtype"`
Image MsgImage `json:"image"`
Link MsgLink `json:"link"`
Miniprogram MsgMiniprogram `json:"miniprogram"`
Video MsgVideo `json:"video"`
File MsgFile `json:"file"`
} `json:"attachments"`
}
type MsgImage struct {
MediaId string `json:"media_id"`
PicUrl string `json:"pic_url"`
}
type MsgLink struct {
Title string `json:"title"`
Picurl string `json:"picurl"`
Desc string `json:"desc"`
Url string `json:"url"`
}
type MsgMiniprogram struct {
Title string `json:"title"`
PicMediaId string `json:"pic_media_id"`
Appid string `json:"appid"`
Page string `json:"page"`
}
type MsgVideo struct {
MediaId string `json:"media_id"`
}
type MsgFile struct {
MediaId string `json:"media_id"`
}
type resTemplateSend struct {
util.CommonError
FailList string `json:"fail_list"`
MsgID int64 `json:"msgid"`
}
// Send 发送应用消息
func (r *Client) Send(msg *ReqMessage) (msgID int64, err error) {
var accessToken string
accessToken, err = r.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", addMsgTemplateUrl, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
return
}
var result resTemplateSend
err = json.Unmarshal(response, &result)
if err != nil {
return
}
if result.ErrCode != 0 {
err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
return
}
msgID = result.MsgID
return
}