-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechatAccount.py
59 lines (52 loc) · 1.79 KB
/
wechatAccount.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
#!/usr/bin/python3
# coding:utf-8
import itchat
import threading
from common.logger import *
class wechatAccount(object):
def __init__(self, noticeGroup="李田所粉丝群"):
self.noticeGroup = noticeGroup
self.instance = itchat.new_instance()
self.status = 0 # 0==available;1==login;2==running;
self.login()
self.start()
def login(self):
if self.status != 0:
return
else:
'''
自动登录,使用缓存
'''
try:
self.instance.auto_login(hotReload=True, enableCmdQR=2)
self.status = 1
except:
pass
def start(self):
if self.status != 1:
return
else:
'''
启动机器人
'''
try:
self.thread = threading.Thread(target=self.wechatRun)
self.thread.setDaemon(True)
self.thread.start()
self.status = 2
except:
pass
def wechatRun(self):
self.instance.run(debug=False)
def sendNotice(self,notice):
if not self.status:
LOG_ERROR("微信模组没有运行,请检查账号是否被封")
print("微信模组没有运行,请检查账号是否被封")
else:
try:
user_name = self.instance.search_chatrooms(name=self.noticeGroup)[0].UserName
except:
LOG_WARN('查询不到"'+self.noticeGroup+'"的群,发送提醒失败!')
print('查询不到"'+self.noticeGroup+'"的群,发送提醒失败!')
return
self.instance.send_msg(notice,toUserName=user_name)