当前位置:网站首页>[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
边栏推荐
- 在idea中System.getProperty(“user.dir“)识别到模块(module)路径的方法:Working directory的设置
- In idea, system.getproperty ("user.dir") identifies the path of the module: the setting of the working directory
- RetinaNet:Focal Loss for Dense Object Detection
- LoRa无线技术与LoRaWAN网关模块的区别
- The number of digits of power and factorial
- 这才是开发者神器正确的打开方式!
- 爬虫与反爬:一场无休止之战
- Ask n! How many zeros are there behind
- Detailed explanation of the implementation process of redistribution watchdog
- The U.S. Department of Homeland Security launched an investigation into the electronic communication records deleted by the secret service during the riots in the Capitol
猜你喜欢

This should be postman, the most complete interface testing tool in the whole network

Download path of twincat3 versions

read_ CSV error: 'GBK' codec can't decode byte 0xb4 in position 274: illegal multibyte sequence

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

08【AIO编程】

selenium3自动化测试(这一篇就够了)——自学篇

E2PROM read / write (xiicps) on PS side of zcu102 board

性能测试总结(一)---基础理论篇

Altium one key automatic BOM

STM32+ESP8266+MQTT协议连接阿里云物联网平台
随机推荐
Collation of important MySQL configuration parameters
[interview: Basics 01: integer binary search]
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
Simply use MySQL index
【10】团队协作和跨团队协作
rs485通信OSI模型网络层
Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution
变频器的四大组成部分和工作原理
「低功耗蓝牙模块」主从一体 蓝牙嗅探-助力智能门锁
Web salted fish self rescue strategy -- typescript classes are not as difficult as you think
High speed ADC test experience
【Golang】golang中map元素的删除和清空
Conversion between hexadecimal string and byte array
基于NoCode构建简历编辑器
Data visualization - White Snake 2: black snake robbery (1)
[white hat talks about web security] Chapter 2 browser security
《Nature》论文插图复刻第3期—面积图(Part2-100)
【Golang】golang实现md5加密函数
Idea hidden Idea folder hides.Iml files
JMeter接口测试步骤-安装教程-脚本录制-并发测试