Implementing Keyword Auto-Reply for a WeChat Public Account with Java Servlets

This guide walks through the end-to-end setup of a WeChat public-account keyword auto-reply, covering the message flow, required server configuration in the WeChat console, and Java servlet implementations for the GET verification and POST message-handling endpoints.

Coder Trainee
Coder Trainee
Coder Trainee
Implementing Keyword Auto-Reply for a WeChat Public Account with Java Servlets

Introduction

Most developers have seen a WeChat public account that replies with a download link when a user sends a keyword such as “python”. This article explains how to implement that keyword‑reply feature.

Implementation Flow

The process is: the WeChat client sends a message to the WeChat server, the server forwards it to our backend, the backend processes the keyword, formats the response in the XML structure required by WeChat, returns it to the WeChat server, and finally the server pushes the reply back to the client.

WeChat Backend Configuration

In the WeChat public‑account console, go to “Settings & Development → Basic Configuration”. Fill in the server URL, Token, EncodingAESKey and choose the encryption mode. After saving, two HTTP endpoints must be provided at the URL.

GET Verification Endpoint

The GET endpoint validates that the request comes from WeChat. The servlet extracts the parameters signature, timestamp, nonce and echostr, checks the signature with SignUtil.checkSignature, and returns echostr if the check succeeds.

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String signature = req.getParameter("signature");
    String timestamp = req.getParameter("timestamp");
    String nonce = req.getParameter("nonce");
    String echostr = req.getParameter("echostr");
    PrintWriter out = resp.getWriter();
    if (SignUtil.checkSignature(signature, timestamp, nonce)) {
        out.print(echostr);
    }
    if (out != null) {
        out.close();
    }
    out = null;
}

POST Message-Handling Endpoint

The POST endpoint receives the XML message sent by the WeChat server, decodes it as UTF‑8, passes it to a core service that builds the reply, and writes the reply back to the response.

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    resp.setCharacterEncoding("UTF-8");
    String respMessage = CoreService.processRequest(req);
    PrintWriter out = resp.getWriter();
    out.print(respMessage);
    out.close();
}

Conclusion

After configuring the server settings and deploying the two endpoints, the public account can automatically respond to keyword messages such as “python” with the desired download link.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaHTTPServletWeChatWeChat APIPublic AccountKeyword Reply
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

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.