Skip to content

Commit

Permalink
feat: 公众号新增模板接口,支持模板参数传递 (#783)
Browse files Browse the repository at this point in the history
* feat: 公众号新增模板接口,支持模板参数传递

* fix: 公众号新增模板接口,合并定义参数

* fix: 公众号新增模板接口,统一代码风格

---------

Co-authored-by: yangj23 <[email protected]>
  • Loading branch information
qietugou and yangj23 authored Jul 16, 2024
1 parent 6b3532c commit 05ac714
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions officialaccount/message/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) {
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
var (
uri = fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
response []byte
)
if response, err = util.PostJSON(uri, msg); err != nil {
return
}
var result resTemplateSend
err = json.Unmarshal(response, &result)
if err != nil {
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.ErrCode != 0 {
Expand Down Expand Up @@ -103,10 +103,11 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) {
if err != nil {
return
}
uri := fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
var (
uri = fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
response []byte
)
if response, err = util.HTTPGet(uri); err != nil {
return
}
var res resTemplateList
Expand All @@ -121,22 +122,23 @@ type resTemplateAdd struct {
}

// Add 添加模板.
func (tpl *Template) Add(shortID string) (templateID string, err error) {
func (tpl *Template) Add(shortID string, keyNameList []string) (templateID string, err error) {
var accessToken string
accessToken, err = tpl.GetAccessToken()
if err != nil {
return
}
var msg = struct {
ShortID string `json:"template_id_short"`
}{ShortID: shortID}
uri := fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
var (
msg = struct {
ShortID string `json:"template_id_short"`
KeyNameList []string `json:"keyword_name_list"`
}{ShortID: shortID, KeyNameList: keyNameList}
uri = fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
response []byte
)
if response, err = util.PostJSON(uri, msg); err != nil {
return
}

var result resTemplateAdd
err = util.DecodeWithError(response, &result, "AddTemplate")
return result.TemplateID, err
Expand All @@ -149,14 +151,14 @@ func (tpl *Template) Delete(templateID string) (err error) {
if err != nil {
return
}
var msg = struct {
TemplateID string `json:"template_id"`
}{TemplateID: templateID}

uri := fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, msg)
if err != nil {
var (
msg = struct {
TemplateID string `json:"template_id"`
}{TemplateID: templateID}
uri = fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
response []byte
)
if response, err = util.PostJSON(uri, msg); err != nil {
return
}
return util.DecodeWithCommonError(response, "DeleteTemplate")
Expand Down

0 comments on commit 05ac714

Please sign in to comment.