前言
今天主要给大家讲一下如何实现微信公众号关键词的自动回复功能,进行人机识别,需要关注公众号回复验证码获取到验证码从而展示文章内容,,具体效果如下图。
1.首先需要自行前往微信公众平台创建公众号(博主已经有公众号就不在进行讲解这一步了)
2.在公众号平台选择基础配置进行服务器的配置,如下图
- url:填写你后端的接口地址,get请求
- token:自己随便定义一个
- EncodingAESKey:可以自己选择也可以随机生成
- 加解密方式:根据自己的实际情况来选择
3.pom.xml引入依赖
<!--微信公众号关键词自动回复-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.4.0</version>
</dependency>
4.application文件:
wechat:
app-id:
# 公众号AppSecret
secret:
# 公众号Token
token:
# 公众号AES Key
aesKey:
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 {
@Autowired
private WechatProperties wechatMpProperties;
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
configStorage.setAppId(wechatMpProperties.getAppId());
configStorage.setSecret(wechatMpProperties.getSecret());
configStorage.setToken(wechatMpProperties.getToken());
configStorage.setAesKey(wechatMpProperties.getAesKey());
return configStorage;
}
@Bean
public WxMpService wxMpService(WxMpConfigStorage wxMpConfigStorage) {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage);
return wxMpService;
}
}
7.controller层代码
/**
* @author blue
* @date 2022/1/11
* @apiNote
*/
@Slf4j
@Api(tags = "微信接口相关控制器")
@RestController()
@RequestMapping("/wechat")
@RequiredArgsConstructor
public class ApiWeChatController {
private final RedisService redisService;
private final WxMpService wxMpService;
@ApiOperation("微信公众号服务器配置校验token")
@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) {
if (wxMpService.checkSignature(timestamp, nonce, signature)) {
return echostr;
}
return "Invalid signature";
}
@PostMapping(produces = "application/xml; charset=UTF-8")
public String handleMsg(HttpServletRequest request) {
try {
WxMpXmlMessage message = WxMpXmlMessage.fromXml(request.getInputStream());
String content = message.getContent();
log.info("公众号请求类型:{};内容为:{}", message.getMsgType(), content);
if (WxConsts.XmlMsgType.TEXT.equals(message.getMsgType())){
if ("验证码".equals(content)) {
String code = RandomUtils.generationNumber(6);
String msg = MessageFormat.format("您的本次验证码:{0},该验证码30分钟内有效。", code);
WxMpXmlOutTextMessage outMessage = WxMpXmlOutTextMessage.TEXT().content(msg).fromUser(message.getToUser()).toUser(message.getFromUser()).build();
redisService.setCacheObject(RedisConstants.WECHAT_CODE+code,code,30, TimeUnit.MINUTES);
return outMessage.toXml();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
此处需要注意controller接口名,需要与前面公众号接口配置填写的url要一致
以上只是讲解了自动回复文本类型的功能,其他类型功能以后在进行讲解或可自行百度
最后献上完成的示例图