直接上代码
```python
"""
飞书群机器人发送通知
"""
import time
import urllib3
import datetime
urllib3.disable_warnings()
class DingTalkRobotAlert():
def __init__(self):
self.webhook = webhook_url
self.headers = {Content-Type: application/json; charset=UTF-8}
def post_to_robot(self, post_data):
'''
给钉钉机器人发送请求
:param data:
:return:
'''
try:
resp = requests.request(method="POST", url=self.webhook, data=post_data, headers=self.headers).json()
if resp.get("status_code") == 200:
Logger.info(f"钉钉通知发送成功,msg={resp}")
else:
Logger.warning(f"钉钉通知发送失败,{resp}")
except Exception as e:
Logger.warning("钉钉通知发送异常")
Logger.warning(e)
pass
def send_message(self, proj_name, total, passed, failed, skipped, adress):
# 钉钉机器人通知标题
robot_alert_title = "自动化报告"
# 自定义钉钉通知内容
robot_alert_content = '<font color="#dd0000">🌟ddd!自动化执行情况反馈,请相关测试童鞋注意👏!并及时跟进💪!</font>'
execute_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
btns = [{"title": "allure报告,请点击查看", "actionURL": adress}]
text = (
f"### {robot_alert_content}\n\n"
f"> - **用例执行完毕时间:** <font color='#228B22'>{execute_time}</font>\n"
f"> - **项目名称:** <font color='#228B22'>{proj_name}</font>\n"
f"> - **用例总数:** <font color='#228B22'>{total}</font>\n"
f"> - **通过用例数:** <font color='#228B22'>{passed}</font>\n"
f"> - **失败用例数:** <font color='#228B22'>{failed}</font>\n"
f"> - **跳过用例数:** <font color='#228B22'>{skipped}</font>\n"
)
actioncard = {"title":robot_alert_title,
"text":text,
"btns":btns,
"btn_orientation":"1",
"hide_avatar":"1"}
card = {
"msgtype": "actionCard",
"actionCard": actioncard
}
msg_body = json.dumps(card)
self.post_to_robot(data=msg_body)
return
if __name__ == '__main__':
a = DingTalkRobotAlert()
a.send_message('你的项目名称GoGoGo', 4, 4, 0, 0)
运行后钉钉消息卡片如下: