go-wechaty

repository·master·Indexed 19 days ago

https://github.com/wechaty/go-wechaty

A 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.

Tokens
614
Snippets
2
Records
3
Agent score
19%

What's inside go-wechaty

  1. Troubleshoot WECHATY_PUPPET_SERVICE_TOKEN errors

    master

    If 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.

  2. Create a basic chatbot with go-wechaty

    master

    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()
    }