Skip to content

Commit

Permalink
✨ 钉钉推送富文本
Browse files Browse the repository at this point in the history
我 TG 号还没恢复!气死了,转钉钉推送了!

Signed-off-by: night-raise <[email protected]>
  • Loading branch information
night-raise committed May 9, 2022
1 parent f73ded6 commit 919538e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions api_rssbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def main(self):
< rss.before * 86400
):
if title:
msg += f"--{rss.title}--\n"
msg += f"## {rss.title}\n"
title = False
msg = (
msg
+ f'{str(c_no).zfill(2)}. <a href="{entry.link}">{entry.title}</a>\n'
+ f'{c_no}. <a href="{entry.link}">{entry.title}</a>\n'
)
no += 1
c_no += 1
Expand Down
39 changes: 22 additions & 17 deletions notify_mtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

from utils_env import get_file_path

reg = re.compile(r"<a href=['|\"](.+)['|\"]>(.+)<\s?/a>")


def html2md(content: str) -> str:
return reg.sub(r'[\2](\1)', content)


# 原先的 print 函数和主线程的锁
_print = print
mutex = threading.Lock()
Expand Down Expand Up @@ -121,11 +128,11 @@ def bark(title: str, content: str) -> None:
}
params = ""
for pair in filter(
lambda pairs: pairs[0].startswith("BARK_")
and pairs[0] != "BARK_PUSH"
and pairs[1]
and bark_params.get(pairs[0]),
push_config.items(),
lambda pairs: pairs[0].startswith("BARK_")
and pairs[0] != "BARK_PUSH"
and pairs[1]
and bark_params.get(pairs[0]),
push_config.items(),
):
params += f"{bark_params.get(pair[0])}={pair[1]}&"
if params:
Expand Down Expand Up @@ -166,7 +173,7 @@ def dingding_bot(title: str, content: str) -> None:
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = f'https://oapi.dingtalk.com/robot/send?access_token={push_config.get("DD_BOT_TOKEN")}&timestamp={timestamp}&sign={sign}'
headers = {"Content-Type": "application/json;charset=utf-8"}
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
data = {"msgtype": "markdown", "markdown": {"text": html2md(content), "title": title}}

datas = requests.post(
url=url, data=json.dumps(data), headers=headers, timeout=15
Expand Down Expand Up @@ -366,8 +373,8 @@ def get_access_token(self):

def send_text(self, message, touser="@all"):
send_url = (
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ self.get_access_token()
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ self.get_access_token()
)
send_values = {
"touser": touser,
Expand All @@ -382,8 +389,8 @@ def send_text(self, message, touser="@all"):

def send_mpnews(self, title, message, media_id, touser="@all"):
send_url = (
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ self.get_access_token()
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="
+ self.get_access_token()
)
send_values = {
"touser": touser,
Expand Down Expand Up @@ -453,13 +460,11 @@ def telegram_bot(title: str, content: str) -> None:
}
proxies = None
if push_config.get("TG_PROXY_HOST") and push_config.get("TG_PROXY_PORT"):
if push_config.get("TG_PROXY_AUTH") is not None and "@" not in push_config.get(
"TG_PROXY_HOST"
):
if push_config.get("TG_PROXY_AUTH") is not None and "@" not in push_config.get("TG_PROXY_HOST"):
push_config["TG_PROXY_HOST"] = (
push_config.get("TG_PROXY_AUTH")
+ "@"
+ push_config.get("TG_PROXY_HOST")
push_config.get("TG_PROXY_AUTH")
+ "@"
+ push_config.get("TG_PROXY_HOST")
)
proxyStr = "http://{}:{}".format(
push_config.get("TG_PROXY_HOST"), push_config.get("TG_PROXY_PORT")
Expand Down Expand Up @@ -544,7 +549,7 @@ def send(title: str, content: str) -> None:
hitokoto = push_config.get("HITOKOTO")

text = one() if hitokoto else ""
content += "\n\n" + text
content += "\n\n> " + text

ts = [
threading.Thread(target=mode, args=(title, content), name=mode.__name__)
Expand Down

0 comments on commit 919538e

Please sign in to comment.