首页 > 编程知识 正文

微信公众号模板,微信公众号模板消息

时间:2023-05-05 00:07:06 阅读:140959 作者:3975

我们开发微信公众号的时候,发送模板消息往往是不可缺少的功能。 今天我们谈谈吧!

1、申请模板短信,首先要知道模板短信是需要申请的。 这个申请对本身来说是easy。 (我前一天晚上申请的,显示需要2--3个工作日,第二天早上发现已经开通了,腾讯政府还在发力。)

但是,我们在申请时有应该注意的事情。 这在官方文件中有非常详细的说明。

这个建议仔细看看。 选择行业时请小心。 这是因为一个月只能修改一次。

那么,看看在哪里申请吧?

这里已经申请了。

申请后耐心等待,审查通过后功能栏会出现模板信息菜单。 请看我上面的截图。 在第三个项目上。

2、添加模板消息审核通过后,即可添加模板消息进行开发。

这很简单:

我们点击模板消息进入后,直接在模板库中选择你需要的消息模板添加即可。 加起来就在我的模板里。 有模板id。 此模板id在发送消息时使用。

3、开发消息发送功能,接下来我们来看看如何发送模板消息:

这是官方文档。 https://mp.weixin.qq.com/wiki? t=resource/RES _ main id=MP 1433751277

我也谈谈我的实现吧。 为了方便,直接粘贴相关代码。

文档将显示接口地址,如下所示:

http请求方式: post https://API.weixin.QQ.com/CGI-bin/message/template/send? access_token=ACCESS_TOKEN这里首先需要的是access_token,这个在这里就不多说了。 可以用你的appid和secret得到。

【获取access _ token :https://MP.weixin.QQ.com/wiki吗? t=resource/RES _ mainid=MP 1421140183】

关于相关参数,我直接贴了官方文件(文件写得很清楚)。

开机自检数据的例子如下所示。

{ 'touser':'OPENID ',' template _ id ' : ' ngqipbwh8bufcssecmogfxcv 14 j0 tqlepbo 27 ize yty ',' URL ' : ' : } 恭喜您成功购买foo=bar' } ' '、' data ' : { ' first ' : { ' value ' : }! ',' color':'#173177' },' keyword1':{ 'value': '巧克力',' color':'#173177' },' keywordd1' ' keyword3' : { ' value ' : ' 2014年9月22日',' color':'#173177' },' Remaaa,' color ' 3360 ' # 173173177 '

参数是否必需说明touser表示收件人openidtemplate_id是模板IDurl模板跳转链接(国外帐户没有跳转能力)的miniprogram跳转小程序所需的数据。 不需要跳转的小应用程序是可以不传递的小应用程序appid ) )该小应用程序appid必须与传送模板消息的公众号相关联(foo=bar ),小游戏数据是模板数据

填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。

返回码说明

在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:

{ "errcode":0, "errmsg":"ok", "msgid":200228332 }

相信看完以上文档,基本上没有什么问题了。

以下是我的部分代码:

// 获取token String token = saveAndFlushAccessTokenUtil.getToken(); String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token; JSONObject jsonObject = new JSONObject(); jsonObject.put("touser", "发送到用户的openid"); // openid jsonObject.put("template_id", "你的模板id"); jsonObject.put("url", "http://www.baidu.com"); JSONObject data = new JSONObject(); JSONObject first = new JSONObject(); first.put("value", "hello"); first.put("color", "#173177"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", "hello"); keyword1.put("color", "#173177"); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "hello"); keyword2.put("color", "#173177"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", "hello"); keyword3.put("color", "#173177"); JSONObject remark = new JSONObject(); remark.put("value", "hello"); remark.put("color", "#173177"); data.put("first",first); data.put("keyword1",keyword1); data.put("keyword2",keyword2); data.put("keyword3",keyword3); data.put("remark",remark); jsonObject.put("data", data); String string = HttpClientUtils.sendPostJsonStr(postUrl, jsonObject.toJSONString()); JSONObject result = JSON.parseObject(string); int errcode = result.getIntValue("errcode"); if(errcode == 0){ // 发送成功 System.out.println("发送成功"); } else { // 发送失败 System.out.println("发送失败"); }

下面是http请求工具类:

package car.repair.common.util;import lombok.extern.slf4j.Slf4j;import org.apache.http.HttpEntity;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.IOException;/** * @author zhuzhe * @date 2017/12/11 * HttpClient工具类 */@Slf4jpublic class HttpClientUtils { /** * 以jsonString形式发送HttpPost的Json请求,String形式返回响应结果 * * @param url * @param jsonString * @return */ public static String sendPostJsonStr(String url, String jsonString) throws IOException { if (jsonString == null || jsonString.isEmpty()) { return sendPost(url); } String resp = ""; StringEntity entityStr = new StringEntity(jsonString, ContentType.create("text/plain", "UTF-8")); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entityStr); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (ClientProtocolException e) { log.error(e.getMessage()); } catch (IOException e) { log.error(e.getMessage()); } finally { if (response != null) { try { response.close(); } catch (IOException e) { log.error(e.getMessage()); } } } if (resp == null || resp.equals("")) { return ""; } return resp; } /** * 发送不带参数的HttpPost请求 * * @param url * @return */ public static String sendPost(String url) throws IOException { // 1.获得一个httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); // 2.生成一个post请求 HttpPost httppost = new HttpPost(url); CloseableHttpResponse response = null; try { // 3.执行get请求并返回结果 response = httpclient.execute(httppost); } catch (IOException e) { log.error(e.getMessage()); } // 4.处理结果,这里将结果返回为字符串 HttpEntity entity = response.getEntity(); String result = null; try { result = EntityUtils.toString(entity); } catch (ParseException | IOException e) { log.error(e.getMessage()); } return result; }}

 

收到消息,我就不自己弄图了。这里附上官方图片一张:

 

转载请务必保留此出处(原作者):https://blog.csdn.net/zhuzhezhuzhe1/article/details/83927016

 

版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

https://blog.csdn.net/zhuzhezhuzhe1

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。