微信公众号自动回复功能,服务端 springboot JAVA 示例代码

以下是一个简单的微信公众号自动回复服务端代码示例:

首先,在 pom.xml 文件中添加如下 maven 依赖:


com.github.binarywang
weixin-java-mp
3.9.0

在 application.properties 文件中配置微信公众号相关信息:

微信公众号配置

wechat.appId=your_app_id
wechat.appSecret=your_app_secret
wechat.token=your_token
wechat.aesKey=your_aes_key
创建一个包含 @RestController 注解的控制器类,添加如下代码:
@RestController
@RequestMapping("/wechat/portal")
public class WechatController {

private final WxMpService wxService;

public WechatController(WxMpService wxService) {
    this.wxService = wxService;
}

@GetMapping(produces = "text/plain;charset=utf-8")
public String doGet(@RequestParam(name = "signature", required = false) String signature,
                    @RequestParam(name = "timestamp", required = false) String timestamp,
                    @RequestParam(name = "nonce", required = false) String nonce,
                    @RequestParam(name = "echostr", required = false) String echostr) {
    if (!wxService.checkSignature(timestamp, nonce, signature)) {
        // 检验失败,返回错误信息
        return "非法请求";
    }
    if (echostr != null) {
        // 验证成功,返回接入的消息
        return echostr;
    }
    return "不支持的请求类型";
}

@PostMapping(produces = "application/xml; charset=UTF-8")
public String doPost(@RequestBody String requestBody,
                     @RequestParam("signature") String signature,
                     @RequestParam("timestamp") String timestamp,
                     @RequestParam("nonce") String nonce,
                     @RequestParam(name = "encrypt_type", required = false) String encType,
                     @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    if (!wxService.checkSignature(timestamp, nonce, signature)) {
        // 检验失败,返回错误信息
        return "非法请求";
    }

    String inMessage = null;
    if (encType == null) {
        // 明文交互
        inMessage = requestBody;
    } else if ("aes".equalsIgnoreCase(encType)) {
        // Aes 模式交互
        inMessage = WxMpCryptUtils.decrypt(wxService.getWxMpConfigStorage(), requestBody, msgSignature, timestamp, nonce);
    }
    if (inMessage == null) {
        return "";
    }

    WxMpXmlMessage inWxMessage = WxMpXmlMessage.fromXml(inMessage);
    WxMpXmlOutMessage outMessage = null;

    // 自动回复功能实现
    String content = "你好,欢迎关注我的公众号!";
    outMessage = WxMpXmlOutMessage.TEXT().toUser(inWxMessage.getFromUser()).fromUser(inWxMessage.getToUser()).content(content).build();

    if (outMessage == null) {
        return "";
    }

    if (encType == null) {
        return outMessage.toXml();
    } else if ("aes".equalsIgnoreCase(encType)) {
        return WxMpCryptUtils.encrypt(wxService.getWxMpConfigStorage(), outMessage.toXml(), timestamp, nonce);
    }

    return "";
}

}
其中 doGet() 方法处理微信公众号的接入验证请求,doPost() 方法处理微信公众号接收到的消息(包括文本、图片、语音、视频等),并根据业务需求自动回复消息。注意在 doPost() 方法中需要根据消息的加密类型进行解密操作。

最后,在启动类中添加如下代码:
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Bean
public WxMpService wxService(WxMpConfigStorage wxMpConfigStorage) {
    WxMpService wxService = new WxMpServiceImpl();
    wxService.setWxMpConfigStorage(wxMpConfigStorage);
    return wxService;
}

@Bean
public WxMpConfigStorage wxMpConfigStorage() {
    WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
    config.setAppId("your_app_id");
    config.setSecret("your_app_secret");
    config.setToken("your_token");
    config.setAesKey("your_aes_key");
    return config;
}

}
以上就是一个简单的微信公众号自动回复服务端代码示例,具体的消息处理逻辑需要根据业务需求进行修改。

赛文市场营销

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注