Implement template management and template message sending for WXA.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.foxinmy.weixin4j.wxa.api;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
public class AddTemplateParameterTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
AddTemplateParameter param = new AddTemplateParameter("AT0002", new int[] { 3, 4, 5 });
|
||||
String json = JSON.toJSONString(param);
|
||||
assertEquals("{\"id\":\"AT0002\",\"keyword_id_list\":[3,4,5]}", json);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.foxinmy.weixin4j.wxa.api;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.util.IOUtil;
|
||||
|
||||
public class TemplateListResultTest {
|
||||
|
||||
@Test
|
||||
public void testTemplatesInLibrary() throws IOException {
|
||||
TemplateListResult r = readTemplateList("TemplateListInLibrary.json");
|
||||
assertEquals(0, r.getErrCode());
|
||||
assertEquals("ok", r.getErrMsg());
|
||||
assertEquals(599, r.getTotalCount().longValue());
|
||||
|
||||
assertEquals(5, r.getList().size());
|
||||
|
||||
assertEquals("AT0002", r.getList().get(0).getId());
|
||||
assertEquals("购买成功通知", r.getList().get(0).getTitle());
|
||||
|
||||
assertEquals("AT0003", r.getList().get(1).getId());
|
||||
assertEquals("购买失败通知", r.getList().get(1).getTitle());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMyTemplates() throws IOException {
|
||||
TemplateListResult r = readTemplateList("TemplateList.json");
|
||||
assertEquals(0, r.getErrCode());
|
||||
assertEquals("ok", r.getErrMsg());
|
||||
assertEquals("wDYzYZVxobJivW9oMpSCpuvACOfJXQIoKUm0PY397Tc", r.getList().get(0).getId());
|
||||
assertEquals("购买成功通知", r.getList().get(0).getTitle());
|
||||
assertEquals("购买地点{{keyword1.DATA}}\n购买时间{{keyword2.DATA}}\n物品名称{{keyword3.DATA}}\n", r.getList().get(0).getContent());
|
||||
assertEquals("购买地点:TIT造舰厂\n购买时间:2016年6月6日\n物品名称:咖啡\n", r.getList().get(0).getExample());
|
||||
}
|
||||
|
||||
private TemplateListResult readTemplateList(String resourceName) throws IOException {
|
||||
InputStream inputStream = getClass().getResourceAsStream(resourceName);
|
||||
try {
|
||||
TemplateListResult tl = JSON.parseObject(inputStream, TemplateListResult.class);
|
||||
return tl;
|
||||
} finally {
|
||||
IOUtil.close(inputStream);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.foxinmy.weixin4j.wxa.api;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.wxa.api.TemplateMessageParameter.TemplateMessageData;
|
||||
|
||||
public class TemplateMessageParameterTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TemplateMessageParameter param = new TemplateMessageParameter();
|
||||
param.setToUser("OPENID");
|
||||
param.setTemplateId("TEMPLATE_ID");
|
||||
param.setPage("index");
|
||||
param.setFormId("FORMID");
|
||||
Map<String, TemplateMessageData> data = new LinkedHashMap<String, TemplateMessageData>();
|
||||
data.put("keyword1", new TemplateMessageData("339208499"));
|
||||
data.put("keyword2", new TemplateMessageData("2015年01月05日 12:30"));
|
||||
data.put("keyword3", new TemplateMessageData("粤海喜来登酒店"));
|
||||
data.put("keyword4", new TemplateMessageData("广州市天河区天河路208号"));
|
||||
param.setData(data);
|
||||
param.setEmphasisKeyword("keyword1.DATA");
|
||||
String json = JSON.toJSONString(param);
|
||||
System.out.println(json);
|
||||
assertTrue(json.contains("\"touser\":"));
|
||||
assertTrue(json.contains("\"template_id\":"));
|
||||
assertTrue(json.contains("\"page\":"));
|
||||
assertTrue(json.contains("\"form_id\":"));
|
||||
assertTrue(json.contains("\"data\":{\"keyword1\":{\"value\":\"339208499\"},\"keyword2\":{\"value\":\"2015年01月05日 12:30\"},\"keyword3\":{\"value\":\"粤海喜来登酒店\"},\"keyword4\":{\"value\":\"广州市天河区天河路208号\"}}"));
|
||||
assertTrue(json.contains("\"emphasis_keyword\":"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.foxinmy.weixin4j.wxa.api;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.foxinmy.weixin4j.util.IOUtil;
|
||||
|
||||
public class TemplateResultTest {
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
TemplateResult t = readTemplate();
|
||||
assertEquals(0, t.getErrCode());
|
||||
assertEquals("ok", t.getErrMsg());
|
||||
assertEquals("AT0002", t.getId());
|
||||
assertEquals("购买成功通知", t.getTitle());
|
||||
assertEquals(3, t.getKeywords().size());
|
||||
assertEquals(3, t.getKeywords().get(0).getId());
|
||||
assertEquals("购买地点", t.getKeywords().get(0).getName());
|
||||
assertEquals("TIT造舰厂", t.getKeywords().get(0).getExample());
|
||||
}
|
||||
|
||||
private TemplateResult readTemplate() throws IOException {
|
||||
InputStream inputStream = getClass().getResourceAsStream("Template.json");
|
||||
try {
|
||||
TemplateResult t = JSON.parseObject(inputStream, TemplateResult.class);
|
||||
return t;
|
||||
} finally {
|
||||
IOUtil.close(inputStream);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"errcode": 0,
|
||||
"errmsg": "ok",
|
||||
"id": "AT0002",
|
||||
"title": "购买成功通知",
|
||||
"keyword_list": [
|
||||
{
|
||||
"keyword_id": 3,
|
||||
"name": "购买地点",
|
||||
"example": "TIT造舰厂"
|
||||
},
|
||||
{
|
||||
"keyword_id": 4,
|
||||
"name": "购买时间",
|
||||
"example": "2016年6月6日"
|
||||
},
|
||||
{
|
||||
"keyword_id": 5,
|
||||
"name": "物品名称",
|
||||
"example": "咖啡"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"errcode": 0,
|
||||
"errmsg": "ok",
|
||||
"list": [
|
||||
{
|
||||
"template_id": "wDYzYZVxobJivW9oMpSCpuvACOfJXQIoKUm0PY397Tc",
|
||||
"title": "购买成功通知",
|
||||
"content": "购买地点{{keyword1.DATA}}\n购买时间{{keyword2.DATA}}\n物品名称{{keyword3.DATA}}\n",
|
||||
"example": "购买地点:TIT造舰厂\n购买时间:2016年6月6日\n物品名称:咖啡\n"
|
||||
}
|
||||
]
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"errcode": 0,
|
||||
"errmsg": "ok",
|
||||
"list": [
|
||||
{
|
||||
"id": "AT0002",
|
||||
"title": "购买成功通知"
|
||||
},
|
||||
{
|
||||
"id": "AT0003",
|
||||
"title": "购买失败通知"
|
||||
},
|
||||
{
|
||||
"id": "AT0004",
|
||||
"title": "交易提醒"
|
||||
},
|
||||
{
|
||||
"id": "AT0005",
|
||||
"title": "付款成功通知"
|
||||
},
|
||||
{
|
||||
"id": "AT0006",
|
||||
"title": "付款失败通知"
|
||||
}
|
||||
],
|
||||
"total_count": 599
|
||||
}
|
||||
Reference in New Issue
Block a user