Skip to content

Commit a054560

Browse files
authored
🎨 #3078 【公众号】模板消息获得模板ID的接口增加选用类目模板的关键词的参数
1 parent 7534975 commit a054560

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpTemplateMsgService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ public interface WxMpTemplateMsgService {
6666
*/
6767
String addTemplate(String shortTemplateId) throws WxErrorException;
6868

69+
/**
70+
* <pre>
71+
* 获得模板ID
72+
* 从类目模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成
73+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
74+
* 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
75+
* </pre>
76+
*
77+
* @param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式,对于类目模板,为纯数字ID
78+
* @param keywordNameList 选用的类目模板的关键词,按顺序传入,如果为空,或者关键词不在模板库中,会返回40246错误码
79+
* @return templateId 模板Id
80+
* @throws WxErrorException .
81+
*/
82+
String addTemplate(String shortTemplateId, List<String> keywordNameList) throws WxErrorException;
83+
6984
/**
7085
* <pre>
7186
* 获取模板列表

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpTemplateMsgServiceImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3+
import com.google.gson.Gson;
34
import com.google.gson.JsonObject;
45
import lombok.RequiredArgsConstructor;
56
import me.chanjar.weixin.common.api.WxConsts;
@@ -68,6 +69,21 @@ public String addTemplate(String shortTemplateId) throws WxErrorException {
6869
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
6970
}
7071

72+
@Override
73+
public String addTemplate(String shortTemplateId, List<String> keywordNameList) throws WxErrorException {
74+
JsonObject jsonObject = new JsonObject();
75+
Gson gson = new Gson();
76+
jsonObject.addProperty("template_id_short", shortTemplateId);
77+
jsonObject.addProperty("keyword_name_list",gson.toJson(keywordNameList));
78+
String responseContent = this.wxMpService.post(TEMPLATE_API_ADD_TEMPLATE, jsonObject.toString());
79+
final JsonObject result = GsonParser.parse(responseContent);
80+
if (result.get(WxConsts.ERR_CODE).getAsInt() == 0) {
81+
return result.get("template_id").getAsString();
82+
}
83+
84+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
85+
}
86+
7187
@Override
7288
public List<WxMpTemplate> getAllPrivateTemplate() throws WxErrorException {
7389
return WxMpTemplate.fromJson(this.wxMpService.get(TEMPLATE_GET_ALL_PRIVATE_TEMPLATE, null));

0 commit comments

Comments
 (0)