Install go-wechaty
masterTo install the go-wechaty SDK, use the standard Go module command. Ensure you have Go 1.18 or higher installed.
go get github.com/wechaty/go-wechatyrepository·master·Indexed 19 days ago
https://github.com/wechaty/go-wechatyA Go implementation of the Wechaty RPA SDK for creating WeChat chatbots for Individual accounts. It provides event handlers such as OnScan, OnLogin, and OnMessage, and requires a Puppet service via wechaty-gateway to interface with WeChat.
To install the go-wechaty SDK, use the standard Go module command. Ensure you have Go 1.18 or higher installed.
go get github.com/wechaty/go-wechatyIf you encounter an error stating WECHATY_PUPPET_SERVICE_TOKEN not found, it is because go-wechaty requires a Puppet to interface with WeChat.
Since most puppets are written in TypeScript, you must use wechaty-gateway to convert the TypeScript puppets into a gRPC service. The connection flow is:
go-wechaty $\rightarrow$ wechaty-gateway $\rightarrow$ puppet.
Refer to the Puppet Services DIY guide for implementation details.
You can build a functional chatbot by initializing a new Wechaty instance and registering event handlers for scanning, logging in, and receiving messages. Use DaemonStart() to begin the bot's lifecycle.
Key event handlers:
OnScan: Triggered when a QR code is generated for login.OnLogin: Triggered when the user successfully logs in.OnMessage: Triggered when a new message is received.package main
import (
"fmt"
"github.com/wechaty/go-wechaty/wechaty"
"github.com/wechaty/go-wechaty/wechaty-puppet/schemas"
"github.com/wechaty/go-wechaty/wechaty/user"
)
func main() {
wechaty.NewWechaty().
OnScan(func(context *wechaty.Context, qrCode string, status schemas.ScanStatus, data string) {
fmt.Printf("Scan QR Code to login: %s\nhttps://wechaty.github.io/qrcode/%s\n", status, qrCode)
}).
OnLogin(func(context *wechaty.Context, user *user.ContactSelf) {
fmt.Printf("User %s logined\n", user)
}).
OnMessage(func(context *wechaty.Context, message *user.Message) {
fmt.Printf("Message: %s\n", message)
}).DaemonStart()
}