-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
96 lines (78 loc) · 2.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"github.com/eatmoreapple/openwechat"
"github.com/json-iterator/go/extra"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
qrcode "github.com/skip2/go-qrcode"
conf2 "go-wxbot/openwechat/comm/conf"
"go-wxbot/openwechat/comm/global"
msg2 "go-wxbot/openwechat/comm/msg"
"go-wxbot/openwechat/comm/ticker"
)
var (
cfgPath = flag.String("c", "config/prod.yaml", "*.yaml config path")
err error
)
func ConsoleQrCode(uuid string) {
q, _ := qrcode.New("https://login.weixin.qq.com/l/"+uuid, qrcode.Low)
fmt.Println(q.ToString(true))
}
func main() {
extra.RegisterFuzzyDecoders()
flag.Parse()
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("config: %s", *cfgPath)
// 加载配置文件
global.Conf, err = conf2.GetConf(*cfgPath)
if err != nil {
logrus.Fatalf(err.Error())
}
bot := openwechat.DefaultBot(openwechat.Desktop)
//bot := openwechat.DefaultBot(openwechat.Normal) // 桌面模式,上面登录不上的可以尝试切换这种模式
bot.SyncCheckCallback = nil // 关闭心跳
// 注册消息处理函数
bot.MessageHandler = func(msg *openwechat.Message) {
if msg.IsText() && msg.Content == "ping" {
_, err = msg.ReplyText("pong")
if err != nil {
err = errors.Wrapf(err, "ping msg replyText err")
logrus.Error(err.Error())
}
}
// 处理消息
msg2.HandleMsg(msg)
}
// 注册登陆二维码回调
//bot.UUIDCallback = openwechat.PrintlnQrcodeUrl
bot.UUIDCallback = ConsoleQrCode
// 登陆
if err = bot.Login(); err != nil {
logrus.Fatalf("bot.Login err %s", err.Error())
}
// 获取登陆的用户
global.WxSelf, err = bot.GetCurrentUser()
if err != nil {
logrus.Fatalf("GetCurrentUser err: %s ", err.Error())
}
// 获取所有的好友
global.WxFriends, err = global.WxSelf.Friends(true)
if err != nil {
logrus.Fatalf("wx self get friends err: %s ", err.Error())
}
// 获取所有的群组
global.WxGroups, err = global.WxSelf.Groups(true)
if err != nil {
logrus.Fatalf("wx self get groups err: %s ", err.Error())
}
ticker.Ticker()
//Test()
// 阻塞主goroutine, 直到发生异常或者用户主动退出
err = bot.Block()
if err != nil {
err = errors.Wrapf(err, "bot.Block() clash")
logrus.Error(err.Error())
}
}