wechatbot
δΈ­ζ–‡

The Go SDK is clean, idiomatic, and goroutine-safe. stdlib only, no CGO, 32 tests. Uses sync.Map for concurrent context_token access. Tested on Linux, macOS, and Windows.

Install

go get github.com/corespeed-io/wechatbot/golang

Quick start

package main import ( "context" "fmt" wechatbot "github.com/corespeed-io/wechatbot/golang" ) func main() { ctx := context.Background() bot := wechatbot.New() creds, _ := bot.Login(ctx, false) fmt.Printf("Logged in: %s\n", creds.AccountID) bot.OnMessage(func(msg *wechatbot.IncomingMessage) { bot.SendTyping(ctx, msg.UserID) bot.Reply(ctx, msg, fmt.Sprintf("Echo: %s", msg.Text)) }) bot.Run(ctx) }

Configuration

bot := wechatbot.New(wechatbot.Options{ BaseURL: "", // default: ilinkai.weixin.qq.com CredPath: "", // default: ~/.wechatbot/credentials.json OnQRURL: func(url string) { ... }, // custom QR rendering OnScanned: func() { ... }, OnExpired: func() { ... }, OnError: func(err error) { ... }, })

Message handling

bot.OnMessage(func(msg *wechatbot.IncomingMessage) { fmt.Printf("[%s] %s: %s\n", msg.Type, msg.UserID, msg.Text) for _, img := range msg.Images { fmt.Printf(" Image: %s\n", img.URL) } for _, voice := range msg.Voices { fmt.Printf(" Voice: %s (%dms)\n", voice.Text, voice.DurationMs) } for _, file := range msg.Files { fmt.Printf(" File: %s (%d bytes)\n", file.FileName, file.Size) } if msg.QuotedMessage != nil { fmt.Printf(" Quoted: %s\n", msg.QuotedMessage.Title) } })

Sending messages

// Reply (auto context_token) bot.Reply(ctx, msg, "Hello back!") // Send to user (needs prior context) bot.Send(ctx, userID, "Proactive message") // Typing bot.SendTyping(ctx, userID) bot.StopTyping(ctx, userID)

Lifecycle

// Start (blocks until Stop or context cancel) err := bot.Run(ctx) // Stop gracefully bot.Stop()

Session recovery is automatic β€” on -14 error, the bot clears state, deletes credentials, and re-runs QR login.

Key types

type IncomingMessage struct { UserID string Text string Type ContentType // "text", "image", "voice", "file", "video" Timestamp time.Time Images []ImageContent Voices []VoiceContent Files []FileContent Videos []VideoContent QuotedMessage *QuotedMessage }
Go SDK β€” wechatbot/golang