package miniprogram import ( "fmt" "github.com/go-resty/resty/v2" ) type SendSubscribeMessageResponse struct { Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` } type SendSubscribeMessageRequest struct { Touser string `json:"touser"` TemplateID string `json:"template_id"` Page string `json:"page"` MiniprogramState string `json:"miniprogram_state"` Lang string `json:"lang"` Data struct { Thing1 struct { Value string `json:"value"` } `json:"thing1"` Time2 struct { Value string `json:"value"` } `json:"time2"` Thing3 struct { Value string `json:"value"` } `json:"thing3"` } `json:"data"` } // SendSubscribeMessage 发送订阅消息 // DOC: https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html func SendSubscribeMessage(accessToken string, requestData *SendSubscribeMessageRequest, resultStruct interface{}) error { response, err := resty.New().R(). ForceContentType("application/json"). SetHeader("Content-Type", "application/json"). SetBody(requestData). SetResult(resultStruct). Post(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s", accessToken)) if err != nil { return err } // 检查响应状态码 if response.IsError() { return fmt.Errorf("服务异常(%d)", response.StatusCode()) } // 检查响应结果 if response.Result().(*SendSubscribeMessageResponse).Errcode != 0 { return fmt.Errorf("服务异常(%d): %s", response.Result().(*SendSubscribeMessageResponse).Errcode, response.Result().(*SendSubscribeMessageResponse).Errmsg) } return nil }