royalrick / weapp

微信小程序服务端 SDK (for Golang)
https://pkg.go.dev/github.com/medivhzhan/weapp/v3
Apache License 2.0
1.47k stars 281 forks source link

SubscribeMessage 发送订阅消息,Data 类型是不是错了 #54

Closed zzir closed 3 years ago

zzir commented 3 years ago

文档里的 SubscribeMessageData{} 结构没有找到,当把消息模版参数转换为字符给 data 时,消息发送失败

royalrick commented 3 years ago

V3版本还是测试版本,接口还没整理完。Data已经改为字符串了,使用起来更方便,因为官方要求的也是字符串。建议现在使用V2版本

royalrick commented 3 years ago

V3版本可以通过这种方式使用:


import "medivhzhan/weapp/v3"

cli:= weapp.NewClient("appid","secret")

submsg := cli.NewSubscribeMessage()

submsg.send(&subscribemessage.SendRequest{})
zzir commented 3 years ago

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0

看到官方文档 data 类型是 Object,尝试

submsg.Send(&subscribemessage.SendRequest{
        ToUser:           "todo",
        TemplateID:       "todo",
        Page:             "todo",
        MiniprogramState: "todo",
        Data:             "{ \"key1\": { \"value\": any }, \"key2\": { \"value\": any } }",
    })

会报错找不到 key

royalrick commented 3 years ago

用map把内容Marshal为JSON字符串再赋值给Data

royalrick commented 3 years ago

然后确认传入的模板相关数据都是正确的