2 Star 8 Fork 3

eatmoreapple / openwechat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bot_test.go 2.63 KB
一键复制 编辑 原始数据 按行查看 历史
package openwechat
import (
"fmt"
"testing"
"time"
)
func TestLogin(t *testing.T) {
bot := DefaultBot(Desktop)
bot.LoginCallBack = func(body CheckLoginResponse) {
t.Log("login")
}
if err := bot.Login(); err != nil {
t.Error(err)
}
}
func TestLogout(t *testing.T) {
bot := DefaultBot(Desktop)
bot.LoginCallBack = func(body CheckLoginResponse) {
t.Log("login")
}
bot.LogoutCallBack = func(bot *Bot) {
t.Log("logout")
}
bot.MessageHandler = func(msg *Message) {
if msg.IsText() && msg.Content == "logout" {
bot.Logout()
}
}
if err := bot.Login(); err != nil {
t.Error(err)
return
}
bot.Block()
}
func TestMessageHandle(t *testing.T) {
bot := DefaultBot(Desktop)
bot.MessageHandler = func(msg *Message) {
if msg.IsText() && msg.Content == "ping" {
msg.ReplyText("pong")
}
}
if err := bot.Login(); err != nil {
t.Error(err)
return
}
bot.Block()
}
func TestFriends(t *testing.T) {
bot := DefaultBot(Desktop)
if err := bot.Login(); err != nil {
t.Error(err)
return
}
user, err := bot.GetCurrentUser()
if err != nil {
t.Error(err)
return
}
friends, err := user.Friends()
if err != nil {
t.Error(err)
return
}
t.Log(friends)
}
func TestGroups(t *testing.T) {
bot := DefaultBot(Desktop)
if err := bot.Login(); err != nil {
t.Error(err)
return
}
user, err := bot.GetCurrentUser()
if err != nil {
t.Error(err)
return
}
groups, err := user.Groups()
if err != nil {
t.Error(err)
return
}
t.Log(groups)
}
func TestPinUser(t *testing.T) {
bot := DefaultBot(Desktop)
if err := bot.Login(); err != nil {
t.Error(err)
return
}
user, err := bot.GetCurrentUser()
if err != nil {
t.Error(err)
return
}
friends, err := user.Friends()
if err != nil {
t.Error(err)
return
}
if friends.Count() > 0 {
f := friends.First()
f.Pin()
time.Sleep(time.Second * 5)
f.UnPin()
}
}
func TestSender(t *testing.T) {
bot := DefaultBot(Desktop)
bot.MessageHandler = func(msg *Message) {
if msg.IsSendByGroup() {
fmt.Println(msg.SenderInGroup())
} else {
fmt.Println(msg.Sender())
}
}
if err := bot.Login(); err != nil {
t.Error(err)
return
}
bot.Block()
}
// TestGetUUID
// @description: 获取登录二维码(UUID)
// @param t
func TestGetUUID(t *testing.T) {
bot := DefaultBot(Desktop)
uuid, err := bot.Caller.GetLoginUUID(bot.Context())
if err != nil {
t.Error(err)
return
}
t.Log(uuid)
}
// TestLoginWithUUID
// @description: 使用UUID登录
// @param t
func TestLoginWithUUID(t *testing.T) {
uuid := "oZZsO0Qv8Q=="
bot := DefaultBot(Desktop, WithUUIDOption(uuid))
err := bot.Login()
if err != nil {
t.Errorf("登录失败: %v", err.Error())
return
}
}
Go
1
https://gitee.com/ivy-1996/openwechat.git
git@gitee.com:ivy-1996/openwechat.git
ivy-1996
openwechat
openwechat
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891