spring boot微信公众号关键词自动回复

前言

今天主要给大家讲一下如何实现微信公众号关键词的自动回复功能,进行人机识别,需要关注公众号回复验证码获取到验证码从而展示文章内容,,具体效果如下图。
16418920121.jpg

image.png

1.首先需要自行前往微信公众平台创建公众号(博主已经有公众号就不在进行讲解这一步了)
2.在公众号平台选择基础配置进行服务器的配置,如下图

16418924261.jpg

  1. url:填写你后端的接口地址,get请求
  2. token:自己随便定义一个
  3. EncodingAESKey:可以自己选择也可以随机生成
  4. 加解密方式:根据自己的实际情况来选择
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要一致

以上只是讲解了自动回复文本类型的功能,其他类型功能以后在进行讲解或可自行百度
最后献上完成的示例图
16418931691.jpg

Bye

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值