springboot 开发微信公众号(3) 发送模板消息(附源码)

1.下载模板示例
https://wximg.gtimg.com/shake_tv/mpwiki/template_allow_sample.zip
2.新增测试模板

标题

内容

示例

退款结果通知

{{first.DATA}}

店铺名称:{{storeName.DATA}}

订单编号:{{orderId.DATA}}

订单类型:{{orderType.DATA}}

{{remark.DATA}}

您的订单1002 已退款成功。

店铺名称:示例餐厅

订单编号:1002

订单类型:外卖

订单金额:100.00元


3.构建模板消息实体类
/**通用基础实体类
 * @author
 * @version 1.0
 * @date 2020/7/29 17:02
 */
@Data
public class BaseTemplateEntity {
    public  String value;
    public String color;
}
 
/**
 * 退款结果通知实体类
 * @author
 * @version 1.0
 * @date 2020/7/29 17:03
 */
@Data
public class RefundMessage {
    private BaseTemplateEntity first;
    private BaseTemplateEntity storeName;
    private BaseTemplateEntity orderId;
    private BaseTemplateEntity orderType;
    private BaseTemplateEntity remark;
}
/**模板消息实体类
 * @author 
 * @version 1.0
 * @date 2020/7/29 17:07
 */
@Data
public class BaseTemplateMessage<T> {
    private String touser;
    private String template_id;
    private String url;
    private T data;
}
4.用实体类对象格式化示例模板消息(也可以根据JSON格式数据一步一步拼写对象)

public BaseTemplateMessage<RefundMessage> getRefundMessage(){
        String message="{\n" +
                "           \"touser\":\"oNinWtypc9eraaElge9ePVwdiq2E\",\n" +
                "           \"template_id\":\"Qww0jmJr3OvQsjpyOcemAvr_5PYse2tJaGdJ1ohHcFk\",          \n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"尊敬的客人你好\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"storeName\":{\n" +
                "                       \"value\":\"江某人店铺\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"orderId\": {\n" +
                "                       \"value\":\"DD54895\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"orderType\": {\n" +
                "                       \"value\":\"退款\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"欢迎再次购买!\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   }\n" +
                "           }\n" +
                "       }";
        BaseTemplateMessage<RefundMessage> response=JSON.parseObject(message, new TypeReference<BaseTemplateMessage<RefundMessage>>(){});
        return response;
    }
5.发送模板消息
public enum ApiEum {
    ACCESS_TOKEN_URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"),
    //ROBBOT_URL("http://api.tianapi.com/txapi/robot/index"),
    API_SET_INDUSTRY("template/api_set_industry"),
    TEMPLATE_SEND("message/template/send"),//发送模板消息
    GET_ALL_PRIVATE_TEMPLATE("template/get_all_private_template"),
    GET_INDUSTRY("template/get_industry"),
    ROBBOT_URL("http://api.tianapi.com/txapi/tuling/index"),
    BASE_URL(" https://api.weixin.qq.com/cgi-bin/"),
    DELETE_PERSONAL_MENU_URL("menu/delconditional"),//删除个性化菜单
    CREATE_PERSONALIZED_MENU_URL("menu/addconditional"),//创建个性化菜单
    MENU_GET_URL("menu/get"),//自定义菜单的查询接口
    MENU_DELETE_URL("menu/delete"),//自定义菜单删除
    MENU_CREATE_URL("menu/create");//自定义菜单创建
    private String info;
    ApiEum(String info) {
        this.info = info;
    }
    public String getInfo() {
        return info;
    }
}
 @RequestMapping("/sendTemplateMessage")
    public String sendTemplateMessage(){
        BaseTemplateMessage<RefundMessage> response=MenuUtil.me().getRefundMessage();
        return templateService.sendTemplateMessage(response);
    }
 public String sendTemplateMessage(BaseTemplateMessage<RefundMessage> baseTemplateMessage) {
        CtBossSender ctBossSender=new CtBossSender(ApiEum.TEMPLATE_SEND.getInfo());        //获取菜单结构
        String accessToken=redisTemplate.opsForValue().get(MessageTypeEnum.ACCESS_TOKEN.getInfo());
        log.info("发送模板消息请求参数为={}",JSON.toJSONString(baseTemplateMessage));
        NutMap nutMap = ctBossSender.sendHttpPost(JSON.toJSONString(baseTemplateMessage),accessToken);
        log.info("发送模板消息返回参数为={}",JSON.toJSONString(nutMap));
        return JSON.toJSONString(nutMap);
    }

ps.源码https://gitee.com/bjiangAnhui/yichengyoushaonian/tree/master/boot-wechart

发送成功截图

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值