增加push消息合并功能,默认3条一发,超时10s

This commit is contained in:
murmur 2023-03-23 14:56:48 +08:00
parent f2ab3012df
commit c7e679cbb1

View File

@ -9,7 +9,7 @@ import time
import requests import requests
import socket import socket
# import json # import json
from threading import Timer
SERVER = "http://www.pushplus.plus/send" SERVER = "http://www.pushplus.plus/send"
TOKEN = "ff328cba923a4225bc4acd0086a9014c" TOKEN = "ff328cba923a4225bc4acd0086a9014c"
@ -31,36 +31,68 @@ def pp2wx(title,content,description):
# ppmsg["description"]= content # ppmsg["description"]= content
requests.post(SERVER, json=ppmsg) requests.post(SERVER, json=ppmsg)
def s2wx(title,description, content): msgcache=[]
def s2wx(title="",description="", content=""):
"""通过自建msgpusher发送至微信 """通过自建msgpusher发送至微信
当前title值无效 当前title值无效
""" """
# return # return
# 受API限制频繁发送会失败
# 拟合并发送满3条已发送超时时间10s
if len(content):
t=f"""
*时戳*
_{datetime.datetime.now()}_
*信息*
{content}"""
msgcache.append(t)
def send():
c= " \n".join(msgcache[:3])
requests.post("http://push.nmhd.eu.org:12722/push/murmur", json={
"title": title,
"description": description,
"content": c,
"token": "tt"})
if len(msgcache) >3:
send()
msgcache.pop(0)
msgcache.pop(0)
msgcache.pop(0)
t= Timer(10,s2wx).start()
elif (not len(content)) and len(msgcache):
# 等效超时
send()
msgcache.clear()
t= Timer(10,s2wx).start()
# GET 方式 # GET 方式
# res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}" # res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}"
# f"&description={description}&content={content}&token={TOKEN}") # f"&description={description}&content={content}&token={TOKEN}")
# POST 方式 # POST 方式
res = requests.post("http://push.nmhd.eu.org:12722/push/murmur", json={ # res = requests.post("http://push.nmhd.eu.org:12722/push/murmur", json={
"title": title, # "title": title,
"description": description, # "description": description,
"content": f""" # "content": f"""
*时戳* # *时戳:*
_{datetime.datetime.now()}_ # _{datetime.datetime.now()}_
*信息* # *信息:*
{content}""", # {content}""",
"token": "tt" # "token": "tt"
}) # })
try: # try:
res = res.json() # res = res.json()
except: # except:
return # return
else: # else:
if res["success"]: # if res["success"]:
return None # return None
else: # else:
return res["message"] # return res["message"]
t=Timer(10,s2wx).start()
cache = defaultdict(list) cache = defaultdict(list)
offs = 0 # 偏移量 offs = 0 # 偏移量
@ -113,8 +145,7 @@ def update_pairs(addr, ccid, clear=0):
t = f"""*信息:* t = f"""*信息:*
天通终端上线 天通终端上线
CCID:{ccid.decode()} CCID:{ccid.decode()}
地址:{str(addr.getpeername())} 地址:{str(addr.getpeername())}"""
"""
print(t) print(t)
s2wx("","天通终端上线",t) s2wx("","天通终端上线",t)
# p[ccid] = addr # p[ccid] = addr