微信公众号发送模板消息

微信公众号发送模板消息

一、controller方法
@RestController
@RequestMapping("/public/num")
public class SendTemplateMsg {

    @GetMapping("/sendOnlyMsg")
    public String sendOnlyMsg(String openId){
        Map<String, WeChatTemplateMsg> msgHashMap = new HashMap<>();
        msgHashMap.put("name",new WeChatTemplateMsg("张三"));

        RestTemplate accessTemplate = new RestTemplate();
        //公众号appid和秘钥
        String appid = "";
        String s= "";
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + s;
        String response = accessTemplate.getForObject(accessTokenUrl, String.class);
        JSONObject object = JSONObject.parseObject(response);
        String accessToken = object.getString("access_token");

        //发送消息的url
        String msgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
        //封装消息信息
        HashMap<String, Object> bodyMsg = new HashMap<>();
        //模板id
        bodyMsg.put("template_id","");
        //模板内容
        bodyMsg.put("data",msgHashMap);
        //openId
        bodyMsg.put("touser", openId);
      
        //设置跳转小程序页面
        JSONObject miniprogram = new JSONObject();
        //小程序的appid
        miniprogram.put("appid", Configure.appID);
        //跳转页面路径
        miniprogram.put("path", "pages/home/home");
        bodyMsg.put("miniprogram",miniprogram);

	    //url设置后是跳转H5页面
	    bodyMsg.put("url","");
        
        RestTemplate msgTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = msgTemplate.postForEntity(msgUrl,bodyMsg,String.class);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            System.out.println("推送消息异常: " + openId);
        }
        return "成功";
    }

    /**
     * 通过微信接口获得微信公众号订阅者的openid
     * @param accessToken
     * @return
     */
    public static List<String> getOpenIdsFromWX(String accessToken) {
        RestTemplate restTemplate = new RestTemplate();

        String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken;
        ResponseEntity<String> response = restTemplate.postForEntity(url, null, String.class);
        JSONObject object = JSONObject.parseObject(response.getBody());
        List<String> openIds = JSONObject.parseArray(object.getJSONObject("data").getString("openid"), String.class);
        return openIds;
    }
}
二、模板信息
@Data
public class WeChatTemplateMsg {

    /**
     * 消息
     */
    private String value;
    /**
     * 消息颜色
     */
    private String color;

    public WeChatTemplateMsg(String value) {
        this.value = value;
        this.color = "#173177";
    }

    public WeChatTemplateMsg(String value, String color) {
        this.value = value;
        this.color = color;
    }
}

一个在学习的开发者,勿喷,欢迎交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 PHP 中发送微信公众号模板消息,首先需要在微信公众平台中创建一个模板消息并获取模板 ID。接下来,您需要在 PHP 中使用 cURL 库向微信 API 发送 POST 请求,以便将模板消息发送给用户。以下是示例代码: ```php $access_token = 'YOUR_ACCESS_TOKEN'; $template_id = 'YOUR_TEMPLATE_ID'; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $data = array( 'touser' => 'OPENID', 'template_id' => $template_id, 'data' => array( 'first' => array('value' => 'Hello, world!'), 'keyword1' => array('value' => 'Keyword 1'), 'keyword2' => array('value' => 'Keyword 2'), 'remark' => array('value' => 'This is a remark.') ) ); $data_string = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) )); $result = curl_exec($ch); curl_close($ch); echo $result; ``` 上述代码中,`$access_token` 是您的公众号访问令牌,`$template_id` 是您创建的模板消息的 ID。您需要将 `OPENID` 替换为要接收模板消息的用户的 OpenID。`$data` 数组包含模板消息的详细信息,其中 `first`、`keyword1`、`keyword2` 和 `remark` 分别对应模板消息中的不同部分。最后,使用 cURL 库将 `$data` 数组作为 JSON 字符串发送微信 API,然后解析响应以查看是否成功。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值