用企业微信的API

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
import requests, sys

"""
/etc/crontab 中添加定时任务
1 10 * * * root python "/root/py_script/实现企业微信API发消息.py" >/dev/null 2>&1
每天 10:01 执行脚本
"""


class SendWeiXinWork:
def __init__(self):
self.CORP_ID = "" # 企业号的标识
self.SECRET = "" # 管理组凭证密钥
self.AGENT_ID = # 应用ID
self.token = self.get_token()

def get_token(self):
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
data = {"corpid": self.CORP_ID, "corpsecret": self.SECRET}
req = requests.get(url=url, params=data)
res = req.json()
if res["errmsg"] == "ok":
return res["access_token"]
else:
return res

def send_message(self, to_user, content):
url = (
"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
% self.token
)
data = {
"touser": to_user, # 发送个人就填用户账号
# "toparty": to_user, # 发送组内成员就填部门ID
"msgtype": "text",
"agentid": self.AGENT_ID,
"text": {"content": content},
"safe": "0",
}

req = requests.post(url=url, json=data)
res = req.json()
if res["errmsg"] == "ok":
print("send message succeed")
return "send message succeed"
else:
print("send message error", res)
return res


if __name__ == "__main__":

# 导入时间模块
import datetime

# 获取当前时间
now = datetime.datetime.now()
# 创建一个指定时间的日期对象
date = datetime.datetime(2024, 5, 15, 23, 59, 59)
# 计算时间差
delta = date - now
msg0 = "网站my.freenom.com的域名还有 {} 天就过期, 请提前14天去更新".format(
delta.days
)

SendWeiXinWork = SendWeiXinWork()
SendWeiXinWork.send_message("USER NAME HERE", msg0)