初学go,网上看到了关于微信公众号发送消息的代码,想将其改成群发试试,读过关于微信的开发者文章,群发只要改其中的touser,但努力了半天还是不会,望有大神指点。
这里贴出网上的代码,若有侵权,还望海涵。
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
const (
appID = ""
appSecret = ""
accessTokenFetchUrl = "https://api.weixin.qq.com/cgi-bin/token"
customServicePostUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
)
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn float64 `json:"expires_in"`
}
type AccessTokenErrorResponse struct {
Errcode float64
Errmsg string
}
type CustomServiceMsg struct {
ToUser string `json:"touser"`
MsgType string `json:"msgtype"`
Text TextMsgContent `json:"text"`
}
type TextMsgContent struct {
Content string `json:"content"`
}
func fetchAccessToken() (string, float64, error) {
requestLine := strings.Join([]string{accessTokenFetchUrl,
"?grant_type=client_credential&appid=",
appID,
"&secret=",
appSecret}, "")
resp, err := http.Get(requestLine)
if err != nil || resp.StatusCode != http.StatusOK {
return "", 0.0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", 0.0, err
}
fmt.Println(string(body))
if bytes.Contains(body, []byte("access_token")) {
fmt.Println("return ok")
atr := AccessTokenResponse{}
err = json.Unmarshal(body, &atr)
if err != nil {
return "", 0.0, err
}
return atr.AccessToken, atr.ExpiresIn, nil
} else {
fmt.Println("return err")
ater := AccessTokenErrorResponse{}
err = json.Unmarshal(body, &ater)
if err != nil {
return "", 0.0, err
}
return "", 0.0, fmt.Errorf("%s", ater.Errmsg)
}
}
func pushCustomMsg(accessToken, toUser, msg string) error {
csMsg := &CustomServiceMsg{
ToUser: toUser,
MsgType: "text",
Text: TextMsgContent{Content: msg},
}
body, err := json.MarshalIndent(csMsg, " ", " ")
if err != nil {
return err
}
fmt.Println(string(body))
postReq, err := http.NewRequest("POST",
strings.Join([]string{customServicePostUrl, "?access_token=", accessToken}, ""),
bytes.NewReader(body))
if err != nil {
return err
}
postReq.Header.Set("Content-Type", "application/json; encoding=utf-8")
client := &http.Client{}
resp, err := client.Do(postReq)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func main() {
accessToken, expiresIn, err := fetchAccessToken()
if err != nil {
log.Println("Get access_token error:", err)
return
}
fmt.Println(accessToken, expiresIn)
openID := "oBQcwuAbKpiSAbbvd_DEZg7q27QI"
msg := "你好"
err = pushCustomMsg(accessToken, openID, msg)
if err != nil {
log.Println("Push custom service message err:", err)
return
}
}
有疑问加站长微信联系(非本文作者)