wx-tools 消息相關(guān)API

2023-03-22 17:20 更新

包括如下接口:
- sendAllByTag - 群發(fā)-通過標(biāo)簽
- sendAllByOpenid - 群發(fā)-通過openid
- sendAllPreview - 群發(fā)預(yù)覽
- sendAllDelete - 刪除群發(fā)
- sendAllGetStatus - 查詢?nèi)喊l(fā)狀態(tài)
- templateSetIndustry - 設(shè)置模板行業(yè)
- templateGetIndustry - 查詢模板行業(yè)
- templateGetId - 獲取模板ID
- templateGetList - 獲取模板列表
- templateDelete - 刪除模板
- templateSend - 發(fā)送模板

3.4.1 sendAllByTag 通過用戶標(biāo)簽來群發(fā)

    WxTagSender sender = new WxTagSender();
    //設(shè)置哪些組需要接受群發(fā)
    sender.setFilter(new SenderFilter(true, 1));
    //群發(fā)文本內(nèi)容
    sender.setText(new Text("文本內(nèi)容"));
    try {
        SenderResult result = iService.sendAllByTag(sender);
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

如果發(fā)其他類型的消息:

    //群發(fā)圖片,以此類推
    sender.setImage(new Media("media_id"));

3.4.2 sendAllByOpenid 針對某群人的openid群發(fā)

    WxOpenidSender sender = new WxOpenidSender();
    List<String> openidList = new ArrayList<>();
    openidList.add("openid1");
    openidList.add("openid2");
    sender.setTouser(openidList);
    //群發(fā)文本內(nèi)容
    sender.setText(new Text("文本內(nèi)容"));
    try {
        SenderResult result = iService.sendAllByOpenid(sender);
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.3 sendAllPreview 群發(fā)預(yù)覽

    PreviewSender sender = new PreviewSender();
    //設(shè)置openid或者微信號,優(yōu)先級為wxname高
    sender.setTouser("openid");
    sender.setTowxname("微信號");
    //群發(fā)文本內(nèi)容
    sender.setText(new Text("文本內(nèi)容"));
    try {
        SenderResult result = iService.sendAllPreview(sender);
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.4 sendAllDelete 刪除群發(fā)

    try {
        //msg_id參數(shù)在群發(fā)接口中返回
        SenderResult result = iService.sendAllDelete("msg_id");
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.5 sendAllGetStatus 獲取群發(fā)狀態(tài)

    try {
        SenderResult result = iService.sendAllGetStatus("msg_id");
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.6 templateSetIndustry 設(shè)置模板消息的行業(yè)

    //行業(yè)代碼參考官方文檔。
    try {
        iService.templateSetIndustry("1", "4");
    } catch (WxErrorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

3.4.7 templateGetIndustry獲取模板消息的行業(yè)設(shè)置

    try {
        IndustryResult result = iService.templateGetIndustry();
        System.out.println(result.getPrimary_industry());
        System.out.println(result.getSecondary_industry());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.8 templateGetId通過短ID獲取模板ID

    //模板庫中模板的編號,有“TM**”和“OPENTMTM**”等形式
    try {
        TemplateResult result = iService.templateGetId("template_id_short");
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.9 templateGetList 獲取模板列表

    try {
        TemplateListResult result = iService.templateGetList();
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.10 templateDelete 刪除模板

    try {
        iService.templateDelete("template_id");
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

3.4.11 templateSend 模板消息發(fā)送

    TemplateSender sender = new TemplateSender();
    sender.setTouser("openid");
    sender.setTemplate_id("templateId");
    sender.setData("Object:與模板內(nèi)容對應(yīng)的對象");
    sender.setUrl("url");
    try {
        TemplateSenderResult result = iService.templateSend(sender);
        System.out.println(result.toString());
    } catch (WxErrorException e) {
        e.printStackTrace();
    }

參數(shù)說明:

  • openid需要發(fā)送給哪個用戶
  • templateId 模板ID
  • Data: 假設(shè)模板格式中有三個字段,分別為:

    • first.DATA
    • keyword1.DATA
    • remark.DATA

    那么就需要創(chuàng)建一個類, 里面包含三個成員變量.如:

    public class Template{
    private String first;
    private String keyword1;
    private String remark;
    }

  • Url 點擊模板消息時跳轉(zhuǎn)的頁面.
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號