Java development WeChat template message push

Dolphin Elf : https://mgo.whhtjl.com

When I see this, I guess you have read all kinds of information on the Internet. Don't talk nonsense, just focus on the key points. You can click on the following link to go to the official query information.

https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

Regarding usage rules, please note:

1. All service accounts can see the entry of the application template message function at Function -> Add Function Plug-in, but only the service account after authentication can apply for and obtain the permission to use the template message;

2. You need to select 2 industries where the official account service is located, and the selected industry can be changed once a month;

3. Select existing templates to call in the template library of the selected industry;

4. Each account can use 25 templates at the same time.

5. The current daily limit of template messages for each account is 100,000 times. There is no special limit for a single template, and the number indicated on the official account MP background developer center page shall prevail.

Regarding interface documentation, please note:

1. The template ID and the assignment content of each parameter in the template are mainly required when the template message is called;

2. The parameter content in the template must end with ".DATA", otherwise it is regarded as a reserved word;

3. Template reserved symbol "{ {}}"

Focus on the code, I only give an example here, as follows:

The weixin-java-mp, pom.xml file I used here is as follows:

<!--公众号(包括订阅号和服务号) -->
<dependency>
	<groupId>com.github.binarywang</groupId>
	<artifactId>weixin-java-mp</artifactId>
	<version>2.7.0</version>
</dependency>

The configuration file code is as follows:

#微信公众号
wx.mp.appId=XXXXXXXXXXXXXXXXX
wx.mp.appSecret=XXXXXXXXXXXXXXXXXXX

Create WeChatMpProperties entity class

package com.ltf.config;

import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 微信公众号商户基本信息
 * @author xhz
 *
 */
@Data
@Component
@ConfigurationProperties(prefix = "wx.mp")
public class WeChatMpProperties {

	/**
	 * appId
	 */
	private String appId;

	/**
	 * 公众平台密钥
	 */
	private String appSecret;

}

Create WxJsMpConfig

package com.ltf.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
 * 微信公众号信息注入bean中
 * @author xhz
 *
 */
@Component
public class WxJsMpConfig {

	@Autowired
	private WeChatMpProperties wxJsMpProperties;

	@Bean
	public WxMpService wxJsMpService( ){
		WxMpService wxJsMpService = new WxMpServiceImpl();
		wxJsMpService.setWxMpConfigStorage(wxMpConfigStorage());
		return wxJsMpService;
	}

	private WxMpConfigStorage wxMpConfigStorage(){
		WxMpInMemoryConfigStorage wxJsMpConfigStorage = new WxMpInMemoryConfigStorage();
		// 公众号appId
		wxJsMpConfigStorage.setAppId(this.wxJsMpProperties.getAppId());
		// 公众号appSecret
		wxJsMpConfigStorage.setSecret(this.wxJsMpProperties.getAppSecret());
		return wxJsMpConfigStorage;
	}

}

Create template message entity class

package com.ltf.entity;
import java.io.Serializable;
import lombok.Data;
/**
 * 微信消息模板实体类
 * @author xhz
 *
 */
@Data
public class WxMsgTemplate implements Serializable {

	private static final long serialVersionUID = 1L;
	private String openId;
	private String templateId;
	private String url;
	private String color;
	private String first;
	private String keyWord1;
	private String keyWord2;
	private String keyWord3;
	private String keyWord4;
	private String keyWord5;
	private String keyWord6;
	private String remark;

}

Create WeChat message push service layer

package com.ltf.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltf.entity.WxMsgTemplate;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

/**
 * 微信消息推送
 * @author xhz
 *
 */
@Service
public class WxMsgPushService {

	private static final Logger logger = LoggerFactory
			.getLogger(WxMsgPushService.class);

	/**
	 * 微信公众号API的Service
	 */
	@Autowired
	private WxMpService wxJsMpService;

	/**
	 * 注册成功后发送微信模板消息
	 * @param wxMsgTemplate
	 * @return
	 */
	public Boolean sendWxMsgAfterSuccessRegister(WxMsgTemplate wxMsgTemplate) {
		String result=null;
		try {
			WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
					.toUser(wxMsgTemplate.getOpenId())//要推送的用户openId
					.templateId(wxMsgTemplate.getTemplateId())//模版id
					.url(wxMsgTemplate.getUrl())//点击模版消息要访问的网址
					.build();
			templateMessage.addWxMpTemplateData(new WxMpTemplateData("first", wxMsgTemplate.getFirst(), wxMsgTemplate.getColor()));
			templateMessage.addWxMpTemplateData(new WxMpTemplateData("keyword1", wxMsgTemplate.getKeyWord1(),  wxMsgTemplate.getColor()));
			templateMessage.addWxMpTemplateData(new WxMpTemplateData("keyword2", wxMsgTemplate.getKeyWord2(),  wxMsgTemplate.getColor()));
			templateMessage.addWxMpTemplateData(new WxMpTemplateData("remark", wxMsgTemplate.getRemark(), wxMsgTemplate.getColor()));
			result=wxJsMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
		} catch (Exception e) {
			logger.error("WxMsgPushService.sendWxMsgAfterSuccessRegister()----error", e);
		}
		return result!=null;
	}

}

Create test class

public static void main(String[] args) {
WxMsgTemplate wxMsgTemplate=new WxMsgTemplate();
wxMsgTemplate.setOpenId("xxxxxxxxxxxxxxxxxxx");
wxMsgTemplate.setTemplateId(CacheConstans.TEMPLATEID_REGISTER);
wxMsgTemplate.setUrl(CacheConstans.URL);
wxMsgTemplate.setColor(CacheConstans.COLOR);
wxMsgTemplate.setFirst("XXXXXX注册通知");//详细内容
wxMsgTemplate.setKeyWord1("小盒子");//用户名
wxMsgTemplate.setKeyWord2(DateUtils.toString(new Date(), "yyyy-MM-dd HH:mm:ss"));//注册时间
wxMsgTemplate.setRemark("欢迎来到XXXXXX");
//给新用户推送消息
wxMsgPushService.sendWxMsgAfterSuccessRegister(wxMsgTemplate);
}

The running code is as follows:

OK, go home from get off work la la la la la la! ! !

 

Guess you like

Origin blog.csdn.net/qq_35393693/article/details/108284699