公众号模板消息

洛塔服务号回复006获取代码。

功能说明

之前发布通知,要用订阅通知替代模板消息,不知道是被骂的太惨还是技术原因,模板消息还是一直能用。

首次使用的时候,需要先申请开通。在 +新的功能(后台左侧最下方)处,点击能开到带开通的。

开通后,广告与服务中,能看到模板消息。进入后可以手动设置行业、添加模板、移除模板等操作,这些操作也是可以通过接口来实现的,但如果不是用三方,感觉基本用不到。

发送模板消息

请求参数及含义

  • touser : 接收模板消息用户的openid,测试的话可以用上一篇中收到推送消息的FromUserName(就是自己的openid)
  • template_id:模板id,后台添加模板成功后能看到
  • url: 点击消息后跳转的链接。如果miniprogram则失效(跳转url或者小程序,只能跳转到一个)
  • miniprogram:点击消息后跳转的小程序
  • data:推送消息模板的参数
  • client_msg_id:防重入id,可以不填

格式如下

      {
           "touser":"接收模板消息的用户openid",
           "template_id":"模板id",
           "url":"链接,如果有miniprogram则失效",  
           "miniprogram":{
             "appid":"小程序的openid,必须是已关联的",
             "pagepath":"小程序路径,可以带参数"
           },
           "client_msg_id":"MSG_000001",
           "data":{
                   "first": {
                       "value":"恭喜你购买成功!",
                       "color":"#173177"
                   },
                   "keyword1":{
                       "value":"巧克力",
                       "color":"#173177"
                   },
                   "keyword2": {
                       "value":"39.8元",
                       "color":"#173177"
                   },
                   "keyword3": {
                       "value":"2014年9月22日",
                       "color":"#173177"
                   },
                   "remark":{
                       "value":"欢迎再次购买!",
                       "color":"#173177"
                   }
           }
       }

发送模板代码

		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		String accessToken = JSON.parseObject(result).getString("access_token");
		// 发送模板消息
		url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
		JSONObject param = new JSONObject();
		param.put("touser", "ohUHp6iaFJq6SISTVwHS5lkb9Pb8"); //接收人的openid
		param.put("template_id", "LZJq4j3ERx_IT0JTw06X0v1lIDZ2Hu3fzTAstXkWBGI");
		param.put("url", "https://blog.csdn.net/m0_58095675"); //非必填,点击后跳转的页面
		// 如果要调整小程序需要下面这个,非必填
		JSONObject miniprogram = new JSONObject(); 
		miniprogram.put("appid", "wxa3b096d8546b270d"); //小程序的appid
		miniprogram.put("pagepath", "pages/station/station"); //不填就是默认首页,这个可以带参数
		param.put("miniprogram", miniprogram); 
		// 数据参数
		JSONObject data = new JSONObject();
		JSONObject first = new JSONObject();
		first.put("value", "车辆已入场");
		first.put("color", "#666666"); //文字颜色,不传默认黑色
		data.put("first", first);
		JSONObject keyword1 = new JSONObject();
		keyword1.put("value", "2022-12-12 11:12:13");
		keyword1.put("color", "#333333");
		data.put("keyword1", keyword1);
		JSONObject keyword2 = new JSONObject();
		keyword2.put("value", "ETC");
		keyword2.put("color", "#333333");
		data.put("keyword2", keyword2);
		JSONObject remark = new JSONObject();
		remark.put("value", "欢迎下次再来");
		remark.put("color", "#333333");
		data.put("remark", remark);
		param.put("data", data);
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
		

接口管理模板

