代码拉取完成,页面将自动刷新
package work
import (
"encoding/json"
"fmt"
"gitee.com/h79/gothird/result"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/weixin/consts"
"gitee.com/h79/gothird/weixin/response"
"gitee.com/h79/gothird/weixin/work/structs"
"gitee.com/h79/goutils/common/http"
)
//https://work.weixin.qq.com/api/doc#90000/90135/90664
func genHead(toWho structs.ToWho, msgType string) structs.Head {
h := structs.Head{
Type: msgType,
Safe: 0,
}
if yes, agent := toWho.IsAgent(); yes {
h.AgentId = agent.AgentId
h.ToTag = agent.ToTag
h.ToParty = agent.ToParty
h.ToUser = agent.ToUser
}
if yes, group := toWho.IsGroup(); yes {
h.ChatId = group.ChatId
}
return h
}
func NewMessage(toWho structs.ToWho, msgType string, msg interface{}) structs.Message {
switch msgType {
case "text":
return &structs.TextMsg{
Head: genHead(toWho, msgType),
Text: msg.(structs.ExText),
}
case "image":
return &structs.ImageMsg{
Head: genHead(toWho, msgType),
Image: msg.(structs.ExImage),
}
case "voice":
return &structs.VoiceMsg{
Head: genHead(toWho, msgType),
Voice: msg.(structs.ExVoice),
}
case "video":
return &structs.VideoMsg{
Head: genHead(toWho, msgType),
Video: msg.(structs.ExVideo),
}
case "news":
return &structs.NewsMsg{
Head: genHead(toWho, msgType),
News: msg.(structs.News),
}
case "textcard":
return &structs.TextCardMsg{
Head: genHead(toWho, msgType),
TextCard: (msg).(structs.TextCard),
}
case "markdown":
return &structs.MarkdownMsg{
Head: genHead(toWho, msgType),
Markdown: msg.(structs.ExText),
}
default:
}
return nil
}
// SendMessage 发送应用消息,群聊消息,客户欢迎语
func SendMessage(api *token.Api, msg structs.Message) error {
var url string
if msg.IsAgent() {
url = fmt.Sprintf("%s/cgi-bin/message/send?", consts.WorkApiPrefixUrl)
} else if msg.IsGroup() {
url = fmt.Sprintf("%s/cgi-bin/appchat/send?", consts.WorkApiPrefixUrl)
} else if msg.IsWelcome() { //发送新客户欢迎语
url = fmt.Sprintf("%s/cgi-bin/externalcontact/send_welcome_msg?", consts.WorkApiPrefixUrl)
} else {
return result.Error(-1, "[WX] SendMessage parameter is error")
}
buf, err := json.Marshal(msg)
if err != nil {
return result.Error(-1, "[WX] SendMessage Json marshal error")
}
res := response.Response{}
err = api.Request("POST", url, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
if err != nil {
return err
}
return res.ErrorIf()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。