度过了好忙好忙好忙的一段时间,最近闲下来,随便写点东西吧
python使用钉钉机器人输出文本,图片,定时推送
python 控制钉钉机器人
- 在钉钉创建一个三人以上的群,在群里添加一个自定义机器人,随意起名字,加个签(钉钉自动给的,要复制出来)


- 查看机器人复制你的Webhook url

- 钉钉输出文本方法
# 向钉钉输出文本
def to_dingding(text1):
# print(text1)
timestamp = str(round(time.time() * 1000))
secret = '放入你的签'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc,
digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = '放入你的Webhookurl×tamp={}&sign={}'.format(
timestamp, sign)
headers = {
'Content-Type': 'application/json'
}
json = {
"at": {
"atMobiles": [
"放入你的电话号码"
],
"atUserIds": [
"user123"
],
"isAtAll": 'false'
},
"text": {
"content": '搞个机器人为您服务,'+text1+"@放入你的电话号码"
},
"msgtype": "text"
}
resp = requests.post(url=url, headers=headers, json=json)
- 以上就可以调用机器人输出文本了,当然机器人可以输出一些图片文档,利用oss 把图片转成url,就可以放到钉钉机器人进行输出了
def oss(img_key, img_path):
auth = oss2.Auth('您的AccessKeyId', '您的AccessKeySecret·')
bucket = oss2.Bucket(auth, '您的服务器域名', '您的bucket名')
endpoint = 'http://oss-cn-beijing.aliyuncs.com'
bucket = oss2.Bucket(auth, endpoint, '您的endpoint')
res = bucket.put_object_from_file(img_key, img_path)
url = bucket.sign_url('GET', img_key, 90000)
return url
print("upload success!")
# 向钉钉输出异常日志
def to_dingding(text1,xxx):
# print(text1)
timestamp = str(round(time.time() * 1000))
secret = '放入你的签'
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc,
digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = '你的Webhook×tamp={}&sign={}'.format(
timestamp, sign)
headers = {
'Content-Type': 'application/json'
}
json = {
"at": {
"atMobiles": [
"你的电话"
],
"atUserIds": [
"user123"
],
"isAtAll": 'false'
},
"markdown": {
"title":f"您的标题",
"text":f"#### {text1} @你的电话 \n>  "
},
"msgtype": "markdown"
}
resp = requests.post(url=url, headers=headers, json=json)
if __name__=="__main__":
url = oss("照片名字","照片地址")
to_dingding("您要输入的文本",url)
- 当然你也可以设置一个定时任务定时输出一些内容
import os
from datetime import datetime,timedelta
import time
def checkTime(t,start,end):
if t>=start and t<=end:
return True
else:
return False
def timedTask(hour=8,minute=0,second=0):
now=datetime.now()
year=now.year
month=now.month
day=now.day
if hour is not None:
if not checkTime(hour,0,23):
raise '%d is not a good hour'%(hour)
else:
hour=0
if minute is not None:
if not checkTime(minute,0,59):
raise '%d is not a good minute'%(minute)
else:
minute=0
if second is not None:
if not checkTime(second,0,59):
raise '%d is not a good second'%(second)
else:
second=0
newtime=datetime(year,month,day,hour,minute,second,0)
if newtime.timestamp()>now.timestamp():
return newtime
else:
return newtime+timedelta(days=1)
while True:
now = datetime.now()
next_time = timedTask(0,30,0)
time.sleep(time.mktime(next_time.timetuple())-time.mktime(now.timetuple()))
#os.system('python dingding.py')
url = oss("照片名字","照片地址")
to_dingding("您要输入的文本",url)
版权声明:本文为Eddy364原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。