当前位置:网站首页>[golang] golang realizes sending wechat service number template messages
[golang] golang realizes sending wechat service number template messages
2022-07-24 11:11:00 【Taoshihan】
In my own Online customer service system project ( Sole customer service ) in , It realizes the function of docking with wechat official account , And you can call the send template message interface
Here are some simplified code , For your reference
These packages are introduced
"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"Memory storage is used access_token
var memory = cache.NewMemory()Routing part
v2.POST("/wechatTemplate", controller.PostSendWechatTemplate)Controller part
// Send wechat template message
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",
})
}Function part
// Send wechat template message
func SendWechatTemplate(wechatConfig *lib.Wechat, msg *message.TemplateMessage) (bool, error) {
if wechatConfig == nil {
return false, errors.New(" The enterprise is not configured appid Wait for official account information ")
}
if msg.TemplateID == "" || msg.ToUser == "" {
return false, errors.New("openid or templateId non-existent ")
}
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(" Send wechat template message :", msgId, err, msg.ToUser)
return true, nil
}The relevant configuration of my official account is stored in the database , according to ent_id Check it out , Here you can modify as appropriate
The above is just a snippet of my code , It can't be used directly , Modify after reference
边栏推荐
- Conversion between hexadecimal string and byte array
- Publish local images to Alibaba cloud
- HDU5667 Sequence
- STM32+ESP8266+MQTT协议连接阿里云物联网平台
- [FPGA]: use of MicroBlaze
- Fifty lectures of Euler (I)
- Xilinx FPGA Microblaze AXI_ IIC usage and experience
- Data visualization - White Snake 2: black snake robbery (1)
- 1184. Distance between bus stops: simple simulation problem
- 【C】 Understanding C language variable scope and life cycle from memory
猜你喜欢

Data visualization - White Snake 2: black snake robbery (1)

Self taught software testing talent -- not covered

The difference between Lora wireless technology and lorawan gateway module

Publish local images to Alibaba cloud

Idea hidden Idea folder hides.Iml files

Fifty lectures of Euler (I)

Introduction to kubernetes Basics

【白帽子讲Web安全】第二章 浏览器安全

Analysis of Lagrange multiplier method and its duality

Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
随机推荐
[interview: Basics 04: insert order]
"Low power Bluetooth module" master-slave integrated Bluetooth sniffer - help smart door lock
基于NoCode构建简历编辑器
在线客服聊天系统源码_美观强大golang内核开发_二进制运行傻瓜式安装_附搭建教程
Druid encryption command
Installing MySQL under Linux
Only "a little bit", why do developers look up to you?
MySQL queries tables and fields according to comments
Analysis of Lagrange multiplier method and its duality
Development and course of Bluetooth Technology
openresty lua-resty-logger-socket日志传输
SQL optimization skills and precautions
Value and technical thinking of vectorization engine for HTAP
「低功耗蓝牙模块」主从一体 蓝牙嗅探-助力智能门锁
This should be postman, the most complete interface testing tool in the whole network
Build resume editor based on Nocode
Artifact ffmpeg - operation video, extremely comfortable
selenium3自动化测试(这一篇就够了)——自学篇
Yum installation prompt to protect multi library version
Fifty lectures of Euler (I)