[增强] 支持动态app_token,和多人接收

This commit is contained in:
msincenselee 2020-04-08 17:08:22 +08:00
parent b6a277ac4d
commit 8c8c5d3790

View File

@ -19,8 +19,8 @@ global wechat_lock
wechat_lock = Lock() wechat_lock = Lock()
# 这里可以设置UID # 这里可以设置UID
UID = 'UID_kZguGPBQPWn41Ni9FK4CgPts2Kj' UID = 'UID_kZguGPBQPWn41Ni9FK4CgPts2K'
APP_TOKEN = 'AT_aDuiQu41dmAQV2vUMXOaaTDrWyhKJN2' APP_TOKEN = 'AT_aDuiQu41dmAQV2vUMXOaaTDrWyhKJN2z'
class wechat_thread(Thread): class wechat_thread(Thread):
@ -28,28 +28,29 @@ class wechat_thread(Thread):
采用线程方式不阻塞 采用线程方式不阻塞
""" """
def __init__(self, uid: str, content: str, topic_ids: list = [], url: str = ''): def __init__(self, uids: list, content: str, topic_ids: list = [], url: str = '', app_token=''):
# text消息标题最长为256必填。 # text消息标题最长为256必填。
# desp消息内容最长64Kb可空支持MarkDown。 # desp消息内容最长64Kb可空支持MarkDown。
super(wechat_thread, self).__init__(name="wechat_thread") super(wechat_thread, self).__init__(name="wechat_thread")
self.request_url = "http://wxpusher.zjiecode.com/api/send/message" self.request_url = "http://wxpusher.zjiecode.com/api/send/message"
self.uid = uid self.uids = uids
self.content = content self.content = content
self.topic_ids = topic_ids self.topic_ids = topic_ids
self.url = url self.url = url
self.lock = wechat_lock self.lock = wechat_lock
self.app_token = app_token if len(app_token) > 0 else APP_TOKEN
def run(self): def run(self):
if self.content is None or len(self.content) == 0: if self.content is None or len(self.content) == 0:
return return
params = {} params = {}
params['appToken'] = APP_TOKEN params['appToken'] = self.app_token
params['content'] = self.content params['content'] = self.content
params['contentType'] = 1 params['contentType'] = 1
params['topicIds'] = self.topic_ids params['topicIds'] = self.topic_ids
params['uids'] = [self.uid] params['uids'] = self.uids
params['url'] = self.url params['url'] = self.url
# 发送请求 # 发送请求
@ -65,7 +66,7 @@ class wechat_thread(Thread):
print("wechat_thread sent successful!") print("wechat_thread sent successful!")
def send_wx_msg(content=''): def send_wx_msg(content='', uids=[], app_token=''):
""" """
发送微信Msg 发送微信Msg
:param content: 发送内容 :param content: 发送内容
@ -75,7 +76,11 @@ def send_wx_msg(content=''):
if len(content) == 0: if len(content) == 0:
return return
t = wechat_thread(uid=UID, content=content) # 没有配置的话使用缺省UID
if len(uids) == 0:
uids.append(UID)
t = wechat_thread(uids=uids, content=content, app_token=app_token)
t.daemon = False t.daemon = False
# t.run() # t.run()
t.start() t.start()