File tree Expand file tree Collapse file tree 3 files changed +41
-6
lines changed
main/java/me/chanjar/weixin/mp/api
test/java/me/chanjar/weixin/mp/api/impl Expand file tree Collapse file tree 3 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -46,4 +46,15 @@ public interface WxMpTemplateMsgService {
46
46
*/
47
47
String sendTemplateMsg (WxMpTemplateMessage templateMessage ) throws WxErrorException ;
48
48
49
+ /**
50
+ * <pre>
51
+ * 获得模板ID
52
+ * 从行业模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成
53
+ * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
54
+ * 接口地址格式:https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
55
+ * </pre>
56
+ *@param shortTemplateId 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
57
+ * @return templateId 模板Id
58
+ */
59
+ String addTemplate (String shortTemplateId ) throws WxErrorException ;
49
60
}
Original file line number Diff line number Diff line change 2
2
3
3
import com .google .gson .JsonObject ;
4
4
import com .google .gson .JsonParser ;
5
+
5
6
import me .chanjar .weixin .common .bean .result .WxError ;
6
7
import me .chanjar .weixin .common .exception .WxErrorException ;
7
8
import me .chanjar .weixin .mp .api .WxMpService ;
@@ -55,4 +56,18 @@ public WxMpIndustry getIndustry() throws WxErrorException {
55
56
return WxMpIndustry .fromJson (responseContent );
56
57
}
57
58
59
+ @ Override
60
+ public String addTemplate (String shortTemplateId ) throws WxErrorException {
61
+ String url = API_URL_PREFIX + "/api_add_template" ;
62
+ JsonObject jsonObject = new JsonObject ();
63
+ jsonObject .addProperty ("template_id_short" , shortTemplateId );
64
+ String responseContent = this .wxMpService .post (url , jsonObject .toString ());
65
+ final JsonObject result = JSON_PARSER .parse (responseContent ).getAsJsonObject ();
66
+ if (result .get ("errcode" ).getAsInt () == 0 ) {
67
+ return result .get ("template_id" ).getAsString ();
68
+ }
69
+
70
+ throw new WxErrorException (WxError .fromJson (responseContent ));
71
+ }
72
+
58
73
}
Original file line number Diff line number Diff line change 1
1
package me .chanjar .weixin .mp .api .impl ;
2
2
3
+ import java .text .SimpleDateFormat ;
4
+ import java .util .Date ;
5
+
6
+ import org .testng .Assert ;
7
+ import org .testng .annotations .Guice ;
8
+ import org .testng .annotations .Test ;
9
+
3
10
import com .google .inject .Inject ;
11
+
4
12
import me .chanjar .weixin .common .exception .WxErrorException ;
5
13
import me .chanjar .weixin .mp .api .ApiTestModule ;
6
14
import me .chanjar .weixin .mp .api .WxXmlMpInMemoryConfigStorage ;
7
15
import me .chanjar .weixin .mp .bean .WxMpIndustry ;
8
16
import me .chanjar .weixin .mp .bean .WxMpTemplateData ;
9
17
import me .chanjar .weixin .mp .bean .WxMpTemplateMessage ;
10
- import org .testng .Assert ;
11
- import org .testng .annotations .Guice ;
12
- import org .testng .annotations .Test ;
13
-
14
- import java .text .SimpleDateFormat ;
15
- import java .util .Date ;
16
18
17
19
/**
18
20
* <pre>
@@ -56,4 +58,11 @@ public void testSetIndustry() throws Exception {
56
58
Assert .assertTrue (result );
57
59
}
58
60
61
+ @ Test
62
+ public void testAddTemplate () throws Exception {
63
+ String result = this .wxService .getTemplateMsgService ().addTemplate ("TM00015" );
64
+ Assert .assertNotNull (result );
65
+ System .err .println (result );
66
+ }
67
+
59
68
}
You can’t perform that action at this time.
0 commit comments