-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleWx.py
415 lines (351 loc) · 15.4 KB
/
ConsoleWx.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/data/software/python3.7/bin/python3
# -*- coding: UTF-8 -*-
import re
import os
import sys
import time
import json
import redis
import pymongo
import requests
import datetime
from queue import Queue
from wxpy import *
import threading
from prompt_toolkit import prompt
from prompt_toolkit.history import FileHistory
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion.word_completer import WordCompleter
class ChatRobot(object):
def __init__(self):
# 实例化,并登录微信
SysType = sys.platform
if SysType == "win32":
bot = Bot(cache_path=True)
else:
bot = Bot(console_qr=2,cache_path=True)
#基本设置
bot.messages.max_history = 10000
bot.auto_mark_as_read = False
saveurl = "http://172.16.9.74:18090/wxdata/"
robotapi_url = "http://10.1.10.77:5000/cc"
#获取当前登录用户名
myself = re.sub(">", "", str(bot.self).split()[1])
allfriends = bot.friends()
allgroup = bot.groups()
friendslist = []
groupslist = []
#所有朋友
for i in allfriends:
name = re.sub(">", "", str(i).split()[1])
friendslist.append(name)
#所有群聊
for g in allgroup:
groupname = re.sub(">", "", str(g).split()[1])
groupslist.append(groupname)
alllist = friendslist + groupslist
NameCompleter = WordCompleter(alllist, ignore_case=True)
#初始化数据
self.bot = bot
self.myself = myself
self.saveurl = saveurl
self.SysType = SysType
self.friendslist = friendslist
self.groupslist = groupslist
self.NameCompleter = NameCompleter
self.robotapi_url = robotapi_url
self.console = "console"
self.msgtypelist = ["Picture","Recording","Attachment","Video"]
#连接redis
def con_redis(self, host='172.16.9.74', port='16379', db=3):
pool = redis.ConnectionPool(host=host, port=port, db=db)
r = redis.Redis(connection_pool=pool)
return r
def get_user(self, userstr):
name = userstr
result = re.sub(">", "", str(name).split()[1])
return result
#连接mongoDB,并且返回集合对象
def con_mongoDB(self, host='172.16.9.74', port=17974):
mongo_client = pymongo.MongoClient(host=host, port=port)
mon_db = mongo_client['WeChat_chat']
mo_collection = mon_db['history_msg']
return mo_collection
#类似于生产者
def push_to_redis(self, savmsg):
try:
r = self.con_redis()
r.lpush('chat_msg', savmsg)
print("OK, End!!")
except Exception as e:
print(e)
# 类似于消费者
def get_redis_msg(self):
r = self.con_redis()
result = r.brpop('chat_msg', 1)
if result:
return result[1].decode()
else:
return False
def save_to_mongodb(self, insert_content, savedb=True, infomsg=True):
try:
if savedb:
mo_collection = self.con_mongoDB()
result = mo_collection.insert_one(insert_content)
if infomsg:
print("save mongodb success!!!")
except Exception as e:
print(e)
def robot_api(self, inputstr):
try:
request_data = {'input': inputstr}
api_url = self.robotapi_url
result = requests.get(api_url,data=request_data)
return json.loads(result.text)
except Exception as e:
print(e)
def my_robot_api(self, inputmsg):
q = Queue()
myrobot = self.bot.mps().search("飞沉血")[1]
myrobot.send(inputmsg)
@self.bot.register(myrobot)
def get_msg(msg):
print("自动返回的消息为: ", msg)
q.put(msg.text)
send_msg = q.get()
return send_msg
#接收某个好友或群聊消息并且用我的接口自动回复
def Receive_Relpy_My_Msg(self, who):
@self.bot.register(who)
def print_one_messages(msg):
if self.SysType == "win32":
print("\n[{} \033[1;31m接收到的消息 ↙\033[0m]: ".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
print("\n[{} \033[1;31m接收到的消息 ↩\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
if msg.type != "Text":
reply_content = self.my_robot_api("不是文字消息!")
else:
reply_content = self.my_robot_api(msg.text)
msg.reply(reply_content)
replay_datetime = self.get_datetime()
print("自动回复的消息为:[{0}]: {1}".format(replay_datetime, reply_content))
self.bot.join()
#接收某个好友或群聊消息并且自动回复
def Receive_Relpy_Msg(self, who):
@self.bot.register(who, except_self=False)
def print_one_messages(msg):
if self.SysType == "win32":
print("\n[{} \033[1;31m接收到的消息 ↙\033[0m]: ".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
print("\n[{} \033[1;31m接收到的消息 ↩\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
reply_content = self.robot_api(msg.text)
msg.reply(reply_content)
replay_datetime = self.get_datetime()
print("自动回复的消息为:[{0}]: {1}".format(replay_datetime, reply_content))
self.bot.join()
#接收全部消息并且自动回复 此处还需要优化
def ReceiveAll_Relpy_Msg(self):
@self.bot.register(except_self=False)
def print_one_messages(msg):
if self.SysType == "win32":
print("\n[{} \033[1;31m接收到的消息 ↙\033[0m]: ".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
print("\n[{} \033[1;31m接收到的消息 ↩\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
if msg.text not in self.msgtypelist:
reply_content = self.robot_api(msg.text)
msg.reply(reply_content)
replay_datetime = self.get_datetime()
print("自动回复的消息为:[{0}]: {1}".format(replay_datetime, reply_content))
else:
msg.reply("呜呜呜,此消息我还没法回答!!!")
self.bot.join()
#仅仅打印全部消息,不做任何其它操作
def Receive_All(self, savedb=True, infomsg=True):
"""
简单示例:
test = ChatRobot()
test.Receive_All()
savedb == True 时,将保存消息到mongodb, 为False时,不保存,默认为保存消息
infomsg 默认为True,打印保存消息
"""
@self.bot.register(except_self=False)
def print_all_messages(msg):
if self.SysType == "win32":
if savedb:
print("\n[{} \033[1;31m接收所有消息(DB) ↙\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
print("\n[{} \033[1;31m接收所有消息 ↙\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
if savedb:
print("\n[{} \033[1;31m接收所有消息(DB) ↩\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
else:
print("\n[{} \033[1;31m接收所有消息 ↩\033[0m]".format(msg.receive_time), "\033[0;32m{}\033[0m".format(msg))
if savedb:
#将消息保存到mongodb
if infomsg:
print("保存消息到mongodb!!!")
if "Group" not in str(msg.sender):
senders = self.get_user(str(msg.sender))
else:
senders = self.get_user(str(msg.sender)) + ">" + self.get_user(str(msg.member))
receivers = self.get_user(str(msg.receiver))
msgtype = msg.type
#判断消息类型
if msg.type in self.msgtypelist:
saveurl = self.Save_medis_all(msg, True)
msg_content = {
'消息类型': msgtype,
'访问资源的URL': saveurl
}
insert_content = {
'消息发送时间': msg.create_time,
'消息接收时间': msg.receive_time,
'消息发送者': senders,
'消息接收者': receivers,
'消息内容': msg_content
}
if infomsg:
print("需要插入的内容为:", insert_content)
s = threading.Thread(target=self.save_to_mongodb, args=(insert_content, savedb, infomsg,))
s.start()
#保存文件
saveurl = threading.Thread(target=self.Save_medis_all, args=(msg,))
saveurl.start()
else:
if msg.url:
msg_content = {
'消息类型': msgtype,
'文本内容': msg.text,
'消息URL': msg.url
}
insert_content = {
'消息发送时间': msg.create_time,
'消息接收时间': msg.receive_time,
'消息发送者': senders,
'消息接收者': receivers,
'消息内容': msg_content
}
else:
msg_content = {
'消息类型': msgtype,
'文本内容': msg.text
}
insert_content = {
'消息发送时间': msg.create_time,
'消息接收时间': msg.receive_time,
'消息发送者': senders,
'消息接收者': receivers,
'消息内容': msg_content
}
if infomsg:
print("需要插入的内容为:", insert_content)
s = threading.Thread(target=self.save_to_mongodb, args=(insert_content, savedb, infomsg,))
s.start()
self.bot.join()
def Receive_one(self, my_obj):
@self.bot.register(my_obj, except_self=False)
def print_one_messages(msg):
print(msg)
def get_datetime(self):
now = datetime.datetime.now()
nowtime = now.strftime('%Y-%m-%d %H:%M:%S')
return nowtime
def get_func_who(self, userinput):
if userinput not in self.friendslist and userinput not in self.groupslist:
print("您输入的好友或者群聊不存在,请重新输入!")
elif userinput in self.friendslist:
my_friend = self.bot.friends().search(userinput)[0]
return my_friend
elif userinput in self.groupslist:
my_group = self.bot.groups().search(userinput)[0]
return my_group
elif userinput == self.console:
self.get_who_msg()
#获取好友或群聊对象,可以通过接口,也可以通过终端
def get_who_msg(self):
while True:
datetimeinfo = self.get_datetime()
inputflag = datetimeinfo + " {}".format(self.myself)
user_input = prompt('[{}{}]: '.format(inputflag, "@InputUser"),
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=self.NameCompleter,
)
#判断用户输入
if user_input == "":
continue
elif user_input not in self.friendslist and user_input not in self.groupslist:
print("您输入的好友或者群里不存在,请重新输入!")
continue
elif user_input in self.friendslist:
print("您输入的用户为: ", user_input)
my_friend = self.bot.friends().search(user_input)[0]
return my_friend
break
else:
print("你输入的群聊名称为: ", user_input)
my_group = self.bot.groups().search(user_input)[0]
return my_group
break
def Save_medis_all(self, msg, geturl=False):
now = datetime.datetime.now()
nowtime = now.strftime('%Y%m%d%H%M%S')
receive_time = msg.receive_time
msg_sender = re.sub(">", "" ,str(msg.sender).split()[1])
if str(msg.member) == "None":
specific_sender = msg_sender
else:
specific_sender = re.sub(">", "" ,str(msg.member).split()[1])
file_name = specific_sender + "-" + msg.file_name
save_path_name = "media/" + msg_sender
if not os.path.isdir(save_path_name):
os.makedirs(save_path_name)
file_path = save_path_name + "/" + file_name
if geturl == True:
saveurl = self.saveurl + msg_sender + "/" + file_name
return saveurl
else:
msg.get_file(save_path=file_path)
def Print_help(self):
help_info = """
h|help) 打印帮助信息! auto) 开启自动聊天功能
u) 切换会话用户! all) 打印所有聊天信息!
q|exit) 退出当前程序!
"""
print(help_info)
def Console(self):
#建立用户对象
my_obj = self.get_who_msg()
while True:
datetimeinfo = self.get_datetime()
inputflag = datetimeinfo + " {}".format(self.myself)
console_input = prompt('[{}{}]: '.format(inputflag, "@Console"),
history=FileHistory('history.txt'),
auto_suggest=AutoSuggestFromHistory(),
completer=self.NameCompleter,
)
if console_input == "":
continue
elif console_input == "h" or console_input == "help":
self.Print_help()
continue
elif console_input == "all":
print("\nAll messages are about to start receiving !!! ↩\n")
all = threading.Thread(target=self.Receive_All, args=())
all.start()
continue
elif console_input == "auto":
print("开启自动聊天功能!")
r = threading.Thread(target=self.Receive_Relpy_Msg, args=(my_obj,))
r.start()
continue
elif console_input == "u":
my_obj = self.get_who_msg()
continue
elif console_input == "q" or console_input == "exit":
sys.exit(0)
elif console_input == "myrobot":
print("开启我的自动聊天功能!")
r = threading.Thread(target=self.Receive_Relpy_My_Msg, args=(my_obj,))
r.start()
continue