模块使用:github.com/eatmoreapple/openwechat
代码实现
package main
import (
"fmt"
"time"
"github.com/eatmoreapple/openwechat"
)
func main() {
//设置桌面登录方式
bot := openwechat.DefaultBot(openwechat.Desktop)
//创建登录热加载
bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
reloadStorage := openwechat.NewJsonFileHotReloadStorage("token.json")
err := bot.HotLogin(reloadStorage)
if err != nil {
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)
//查询用户
girlFriend := friends.SearchByRemarkName(1, "嗯嗯")
//发送信息
if girlFriend.Count() > 0 {
girlFriend.SendText("Hello~")
}
//阻塞主程序
err = bot.Block()
if err != nil {
fmt.Println(err)
return
}
}