WeChat public account template message source code implementation, breaking the limit on the number of service account group sending push times

The official account service account can only send and push articles four times a month, and we can use template messages to push information for the fans of the official account

The following is the encapsulation of the template message sending class library implemented by golang, which can easily realize template message sending

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)
        }
    }

}

How we use it

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,
    )
}

The push effect is shown in the figure, click on the template to jump to our custom url

I also use this method to remind the customer service in my customer service system. After receiving the reminder, the customer service can reply to the visitor's message by clicking, which is very convenient.

Friends in need can visit gofly.v1kf.com to understand and test my customer service system

Guess you like

Origin blog.csdn.net/taoshihan/article/details/131990631