Go package for the Weixin QR login flow and bot API used by OpenClaw.
weixin-clawbot is a Go library for integrating with WeChat (Weixin) as a bot via the iLink Bot platform. It serves as the underlying client for the OpenClaw WeChat plugin.
It addresses the core problem of how to log into WeChat as a bot, receive messages, and reply automatically from Go code. The main capabilities are:
- QR-code login: Renders a QR code in the terminal; after the user scans it with their phone, the library persists the login credentials so subsequent runs don't require re-scanning.
- Message monitoring: Receives messages from WeChat contacts and groups in real time using long polling.
- Message sending: Sends text, image, video, and file messages to individual users or groups.
- Media handling: Uploads media files to the CDN (with AES-ECB encryption) and downloads inbound media to local storage.
If you are building a WeChat chatbot, automated messaging system, or customer-service bot, this library provides the low-level building blocks you need.
go get github.com/daemon365/weixin-clawbotimport weixin "github.com/daemon365/weixin-clawbot"- Interactive Weixin QR login with local account persistence
- Long-poll message monitoring with sync buffer persistence
- Text, image, video, and file sending helpers
- CDN upload/download helpers with AES-ECB handling
- Utilities for inbound media download and message conversion
package main
import (
"context"
"log"
"os"
weixin "github.com/daemon365/weixin-clawbot"
)
func main() {
client := weixin.NewClient(weixin.Options{})
account, err := client.LoginInteractive(context.Background(), weixin.InteractiveLoginOptions{
Output: os.Stdout,
SaveDir: ".weixin-accounts",
})
if err != nil {
log.Fatal(err)
}
log.Printf("account=%s token=%s", account.AccountID, account.BotToken)
}ctx := context.Background()
sender := weixin.NewSender(weixin.SenderOptions{
BaseURL: "https://ilinkai.weixin.qq.com",
Token: "YOUR_BOT_TOKEN",
Timeout: 15 * time.Second,
})
conversation := sender.Conversation(weixin.Target{
ToUserID: "user@im.wechat",
ContextToken: "YOUR_CONTEXT_TOKEN",
})
clientID, err := conversation.SendText(ctx, "hello from bot")
if err != nil {
log.Fatal(err)
}
log.Println("sent:", clientID)api := weixin.NewAPIClient(weixin.APIOptions{
BaseURL: "https://ilinkai.weixin.qq.com",
Token: "YOUR_BOT_TOKEN",
})
err := weixin.Monitor(context.Background(), weixin.MonitorOptions{
API: api,
AccountID: "bot@im.bot",
SyncBufPath: weixin.SyncBufFilePath(weixin.ResolveStateDir(), "bot@im.bot"),
OnMessages: func(ctx context.Context, messages []weixin.WeixinMessage) error {
for _, msg := range messages {
log.Printf("from=%s body=%s", msg.FromUserID, weixin.BodyFromItemList(msg.ItemList))
}
return nil
},
})
if err != nil {
log.Fatal(err)
}Client: QR login flowAPIClient: ilink bot API wrapperSender: reusable outbound senderConversation: bound sender for oneToUserID+ContextTokenTarget: outbound conversation targetMonitorOptions: long-poll monitor configurationUploadedFileInfo: CDN upload result
- The package name is
weixin, while the module import path isgithub.com/daemon365/weixin-clawbot. - Account files are stored as base64url-encoded filenames to avoid unsafe path characters.
Target.ContextTokenis required for outbound messaging.