- 先准备好公众号和小程序,并且将小程序关联公众号
-
准备一张小程序的缩略图
//使用 https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=yourtoken&type=thumb 接口进行缩略图进行上传 //这里我是通过postman进行上传的 //获取asstoken 可能会报白名单的问题 ,需要把你获取token的ip 添加到公众号白名单。
记录下返回的thumb_media_id(media_id) - 使用 "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s" 接口进行发送客户服务消息
card := MiniprogramCard{ Touser: reflect.ValueOf(server.RequestMsg.FromUserName).String(), //需要发送的用户 //miniprogrampage Msgtype: "miniprogrampage", Miniprogrampage: MiniProgramPage{ Title: "title", Appid: "", //微信小程序appid PagePath: "pages/index/index", //微信小程序页面地址 ThumbMediaId: "A9WJuZt1K57Vs1AQAPmx3JG_G7IGzBS_2sx_WpNSGvU"}, //小程序卡片图片的媒体ID }
下面是具体实现的代码
package main import ( "bytes" "encoding/json" "fmt" wechat "github.com/silenceper/wechat/v2" "github.com/silenceper/wechat/v2/cache" offConfig "github.com/silenceper/wechat/v2/officialaccount/config" "github.com/silenceper/wechat/v2/officialaccount/message" "net/http" "reflect" "time" ) func GenHTTPClient(timeout *time.Duration) *http.Client { if timeout != nil { return &http.Client{ Timeout: *timeout * time.Second, } } else { return &http.Client{ Timeout: 3 * time.Second, } } } type MiniprogramCard struct { Touser string `json:"touser"` Msgtype string `json:"msgtype"` Miniprogrampage MiniProgramPage `json:"miniprogrampage"` } type MiniProgramPage struct { Title string `json:"title"` Appid string `json:"appid"` //微信小程序appid PagePath string `json:"pagepath"` //微信小程序页面地址 ThumbMediaId string `json:"thumb_media_id"` } func serveWechat(rw http.ResponseWriter, req *http.Request) { wc := wechat.NewWechat() //这里本地内存保存access_token,也可选择redis,memcache或者自定cache memory := cache.NewMemory() cfg := &offConfig.Config{ //从你自己公众号后台去拿 AppID: "", AppSecret: "", Token: "", EncodingAESKey: "", Cache: memory, } officialAccount := wc.GetOfficialAccount(cfg) ak, _ := officialAccount.GetAccessToken() // 传入request和responseWriter server := officialAccount.GetServer(req, rw) //设置接收消息的处理方法 server.SetMessageHandler(func(mixMessage *message.MixMessage) *message.Reply { //TODO //回复消息:演示回复用户发送的消息 switch mixMessage.MsgType { //自动回复消息 case message.MsgTypeText: text := message.NewText(mixMessage.Content) return &message.Reply{MsgType: message.MsgTypeText, MsgData: text} } switch mixMessage.Event { //如果是初次订阅回复图文消息 case message.EventSubscribe: //获取token //根据token 发送客服消息 url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s", ak) card := MiniprogramCard{ Touser: reflect.ValueOf(server.RequestMsg.FromUserName).String(), //需要发送的用户 //miniprogrampage Msgtype: "miniprogrampage", Miniprogrampage: MiniProgramPage{ Title: "title", Appid: "", //微信小程序appid PagePath: "pages/index/index", //微信小程序页面地址 ThumbMediaId: "A9WJuZt1K57Vs1AQAPmx3JG_G7IGzBS_2sx_WpNSGvU"}, //小程序卡片图片的媒体ID } req, _ := json.Marshal(card) res, _ := GenHTTPClient(nil).Post(url, "application/json", bytes.NewBuffer(req)) fmt.Println(res) } text := message.NewText(mixMessage.Content) return &message.Reply{MsgType: message.MsgTypeText, MsgData: text} }) //处理消息接收以及回复 err := server.Serve() if err != nil { fmt.Println(err) return } //发送回复的消息 server.Send() } func main() { http.HandleFunc("/", serveWechat) fmt.Println("wechat server listener at", ":8001") err := http.ListenAndServe(":8001", nil) if err != nil { fmt.Printf("start server error , err=%v", err) } }
tips:另外如果没有线上环境域名 ,可以在花生壳买一个域名,然后把域名代理到本地的一个端口。这样就能在本地断点微信公众号那边发送过来的消息了。
使用go开发公众号之 关注公众号发送小程序卡片
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.