forked from hawklithm/wechatcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (116 loc) · 3.37 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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/daviddengcn/go-colortext"
"github.com/hawklithm/wechatcmd/ui"
chat "github.com/hawklithm/wechatcmd/wechat"
)
const (
maxChanSize = 50
)
type Config struct {
SaveToFile bool `json:"save_to_file"`
AutoReply bool `json:"auto_reply"`
AutoReplySrc bool `json:"auto_reply_src"`
ReplyMsg []string `json:"reply_msg"`
}
func main() {
ct.Foreground(ct.Green, true)
flag.Parse()
logger := log.New(os.Stdout, "[*🤔 *]->:", log.LstdFlags)
logger.Println("启动...")
fileName := "log.txt"
var logFile *os.File
logFile, err := os.OpenFile(fileName, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
defer logFile.Close()
if err != nil {
logger.Printf("打开文件失败!\n")
}
wxLogger := log.New(logFile, "[*]", log.LstdFlags)
wechat := chat.NewWechat(wxLogger)
if err := wechat.WaitForLogin(); err != nil {
logger.Fatalf("等待失败:%s\n", err.Error())
return
}
srcPath, err := os.Getwd()
if err != nil {
logger.Printf("获得路径失败:%#v\n", err)
}
configFile := path.Join(path.Clean(srcPath), "config.json")
if _, err := os.Stat(configFile); os.IsNotExist(err) {
logger.Fatalln("请提供配置文件:config.json")
return
}
b, err := ioutil.ReadFile(configFile)
if err != nil {
logger.Fatalln("读取文件失败:%#v", err)
return
}
var config *Config
err = json.Unmarshal(b, &config)
logger.Printf("登陆...\n")
wechat.AutoReplyMode = config.AutoReply
wechat.ReplyMsgs = config.ReplyMsg
wechat.AutoReplySrc = config.AutoReplySrc
if err := wechat.Login(); err != nil {
logger.Printf("登陆失败:%v\n", err)
return
}
logger.Printf("配置文件:%+v\n", config)
logger.Println("成功!")
logger.Println("微信初始化成功...")
logger.Println("开启状态栏通知...")
if err := wechat.StatusNotify(); err != nil {
return
}
if err := wechat.GetContacts(); err != nil {
logger.Fatalf("拉取联系人失败:%v\n", err)
return
}
if err := wechat.TestCheck(); err != nil {
logger.Fatalf("检查状态失败:%v\n", err)
return
}
nickNameList := []string{}
userIDList := []string{}
for _, member := range wechat.InitContactList {
nickNameList = append(nickNameList, member.NickName)
userIDList = append(userIDList, member.UserName)
}
for _, member := range wechat.ContactList {
nickNameList = append(nickNameList, member.NickName)
userIDList = append(userIDList, member.UserName)
}
for _, member := range wechat.PublicUserList {
nickNameList = append(nickNameList, member.NickName)
userIDList = append(userIDList, member.UserName)
}
groupIdList := []string{}
for _, user := range userIDList {
if strings.HasPrefix(user, "@@") {
groupIdList = append(groupIdList, user)
}
}
//群成员列表
groupMemberList, err := wechat.GetContactsInBatch(groupIdList)
if err != nil {
logger.Fatal("get batch contact error=", err)
return
}
msgIn := make(chan chat.Message, maxChanSize)
msgOut := make(chan chat.MessageOut, maxChanSize)
imageIn := make(chan chat.MessageImage, maxChanSize)
closeChan := make(chan int, 1)
autoChan := make(chan int, 1)
go wechat.SyncDaemon(msgIn, imageIn)
go wechat.MsgDaemon(msgOut, autoChan)
ui.NewLayout(nickNameList, userIDList, groupMemberList,
wechat.User.NickName,
wechat.User.UserName, msgIn, msgOut, imageIn, closeChan, autoChan, wxLogger)
}