Backend Development 8 min read

Building a WeChat Subscription Account Bot with Spring Boot and Image Moderation

This tutorial explains how to configure a WeChat public account, set up a Spring Boot backend with the wechat-spring-boot-starter, implement GET and POST endpoints for message verification and handling, and integrate an image‑moderation service to automatically reply to picture messages.

Top Architect
Top Architect
Top Architect
Building a WeChat Subscription Account Bot with Spring Boot and Image Moderation

The article walks through creating a WeChat subscription‑account robot, starting from the overall message flow: user sends a message, WeChat forwards it to your server, the server processes it, and the response is sent back to the user.

1. Configure the WeChat public account – create a subscription account, open the developer settings, and fill in the required fields such as AppID, AppSecret, server URL, Token, and encryption options. Only the server address, token, and encryption settings are needed for the simple image‑reply bot.

2. Set up the Spring Boot backend – add a custom Maven repository and the wechat-spring-boot-starter dependency:

<repositories>
    <repository>
        <id>developer-weapons-repository</id>
        <url>https://raw.githubusercontent.com/developer-weapons/repository/master</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.developer.weapons</groupId>
    <artifactId>wechat-spring-boot-starter</artifactId>
    <version>1.2.6</version>
</dependency>

Two controller methods are required: a GET endpoint for the initial verification and a POST endpoint for receiving messages.

@Autowired
private WechatOfficialService wechatOfficialService;

@Value("${weixin.token}")
private String token;

@RequestMapping(value = "/weixin/receive", method = RequestMethod.GET)
public void receive(@RequestParam("signature") String signature,
                    @RequestParam("timestamp") String timestamp,
                    @RequestParam("nonce") String nonce,
                    @RequestParam("echostr") String echostr,
                    HttpServletResponse response) throws IOException {
    boolean valid = wechatOfficialService.isValid(signature, timestamp, nonce, token);
    PrintWriter writer = response.getWriter();
    if (valid) {
        writer.print(echostr);
    } else {
        writer.print("error");
    }
    writer.flush();
    writer.close();
}

The POST method validates the request, parses the XML payload into a map, and checks the MsgType . If the message is an image, it calls an external moderation service and replies with either a warning or a success text.

@RequestMapping(value = "/weixin/receive", method = RequestMethod.POST)
public void receive(@RequestParam("signature") String signature,
                    @RequestParam("timestamp") String timestamp,
                    @RequestParam("nonce") String nonce,
                    HttpServletRequest request,
                    HttpServletResponse response) throws IOException {
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    boolean valid = wechatOfficialService.isValid(signature, timestamp, nonce, token);
    PrintWriter writer = response.getWriter();
    if (!valid) {
        writer.print("error");
        writer.flush();
        writer.close();
        return;
    }
    try {
        Map
map = wechatOfficialService.toMap(request.getInputStream());
        if ("image".equals(map.get("MsgType"))) {
            String res = checkService.check(publicKey, privateKey, map.get("PicUrl"));
            OfficialAutoReplyMessage reply = OfficialAutoReplyMessage.build()
                .withMsgtype(MessageTypeEnum.TEXT)
                .withFromUserName(map.get("ToUserName"))
                .withToUserName(map.get("FromUserName"));
            if ("forbid".equals(res)) {
                reply.withContent("小哥,你的图片有点问题哦");
            } else {
                reply.withContent("骚年,你这图片刚刚的没问题");
            }
            writer.print(reply.toXml());
            writer.flush();
            writer.close();
            return;
        }
    } catch (Exception e) {
        log.error("WeixinController receive error", e);
    }
    writer.print("success");
    writer.flush();
    writer.close();
}

After deploying the service and enabling the configuration in the WeChat admin console, the bot can automatically reply with the image URL or moderation result. The article also includes promotional notes for obtaining the full source code and joining a community group.

backendJavaSpring BootWeChatBotImage Moderation
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.