当前位置:网站首页>【Golang】golang实现发送微信服务号模板消息
【Golang】golang实现发送微信服务号模板消息
2022-07-24 11:06:00 【陶士涵】
在我自己的在线客服系统项目(唯一客服)中,实现了对接微信公众号的功能,并且可以调用发送模板消息接口
下面是一些简化后的代码,供大家参考
引入的包是这些
"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去查的,这里大家可以酌情修改
上面只是我的代码片段,并不能直接使用,参考后进行修改
边栏推荐
- Cookie sessionstorage localstorage differences
- rs485通信OSI模型网络层
- Self taught software testing talent -- not covered
- [FPGA]: IP core - multiplier
- Linux redis download and installation
- Data visualization - White Snake 2: black snake robbery (1)
- Redis cluster setup
- Openresty Lua resty logger socket log transfer
- Golang Li Kou leetcode 494. goals and
- Docker builds MySQL master-slave replication
猜你喜欢

爬虫与反爬:一场无休止之战

自学软件测试天赋异禀——不是盖的

Use Modelsim to independently simulate Altera and Xilinx IP cores

Zero basis learning canoe panel (5) -- change the value of the variable, and the control image also changes. What's going on?

数据可视化-《白蛇2:青蛇劫起》(1)
![[micro service] eureka+ribbon realizes registration center and load balancing](/img/a7/57e28ce146270524774fd5d3304d96.png)
[micro service] eureka+ribbon realizes registration center and load balancing

西门子200smart自创库与说明

Five network IO models

The difference between Lora wireless technology and lorawan gateway module

read_ CSV error: 'GBK' codec can't decode byte 0xb4 in position 274: illegal multibyte sequence
随机推荐
Docker builds MySQL master-slave replication
1184. Distance between bus stops: simple simulation problem
Zero basic learning canoe panel (9) -- combobox
Value and technical thinking of vectorization engine for HTAP
自动推理的逻辑06--谓词演算
「低功耗蓝牙模块」主从一体 蓝牙嗅探-助力智能门锁
JS tree structure, find out the parent set of each layer it belongs to according to the ID of the inner layer
Detailed explanation and example demonstration of Modbus RTU communication protocol
小熊派学习——内核开发
Read the triode easily. It turns out that it works like this
Siemens 200smart self created library and description
Robot Framework官方教程(一)入门
FastCGI运行原理及php-fpm参数配置
Mockito3.8 how to mock static methods (how to mock PageHelper)
Neo4j installation tutorial
关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈
BBR and queuing
[white hat talks about web security] Chapter 2 browser security
Installing Oracle Xe with Linux
Linux redis download and installation