forked from qwIvan/microMsg-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
93 lines (76 loc) · 3.21 KB
/
bot.py
File metadata and controls
93 lines (76 loc) · 3.21 KB
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
import threading
import shelve
from wxpy import *
from threading import Lock
from .rule import reg_event, BotSetting
from .logger import logger
settings_lock = Lock()
class EmotionBot(Bot):
class TimeoutException(Exception):
def __init__(self, uuid, status):
self.uuid = uuid
self.status = status
def __init__(self, name=None, need_login=True, timeout_max=15, qr_callback=None, *args, **kwargs):
self.name = name
self.timeout_count = 0 # QR code timeout count
self.setting = None
if need_login:
self.login(timeout_max=timeout_max, qr_callback=qr_callback, *args, **kwargs)
def login(self, timeout_max=15, qr_callback=None, *args, **kwargs):
def _qr_callback(uuid, status, qrcode):
if status == '408':
self.timeout_count += 1
if self.timeout_count > timeout_max:
raise self.TimeoutException(uuid, status)
elif status == '400': # exit thread when time out at QR code waiting for scan
raise self.TimeoutException(uuid, status)
if callable(qr_callback):
qr_callback(uuid, status, qrcode)
super().__init__(qr_callback=_qr_callback, *args, **kwargs)
uin = str(self.self.uin)
with settings_lock:
with shelve.open('settings') as settings:
self.setting = settings.get(uin, None) or BotSetting()
def save_setting(setting, name, value):
setting.__dict__[name] = value
with settings_lock:
with shelve.open('settings') as settings:
settings[uin] = self.setting
logger.info('%s updated setting', self.self.name)
BotSetting.__setattr__ = save_setting
reg_event(self)
def self_msg(self, msg):
try:
self.self.send(msg)
except exceptions.ResponseError:
self.file_helper.send(msg)
class SyncEmotionBot(EmotionBot):
def __init__(self, need_login=True, *args, **kwargs):
super().__init__(need_login=False, *args, **kwargs)
self.uuid_lock = threading.Event()
self.login_lock = threading.Event()
self.timeout_count = 0 # QR code timeout count
self.thread = None
if need_login:
self.login(*args, **kwargs)
def login(self, qr_callback=None, *args, **kwargs):
def _qr_callback(uuid, status, qrcode):
if status == '0':
self.uuid = uuid
self.uuid_lock.set()
if callable(qr_callback):
qr_callback(uuid, status, qrcode)
kwargs.update(qr_callback=_qr_callback)
self.thread = threading.Thread(target=self._login_thread, args=args, kwargs=kwargs)
self.thread.start()
self.uuid_lock.wait() # lock release when QR code uuid got
return self.uuid
def _login_thread(self, *args, **kwargs):
try:
super().login(*args, **kwargs)
except self.TimeoutException as e:
logger.warning('uuid=%s, status=%s, timeout', e.uuid, e.status)
return
self.login_lock.set()
def is_logged(self, timeout=None):
return self.login_lock.wait(timeout)