forked from KevinGong2013/ggbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (137 loc) · 3.48 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"io/ioutil"
"os"
"github.com/KevinGong2013/ggbot/service"
"github.com/KevinGong2013/ggbot/uuidprocessor"
"github.com/KevinGong2013/wechat"
"github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var logger = logrus.WithFields(logrus.Fields{
"module": "ggbot",
})
// Config ..
type Config struct {
ShowQRCodeOnTerminal bool
FuzzyDiff bool
UniqueGroupMember bool
Features struct {
Assistant struct {
Enable bool
OwnerGGID string
}
Guard struct {
Enable bool
}
Tuling struct {
Enable bool
Key string
}
Xiaoice struct {
Enable bool
}
WebhookService struct {
Enable bool
MsgWebhook string
LoginStateWebhook string
UUIDWebhook string
}
}
}
var config = Config{}
func main() {
tf := logrus.TextFormatter{}
tf.FullTimestamp = true
tf.TimestampFormat = `2006-01-02 15:04:05`
logrus.SetFormatter(&tf)
logrus.SetLevel(logrus.DebugLevel)
path := `conf.yaml`
_, err := os.Stat(path)
if !(err == nil || os.IsExist(err)) {
config.ShowQRCodeOnTerminal = false
config.Features.Tuling.Key = ``
config.Features.Tuling.Enable = false
config.FuzzyDiff = true
data, _ := yaml.Marshal(config)
createFile(path, data)
}
data, _ := ioutil.ReadFile(path)
err = yaml.Unmarshal(data, &config)
if err != nil {
logger.Error(`配置文件不正确`)
}
options := wechat.DefaultConfigure()
// 是否开启联系人模糊匹配
options.FuzzyDiff = config.FuzzyDiff
options.UniqueGroupMember = config.UniqueGroupMember
if config.ShowQRCodeOnTerminal {
options.Processor = uuidprocessor.New()
}
var webhookService *service.Wrapper
if config.Features.WebhookService.Enable {
webhookService = service.NewWrapper(config.Features.WebhookService.UUIDWebhook)
}
if webhookService != nil && len(config.Features.WebhookService.UUIDWebhook) > 0 {
options.Processor = webhookService
}
bot, err := wechat.AwakenNewBot(options)
if err != nil {
panic(err)
}
t := newTuling(config.Features.Tuling.Key, bot)
x := newXiaoice(bot)
a := newAssisant(bot, config.Features.Assistant.OwnerGGID)
g := newGuard(bot)
bot.Handle(`/msg`, func(evt wechat.Event) {
logger.Debug(`begin handle [/msg]`)
data := evt.Data.(wechat.EventMsgData)
if config.Features.Tuling.Enable {
go t.autoReplay(data)
}
if config.Features.Xiaoice.Enable {
go x.autoReplay(data)
}
if config.Features.Guard.Enable {
go g.autoAcceptAddFirendRequest(data)
}
if config.Features.Assistant.Enable {
go a.handle(data)
}
if webhookService != nil && len(config.Features.WebhookService.MsgWebhook) > 0 {
go webhookService.Forward(config.Features.WebhookService.MsgWebhook, data)
}
})
bot.Handle(`/login`, func(arg2 wechat.Event) {
isSuccess := arg2.Data.(int) == 1
if isSuccess && x != nil {
if cs, err := bot.ContactsByNickName(`小冰`); err == nil {
for _, c := range cs {
if c.Type == wechat.Offical {
x.un = c.UserName // 更新小冰的UserName
break
}
}
}
}
if webhookService != nil && len(config.Features.WebhookService.LoginStateWebhook) > 0 {
go webhookService.Forward(config.Features.WebhookService.LoginStateWebhook, arg2.Data)
}
})
bot.Go()
}
func createFile(name string, data []byte) (err error) {
defer func() {
if err != nil {
logger.Error(err)
}
}()
oflag := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
file, err := os.OpenFile(name, oflag, 0666)
if err != nil {
return
}
defer file.Close()
_, err = file.Write(data)
return
}