使用 钉钉机器人 推送消息。
(title: str, content: str)
| 179 | |
| 180 | |
| 181 | def dingding_bot(title: str, content: str) -> None: |
| 182 | """ |
| 183 | 使用 钉钉机器人 推送消息。 |
| 184 | """ |
| 185 | if not push_config.get("DD_BOT_SECRET") or not push_config.get("DD_BOT_TOKEN"): |
| 186 | print("钉钉机器人 服务的 DD_BOT_SECRET 或者 DD_BOT_TOKEN 未设置!!\n取消推送") |
| 187 | return |
| 188 | print("钉钉机器人 服务启动") |
| 189 | |
| 190 | timestamp = str(round(time.time() * 1000)) |
| 191 | secret_enc = push_config.get("DD_BOT_SECRET").encode("utf-8") |
| 192 | string_to_sign = "{}\n{}".format(timestamp, push_config.get("DD_BOT_SECRET")) |
| 193 | string_to_sign_enc = string_to_sign.encode("utf-8") |
| 194 | hmac_code = hmac.new( |
| 195 | secret_enc, string_to_sign_enc, digestmod=hashlib.sha256 |
| 196 | ).digest() |
| 197 | sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) |
| 198 | url = f'https://oapi.dingtalk.com/robot/send?access_token={push_config.get("DD_BOT_TOKEN")}×tamp={timestamp}&sign={sign}' |
| 199 | headers = {"Content-Type": "application/json;charset=utf-8"} |
| 200 | data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}} |
| 201 | response = requests.post( |
| 202 | url=url, data=json.dumps(data), headers=headers, timeout=15 |
| 203 | ).json() |
| 204 | |
| 205 | if not response["errcode"]: |
| 206 | print("钉钉机器人 推送成功!") |
| 207 | else: |
| 208 | print("钉钉机器人 推送失败!") |
| 209 | |
| 210 | |
| 211 | def feishu_bot(title: str, content: str) -> None: |