示例程序

This commit is contained in:
Noth Amor
2022-07-18 17:48:13 +08:00
parent 0bdc59273b
commit 86b00d054a
3 changed files with 51 additions and 0 deletions

5
go.mod Normal file
View File

@@ -0,0 +1,5 @@
module go-bot/bot
go 1.18
require github.com/eatmoreapple/openwechat v1.1.11 // indirect

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/eatmoreapple/openwechat v1.1.11 h1:YJL8tUenK1NTflNPt5lWOl0KWcP0CTGjWNYAvN0dGFk=
github.com/eatmoreapple/openwechat v1.1.11/go.mod h1:61HOzTyvLobGdgWhL68jfGNwTJEv0mhQ1miCXQrvWU8=

44
main.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"fmt"
"github.com/eatmoreapple/openwechat"
)
func main() {
bot := openwechat.DefaultBot()
// bot := openwechat.DefaultBot(openwechat.Desktop) // 桌面模式,上面登录不上的可以尝试切换这种模式
// 注册消息处理函数
bot.MessageHandler = func(msg *openwechat.Message) {
if msg.IsText() && msg.Content == "ping" {
msg.ReplyText("pong")
}
}
// 注册登陆二维码回调
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
// 登陆
if err := bot.Login(); err != nil {
fmt.Println(err)
return
}
// 获取登陆的用户
self, err := bot.GetCurrentUser()
if err != nil {
fmt.Println(err)
return
}
// 获取所有的好友
friends, err := self.Friends()
fmt.Println(friends, err)
// 获取所有的群组
groups, err := self.Groups()
fmt.Println(groups, err)
// 阻塞主goroutine, 直到发生异常或者用户主动退出
bot.Block()
}