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

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

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: 
# 公众号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 {

    @Resource
    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层代码

此处需要注意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();
    }

}

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

在这里插入图片描述

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Peak_Gao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值