【Golang】golang实现发送微信服务号模板消息

在我自己的在线客服系统项目(唯一客服)中,实现了对接微信公众号的功能,并且可以调用发送模板消息接口

下面是一些简化后的代码,供大家参考

引入的包是这些

    "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"
    workConfig "github.com/silenceper/wechat/v2/work/config"

使用了内存存储access_token

var memory = cache.NewMemory()

路由部分

v2.POST("/wechatTemplate", controller.PostSendWechatTemplate)

控制器部分

//发送微信模板消息
func PostSendWechatTemplate(c *gin.Context) {
    entId := c.PostForm("ent_id")
    openid := c.PostForm("openid")
    templateId := c.PostForm("template_id")
    url := c.PostForm("url")
    keyword1 := c.PostForm("keyword1")
    keyword2 := c.PostForm("keyword2")
    keyword3 := c.PostForm("keyword3")
    wechatConfig, _ := lib.NewWechatLib(entId)

    msgData := make(map[string]*message.TemplateDataItem)
    msgData["keyword1"] = &message.TemplateDataItem{
        Value: keyword1,
        Color: "",
    }
    msgData["keyword2"] = &message.TemplateDataItem{
        Value: keyword2,
        Color: "",
    }
    msgData["keyword3"] = &message.TemplateDataItem{
        Value: keyword3,
        Color: "",
    }
    msgData["remark"] = &message.TemplateDataItem{
        Value: models.FindConfig("WechatTemplateRemark"),
        Color: "",
    }
    msg := &message.TemplateMessage{
        ToUser:     openid,
        Data:       msgData,
        TemplateID: templateId,
        URL:        url,
    }
    _, err := SendWechatTemplate(wechatConfig, msg)
    if err != nil {
        c.JSON(200, gin.H{
            "code": 400,
            "msg":  err.Error(),
        })
        return
    }
    c.JSON(200, gin.H{
        "code": 200,
        "msg":  "ok",
    })
}

函数部分

//发送微信模板消息
func SendWechatTemplate(wechatConfig *lib.Wechat, msg *message.TemplateMessage) (bool, error) {

    if wechatConfig == nil {
        return false, errors.New("该企业未配置appid等公众号资料")
    }
    if msg.TemplateID == "" || msg.ToUser == "" {
        return false, errors.New("openid或templateId不存在")
    }
    wc := wechat.NewWechat()
    cfg := &offConfig.Config{
        AppID:     wechatConfig.AppId,
        AppSecret: wechatConfig.AppSecret,
        Token:     wechatConfig.Token,
        //EncodingAESKey: "xxxx",
        Cache: memory,
    }
    officialAccount := wc.GetOfficialAccount(cfg)
    template := officialAccount.GetTemplate()

    msgId, err := template.Send(msg)
    if err != nil {
        return false, err
    }
    log.Println("发送微信模板消息:", msgId, err, msg.ToUser)
    return true, nil
}

我的公众号相关配置存储到了数据库里,根据ent_id去查的,这里大家可以酌情修改

上面只是我的代码片段,并不能直接使用,参考后进行修改

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值