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

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

实现展示

适用场景

当我们个人在接入微信登录时需要微信认证,个人认证不了,这时就可以使用公众号关键词自动回复获取验证码实现登录功能。

不说废话,直接开干

实现步骤

①、去微信公众平台注册

微信公众平台 (qq.com)

在公众号平台选择基础配置进行服务器的配置,如下图

之后进入到你的开发者中心

然后生成你的开发者密码,开发者id,以及设置你的IP白名单。这里的IP白名单中的IP必须是一个公网IP,因为微信官方会把他们的请求发送到公网上,然后你接受到请求之后需要给这个请求做一个响应才能实现消息互通的功能。

ip:必须是你公网ip地址

之后开始配置你的服务器信息

首先是URL,这里的URL需要填写的是

http://ip:80/path(这里的path满足请求路径的格式即可)

或者是

https://ip:443/path

以上就是整合SpringBoot时需要的配置属性

②、整合SpringBoot

导入依赖

<!--微信公众号关键词自动回复-->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>3.4.0</version>
        </dependency>
wechat:
  app-id: #公众号appid
  secret: #公众号密钥
  token: towelove#token自己随便设置一个,但是必须对应公众号里面设置的一样
  aesKey: #加密密钥,可以选随机生成
package com.shiyi.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "wechat")
public class WechatProperties {

    private String appId;
    private String secret;
    private String token;
    private String aesKey;
}
package com.shiyi.config;

import com.shiyi.config.properties.WechatProperties;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@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;
    }
}
package com.shiyi.controller.api;

import com.shiyi.common.RedisConstants;
import com.shiyi.service.RedisService;
import com.shiyi.utils.RandomUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

/**
 * @author weixueshi
 * @date 2023/8/4
 * @apiNote
 */
@Slf4j
@RestController()
@RequestMapping("/wx")
@RequiredArgsConstructor
public class ApiWeChatController {

    private final WxMpService wxMpService;

    private final RedisService redisService;

    @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) {
        log.info("微信公众号get请求进来了....");
        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();
            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},该验证码5分钟内有效。", code);
                    WxMpXmlOutTextMessage outMessage = WxMpXmlOutTextMessage.TEXT().content(msg).fromUser(message.getToUser()).toUser(message.getFromUser()).build();
                    //将code值保存在redis中
                    redisService.setCacheObject(RedisConstants.WECHAT_CODE + code,code,5, TimeUnit.MINUTES);
                    return outMessage.toXml();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

注意:项目必须上线才会整合成功,因为微信需要访问上线后的地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ASP微信公众号自动回复是指通过ASP编程语言实现的一种功能,当用户在微信公众号中发送消息时,公众号会根据预设的规则和条件自动回复相应的内容。 通过ASP微信公众号自动回复,可以实现以下功能: 1. 关键词回复:根据用户发送的关键词自动回复相应的内容。可以预设一系列常见问题的关键词,当用户发送匹配的关键词时,公众号自动回复相应的答案。 2. 模糊匹配:可以设置关键词的模糊匹配规则,即使用户输入了略有不同的关键词公众号也可以根据相似度自动回复相应的答案。 3. 自定义回复:用户可以根据实际需求自定义回复的内容,包括文本、图片、语音等形式。可以根据用户的消息类型进行不同的回复,提供更丰富的交互体验。 4. 多场景回复:可以根据用户所在的场景进行不同的回复。例如,当用户在购物场景中发送消息时,公众号可以自动回复商品信息和优惠信息;当用户在咨询场景中发送消息时,公众号可以自动回复相关问题的答案。 5. 定时回复:可以设置定时任务,根据预设的时间进行自动回复。例如,可以设置每天早上9点自动回复早安,晚上10点自动回复晚安。 通过ASP微信公众号自动回复,可以提高公众号的效率和服务质量,实现自动化的客户互动。同时,也可以节省人力资源,让公众号能够更好地应对大量用户的咨询和需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

six-key

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

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

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

打赏作者

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

抵扣说明:

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

余额充值