import datetime import requests import sys import json as JSON class NotifyLark: def __init__(self): robot_id:str = "" self.url: str = f"https://open.larksuite.com/open-apis/bot/v2/hook/{robot_id}" self.text: str = "Empty Text" self.content: list = [] # [ # [{ # "tag": "text", # "text": self.text # }] # ] def set_text(self, state: str, branch: str, commit_user: str, commit_message: str, commit_hash: str, project_name: str, build_status: str) -> None: self.content.append([{"tag": "text", "text": f"项目名称: {project_name}"}]) # 项目名称 self.content.append([{"tag": "text", "text": f"提交用户: {commit_user}"}]) # 代码提交人 self.content.append([{"tag": "text", "text": f"更新内容: {commit_message}"}]) # 更新内容 if state == "start": self.content.append([{"tag": "text", "text": f"开始构建: {datetime.datetime.now()}"}]) # 开始构建时间 else: self.content.append([{"tag": "text", "text": f"结束构建: {datetime.datetime.now()}"}]) # 结束构建的时间 self.content.append([{"tag": "text", "text": f"构建状态: {build_status}"}]) # 构建状态 def send_message(self) -> None: data = { "msg_type": "post", "content": { "post": { "zh_cn": { "title": "项目更新通知", "content": self.content } } } } headers = {'Content-Type': 'application/json'} html = requests.post(self.url, headers=headers, data=JSON.dumps(data)) print(html.text) x = NotifyLark() # 状态, 分支, 提交人, 提交信息, 提交哈希, 项目名, 构建状态 x.set_text(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7]) x.send_message()