当前位置:网站首页>[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
边栏推荐
- UNIX C language POSIX mutex thread synchronization
- This should be postman, the most complete interface testing tool in the whole network
- 07【Path、Files类的使用】
- 周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
- 如何在家中使用 SSH 和 SFTP 协议
- Publish local image to private library
- 爬虫与反爬:一场无休止之战
- 2022, the average salary of the soft tester, after reading it, I was instantly cool
- Data visualization - White Snake 2: black snake robbery (1)
- 在idea中System.getProperty(“user.dir“)识别到模块(module)路径的方法:Working directory的设置
猜你喜欢

变频器的四大组成部分和工作原理

Why can't memset initialize array elements to 1?

08【AIO编程】

网络爬虫之短信验证

Cub school learning - Kernel Development

轻松读懂三极管,原来它是这样工作的

在idea中System.getProperty(“user.dir“)识别到模块(module)路径的方法:Working directory的设置

Working principle and function application of frequency converter

MySQL engine

爬虫与反爬:一场无休止之战
随机推荐
Take care of me when I meet you for the first time
【直播报名】Location Cache 模块浅析及 OCP 监控、报警详解
Mockito3.8 how to mock static methods (how to mock PageHelper)
【白帽子讲Web安全】第一章 我的安全世界观
[FPGA]: IP core -- xadc
UNIX C language POSIX thread creation, obtaining thread ID, merging thread, separating thread, terminating thread, thread comparison
Altium one key automatic BOM
Capture and handling of JDBC exception sqlexception
UNIX C language POSIX mutex thread synchronization
How to convert word to markdown text
Kubernetes Foundation
Only "a little bit", why do developers look up to you?
如何给自己的网站接入在线客服系统代码
【Golang】golang实现简单memcache
关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈
Self taught software testing talent -- not covered
CSDN blog removes the uploaded image watermark
西门子200smart自创库与说明
07【Path、Files类的使用】
对话ACE第四期:分布式数据库未来发展的挑战和机遇