4.4)消息相关API
包括如下接口:
- sendAllByGroup - 群发-通过组
- sendAllByOpenid - 群发-通过openid
- sendAllPreview - 群发预览
- sendAllDelete - 删除群发
- sendAllGetStatus - 查询群发状态
- templateSetIndustry - 设置模板行业
- templateGetIndustry - 查询模板行业
- templateGetId - 获取模板ID
- templateGetList - 获取模板列表
- templateDelete - 删除模板
- templateSend - 发送模板
1. sendAllByGroup 通过用户组来群发
WxGroupSender sender = new WxGroupSender();
//设置哪些组需要接受群发
sender.setFilter(new SenderFilter(true, 1));
//群发文本内容
sender.setText(new Text("文本内容"));
//群发图片,以此类推
sender.setImage(new Media("media_id"));
try {
SenderResult result = iService.sendAllByGroup(new WxGroupSender());
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2. sendAllByOpenid 针对某群人的openid群发
WxOpenidSender sender = new WxOpenidSender();
List<String> openidList = new ArrayList<>();
openidList.add("openid1");
openidList.add("openid2");
sender.setTouser(openidList);
//群发文本内容
sender.setText(new Text("文本内容"));
//群发图片,以此类推
sender.setImage(new Media("media_id"));
try {
SenderResult result = iService.sendAllByOpenid(sender);
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3. sendAllPreview 群发预览
PreviewSender sender = new PreviewSender();
//设置openid或者微信号,优先级为wxname高
sender.setTouser("openid");
sender.setTowxname("微信号");
//群发文本内容
sender.setText(new Text("文本内容"));
//群发图片,以此类推
sender.setImage(new Media("media_id"));
try {
SenderResult result = iService.sendAllPreview(sender);
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
4. sendAllDelete 删除群发
try {
//此参数在发送接口 返回
SenderResult result = iService.sendAllDelete("msg_id");
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5. sendAllGetStatus 获取群发状态
try {
SenderResult result = iService.sendAllGetStatus("msg_id");
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
6. templateSetIndustry 设置模板消息的行业
//行业代码参考官方文档。
try {
iService.templateSetIndustry("1", "4");
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
7. templateGetIndustry获取模板消息的行业设置
try {
IndustryResult result = iService.templateGetIndustry();
System.out.println(result.getPrimary_industry());
System.out.println(result.getSecondary_industry());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
8. templateGetId通过短ID获取模板ID
//模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
try {
TemplateResult result = iService.templateGetId("template_id_short");
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
9. templateGetList 获取模板列表
try {
TemplateListResult result = iService.templateGetList();
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
10. templateDelete 删除模板
try {
iService.templateDelete("template_id");
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
11. templateSend 模板消息发送
TemplateSender sender = new TemplateSender();
sender.setTouser("openid");
sender.setTemplate_id("templateId");
sender.setData("Object:与模板内容对应的对象");
sender.setUrl("url");
try {
TemplateSenderResult result = iService.templateSend(sender);
System.out.println(result.toString());
} catch (WxErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}