管理后台都能直接操作,这个主要是给第三方调用的。

  • 设置所属行业
    一个月只能修改一次,不是自然月,是本次修改后,下个月相同日期才能再次修改。今天9月28日,下次修改就是10月28日
		// 设置所属行业(一个月只能修改一次,不是自然月,是本次修改后,下个月相同日期才能再次修改。今天9月28日,下次修改就是10月28日)
		url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + accessToken;
		param = new JSONObject();
		param.put("industry_id1", "2");
		param.put("industry_id2", "14");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
  • 获取设置的行业信息
		// 获取设置的行业信息
		url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
  • 获取模板id
    根据模板的编号获取到模板id,请求参数中编号就是模板库列表的第一列。
		// 获取模板id
		url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" + accessToken;
		param = new JSONObject();
		param.put("template_id_short", "TM00074");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
  • 获取模板列表
    返回的是已经添加到我的模板里面的,至多25条
		// 获取模板列表,返回的是已经添加到我的模板里面的
		url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
  • 删除模板消息
    经测试没有从我的模板中的删除,只是变更了模板ID
		// 删除模板消息。 经测试没有从我的模板中的删除,只是变更了模板ID
		url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=" + accessToken;
		param = new JSONObject();
		param.put("template_id", "WG-muySFRWcSd7GfaYuL38kkle961ORO854FsJTp5C4");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);	

完整代码

package com.lootaa.wechat;

import org.jsoup.Jsoup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.jsoup.Connection.Method;

/**
 * 前置条件:公众号后台设置ip白名单
 */
public class Test006 {

	public static final String APPID = "wx276049d6a7551dca";
	public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";
	
	/**
	 * 完整项目源码可关注公众号"lootaayun"(洛塔),回复006获取
	 */
	public static void main(String[] args) throws Exception {
		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		String accessToken = JSON.parseObject(result).getString("access_token");
		// 发送模板消息
		url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
		JSONObject param = new JSONObject();
		param.put("touser", "ohUHp6iaFJq6SISTVwHS5lkb9Pb8"); //接收人的openid
		param.put("template_id", "LZJq4j3ERx_IT0JTw06X0v1lIDZ2Hu3fzTAstXkWBGI");
		param.put("url", "https://blog.csdn.net/m0_58095675"); //非必填,点击后跳转的页面
		// 如果要调整小程序需要下面这个,非必填
		JSONObject miniprogram = new JSONObject(); 
		miniprogram.put("appid", "wxa3b096d8546b270d"); //小程序的appid
		miniprogram.put("pagepath", "pages/station/station"); //不填就是默认首页,这个可以带参数
		param.put("miniprogram", miniprogram); 
		// 数据参数
		JSONObject data = new JSONObject();
		JSONObject first = new JSONObject();
		first.put("value", "车辆已入场");
		first.put("color", "#666666"); //文字颜色,不传默认黑色
		data.put("first", first);
		JSONObject keyword1 = new JSONObject();
		keyword1.put("value", "2022-12-12 11:12:13");
		keyword1.put("color", "#333333");
		data.put("keyword1", keyword1);
		JSONObject keyword2 = new JSONObject();
		keyword2.put("value", "ETC");
		keyword2.put("color", "#333333");
		data.put("keyword2", keyword2);
		JSONObject remark = new JSONObject();
		remark.put("value", "欢迎下次再来");
		remark.put("color", "#333333");
		data.put("remark", remark);
		param.put("data", data);
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
		
		// 设置所属行业(一个月只能修改一次,不是自然月,是本次修改后,下个月相同日期才能再次修改。今天9月28日,下次修改就是10月28日)
		url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + accessToken;
		param = new JSONObject();
		param.put("industry_id1", "2");
		param.put("industry_id2", "14");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
		
		// 获取设置的行业信息
		url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		
		// 获取模板id
		url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" + accessToken;
		param = new JSONObject();
		param.put("template_id_short", "TM00074");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
		
		// 获取模板列表,返回的是已经添加到我的模板里面的
		url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		
		// 删除模板消息。 经测试没有从我的模板中的删除,只是变更了模板ID
		url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=" + accessToken;
		param = new JSONObject();
		param.put("template_id", "WG-muySFRWcSd7GfaYuL38kkle961ORO854FsJTp5C4");
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
		System.out.println(result);
		
	}
	
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lootaa

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值