feat(wechat): 关注公众号后自动回复欢迎语(被动回复,可后台配置)

用户关注服务号(扫登录码后关注 或 直接搜索关注)时,在回调 HTTP 响应里
回一条文本消息(微信「被动回复」)。被动回复不需要 access_token、不受 IP 白名单
限制,永远能发。欢迎语在管理端「登录设置」可配,留空用默认。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-21 11:17:41 +08:00
parent 9e2ff6007f
commit 83269e067a
5 changed files with 106 additions and 12 deletions
+22 -1
View File
@@ -31,12 +31,17 @@ import (
"github.com/sundynix/sundynix-shared/secrets"
)
// DefaultWelcome 是关注后自动回复的默认欢迎语(管理端可覆盖,建议填上下载链接)。
const DefaultWelcome = "欢迎关注!\n下载客户端并扫码登录,即可开始体验。"
// Config 是公众号登录所需配置。AppSecret 加密入库、只写不回显(同微信支付 APIv3 密钥)。
type Config struct {
AppID string `json:"appid"`
AppSecret string `json:"app_secret"`
// Token:消息推送签名校验用,与公众平台「服务器配置」里填的一致。
Token string `json:"token"`
// Welcome:用户关注后自动回复的欢迎语(被动回复)。空则用 DefaultWelcome。非密文。
Welcome string `json:"welcome"`
}
// Enabled 报告配置是否完整到可用(登录二维码需要 appid+secret;回调验签需要 token)。
@@ -174,13 +179,29 @@ func (c Config) CreateLoginQR(ctx context.Context, accessToken, scene string, ex
return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + url.QueryEscape(r.Ticket), nil
}
// Event 是微信推送的事件(明文 XML)。只取登录需要的字段。
// Event 是微信推送的事件(明文 XML)。只取登录/回复需要的字段。
type Event struct {
XMLName xml.Name `xml:"xml"`
MsgType string `xml:"MsgType"` // event
Event string `xml:"Event"` // subscribe / SCAN / unsubscribe ...
EventKey string `xml:"EventKey"` // subscribe: qrscene_<scene>SCAN: <scene>
FromUserName string `xml:"FromUserName"` // 用户 openid
ToUserName string `xml:"ToUserName"` // 公众号原始 ID(回复时作 FromUserName
CreateTime int64 `xml:"CreateTime"` // 事件时间(秒)
}
// IsSubscribe 报告是否是「新关注」事件(用于自动回复欢迎语)。
func (e Event) IsSubscribe() bool { return e.MsgType == "event" && e.Event == "subscribe" }
// BuildTextReply 组装「被动回复」的文本消息 XML(回调 HTTP 响应体)。
// 被动回复不需要 access_token、不受 IP 白名单限制,微信收到即转发给用户。
// toUser=用户 openid=事件的 FromUserName);fromUser=公众号原始 ID=事件的 ToUserName)。
func BuildTextReply(toUser, fromUser, content string, createTime int64) string {
content = strings.ReplaceAll(content, "]]>", "]] >") // 防 CDATA 提前闭合
return fmt.Sprintf(
"<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName>"+
"<CreateTime>%d</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>",
toUser, fromUser, createTime, content)
}
// Scene 从事件里还原出我们的 scene(登录 ticket)。subscribe 事件带 qrscene_ 前缀,SCAN 不带。
@@ -97,6 +97,48 @@ func TestParseEvent_IgnoresNonLogin(t *testing.T) {
}
}
// 关注事件要能识别(自动回复欢迎语),并解析出回复所需的 openid + 公众号原始 ID。
func TestParseEvent_SubscribeReplyFields(t *testing.T) {
sub := `<xml><ToUserName><![CDATA[gh_808ba17576cf]]></ToUserName>
<FromUserName><![CDATA[openid_x]]></FromUserName>
<CreateTime>1700000000</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[]]></EventKey></xml>`
ev, err := ParseEvent([]byte(sub))
if err != nil {
t.Fatal(err)
}
if !ev.IsSubscribe() {
t.Fatal("subscribe 应被识别为新关注")
}
if ev.ToUserName != "gh_808ba17576cf" {
t.Fatalf("公众号原始 ID 取错:%q", ev.ToUserName)
}
// 无 scene 的直接关注:不是登录扫码,但仍是 subscribe(要回欢迎语)
if ev.IsLoginScan() {
t.Fatal("无 scene 关注不该触发登录")
}
}
// 被动回复 XMLToUser=用户、FromUser=公众号、含文本内容,且防 CDATA 提前闭合。
func TestBuildTextReply(t *testing.T) {
xml := BuildTextReply("openid_user", "gh_pub", "你好]]>危险", 1700000000)
for _, want := range []string{
"<ToUserName><![CDATA[openid_user]]>",
"<FromUserName><![CDATA[gh_pub]]>",
"<MsgType><![CDATA[text]]>",
"<CreateTime>1700000000</CreateTime>",
} {
if !strings.Contains(xml, want) {
t.Fatalf("回复 XML 缺 %q\n%s", want, xml)
}
}
if strings.Contains(xml, "你好]]>危险") {
t.Fatal("内容里的 ]]> 应被转义,避免 CDATA 提前闭合")
}
}
func TestConfig_SecretRoundTrip(t *testing.T) {
c := Config{AppID: "x", AppSecret: "plain-secret", Token: "t"}
stored, err := c.EncryptedForStore()