今天主要给大家讲一下如何实现微信公众号关键词的自动回复功能,就如网站的文章而言,进行人机识别,需要关注公众号回复验证码获取到验证码从而展示文章内容,,具体效果如下图。
springboot 2.3.2RELEASE
1、微信公众平台创建公众号
首先需要自行前往微信公众平台创建公众号(博主已经有公众号就不在进行讲解这一步了(注册简单,自行百度))
2、微信公众平台基础配置
在公众号平台选择基础配置进行服务器的配置,如下图
1.url:填写你后端的接口地址,get请求
2.token:自己随便定义一个
3.EncodingAESKey:可以自己选择也可以随机生成
4.加解密方式:根据自己的实际情况来选择
3、pom引入依赖
<!--微信公众号关键词自动回复--><dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>3.4.0</version>
</dependency>
4、application.yml
wechat:app-id:
# 公众号AppSecretsecret:
# 公众号Tokentoken:
# 公众号AES KeyaesKey:
5、新建WechatProperties类
@Data
@Configuration
@ConfigurationProperties(prefix = "wechat")
public class WechatProperties {private String appId;private String secret;private String token;private String aesKey;
}
6、新建WechatConfiguration类
@Configuration
public class WechatConfiguration {@Resourceprivate WechatProperties wechatMpProperties;@Beanpublic WxMpConfigStorage wxMpConfigStorage() {WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();configStorage.setAppId(wechatMpProperties.getAppId());configStorage.setSecret(wechatMpProperties.getSecret());configStorage.setToken(wechatMpProperties.getToken());configStorage.setAesKey(wechatMpProperties.getAesKey());return configStorage;}@Beanpublic WxMpService wxMpService(WxMpConfigStorage wxMpConfigStorage) {WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxMpConfigStorage);return wxMpService;}
}
7.controller层代码
此处需要注意controller接口名,需要与前面公众号接口配置填写的url要一致
/*** 微信接口相关控制器* Created by PeakGao on 2023/8/5.*/@RestController
@RequestMapping("/wechat")
@RequiredArgsConstructor
public class ApiWeChatController extends BaseController {private final WxMpService wxMpService;/*** 微信公众号服务器配置校验token(这个接口是配置在服务器配置中的)** @param signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。* @param timestamp 时间戳* @param nonce 随机数* @param echostr 随机字符串* @return*/@GetMapping(produces = "text/plain;charset=utf-8")public String checkSignature(@RequestParam(name = "signature") String signature,@RequestParam(name = "timestamp") String timestamp,@RequestParam(name = "nonce") String nonce,@RequestParam(name = "echostr") String echostr) {logger.info("接收来自微信服务器得认证消息:[{},{},{},{}]", signature, timestamp, nonce, echostr);if (wxMpService.checkSignature(timestamp, nonce, signature)) {return echostr;}return "Invalid signature";}/*** 关键字回复** @param request* @return*/@PostMapping(produces = "application/xml; charset=UTF-8")public String handleMsg(HttpServletRequest request) {try {WxMpXmlMessage message = WxMpXmlMessage.fromXml(request.getInputStream());String content = message.getContent();logger.info("公众号请求类型:{};内容为:{}", message.getMsgType(), content);if (WxConsts.XmlMsgType.TEXT.equals(message.getMsgType())) {if ("验证码".equals(content)) {String code = RandomUtils.generationNumberChar(6);String msg = MessageFormat.format("您的本次验证码:{0},该验证码30分钟内有效。", code);redisUtil.set(Constant.WECHAT_CODE + code, code, 1800);return returnMsg(msg, message);}}} catch (Exception e) {e.printStackTrace();}return "";}/*** 返回消息** @param msg 消息内容* @param message* @return*/private static String returnMsg(String msg, WxMpXmlMessage message) {WxMpXmlOutTextMessage outMessage = WxMpXmlOutTextMessage.TEXT().content(msg).fromUser(message.getToUser()).toUser(message.getFromUser()).build();return outMessage.toXml();}}
以上只是讲解了自动回复文本类型的功能,其他类型功能以后在进行讲解或可自行百度
最后献上完成的示例图