公眾號服務號每個月只能群發推送四次文章,我們可以使用模板消息為公眾號粉絲推送信息 下麵是使用golang實現的模板消息發送類庫封裝,輕鬆實現模板消息發送 wechat.go package lib import ( "github.com/silenceper/wechat/v2" "github. ...
公眾號服務號每個月只能群發推送四次文章,我們可以使用模板消息為公眾號粉絲推送信息
下麵是使用golang實現的模板消息發送類庫封裝,輕鬆實現模板消息發送
wechat.go
package lib import ( "github.com/silenceper/wechat/v2" "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/officialaccount" offConfig "github.com/silenceper/wechat/v2/officialaccount/config" "github.com/silenceper/wechat/v2/officialaccount/message" "log" ) type WechatOffical struct { AppId, AppSecret, Token string OfficialAccount *officialaccount.OfficialAccount } func NewWechatOffical(appId, appSecret, token string, memory cache.Cache) *WechatOffical { obj := &WechatOffical{ AppId: appId, AppSecret: appSecret, Token: token, } wc := wechat.NewWechat() //這裡本地記憶體保存access_token,也可選擇redis,memcache或者自定cache cfg := &offConfig.Config{ AppID: appId, AppSecret: appSecret, Token: token, //EncodingAESKey: "xxxx", Cache: memory, } obj.OfficialAccount = wc.GetOfficialAccount(cfg) return obj } /* 發送模板消息 messages := []map[string]string{ {"key": "thing4", "value": "我想購買客服系統"}, {"key": "thing13", "value": "老狼"}, {"key": "time14", "value": "2023-07-27 10:10:10"}, } offical.SendTemplateMessage( []string{"openid"}, "模板ID", "跳轉地址", messages, ) */ func (this *WechatOffical) SendTemplateMessage(openids []string, templateId, url string, messages []map[string]string) { template := this.OfficialAccount.GetTemplate() msgData := make(map[string]*message.TemplateDataItem) for _, item := range messages { msgData[item["key"]] = &message.TemplateDataItem{ Value: item["value"], } } for _, openid := range openids { msg := &message.TemplateMessage{ ToUser: openid, Data: msgData, TemplateID: templateId, URL: url, } msgId, err := template.Send(msg) if err != nil { log.Println(err, msgId) } } }
我們的使用方式
package lib import ( "github.com/silenceper/wechat/v2/cache" "testing" ) func TestWechatOffical_SendTemplateMessage(t *testing.T) { memory := cache.NewMemory() offical := NewWechatOffical("xxx", "xxxxx", "xxxx", memory) messages := []map[string]string{ {"key": "thing4", "value": "我想購買客服系統"}, {"key": "thing13", "value": "老狼"}, {"key": "time14", "value": "2023-07-27 10:10:10"}, } offical.SendTemplateMessage( []string{"xxxx"}, "xxxxxxxx", "https://gofly.v1kf.com", messages, ) }
推送的效果如圖所示,點擊模板就能跳轉到我們自定義的url上
我在自己客服系統中也是使用這種方式為客服進行消息提醒的,客服收到提醒後,點擊就能回覆訪客消息,非常方便
有需要的朋友可以訪問 gofly.v1kf.com ,瞭解測試我的客服系統