引言 目前工作中使用禅道进行项目管理(需求、缺陷等),为了更好的将禅道的信息通知给指定的人员。我们可以利用禅道提供的WebHook功能,但是禅道本身的WebHook是发送的text格式的内容。 我们可以单独利用Flask开发一个Webhook来自定义格式(飞书:卡片、Post等)进行“飞书/钉钉-机器人推送”。
1.代码实现
1.Flask编写WebHook服务
from flask import Flask, request
from flask_cors import CORS
import json, requests
app = Flask(__name__)
# CORS(app)
@app.route("/")
# @app.post("/webhook")
@app.route("/webhook", methods=['GET', 'POST'])
def hello():
# {'objectType': 'story', 'objectID': '6', 'product': ',1,', 'execution': '0', 'action': 'closed', 'actor': 'admin',
# 'date': '2022-01-26 13:28:39', 'comment': '',
# 'text': 'admin关闭了研发需求 [#6::21ssw](http://127.0.0.1/zentao/story-view-6.html)'}
# 'action': 'opened'
# 'action': 'commented'
# 'action': 'changed'
print("start")
# print(request.method) #['GET', 'POST']
myRquestData = request.get_json()
if myRquestData is not None:
print(myRquestData)
feishuNotice(myRquestData) # 嵌入-飞书推送
# {'objectType': 'story', 'objectID': '8', 'product': ',1,', 'execution': '0',
# 'action': 'opened', 'actor': 'admin', 'date': '2022-01-28 19:22:50', 'comment': '', 'text': 'admin创建研发需求 [#8::商城配置PRD](http://127.0.0.1/zentao/story-view-8.html)'}
return json.dumps(myRquestData, indent=4, ensure_ascii=False)
else:
print('无请求数据')
return '无请求数据'
2.飞书推送机器人
def feishuNotice(myRquestData=None):
type = myRquestData['objectType']
if type=='story':
text = myRquestData['text']
title = text.split(" ")[0]
content = text.split(" ")[1]
objectID = myRquestData['objectID']
myLink_index = int(content.index('('))
myLink = content[myLink_index + 1:-1]
print(myLink)
actor = myRquestData['actor']
date = myRquestData['date']
comment = myRquestData['comment']
mydata = {
"msg_type": "interactive",
"card": {
"elements": [
{
"fields": [
{
"is_short": True,
"text": {
"content": f"**👤 创建人:**\n{actor}",
"tag": "lark_md"
}
},
{
"is_short": False,
"text": {
"content": "",
"tag": "lark_md"
}
},
{
"is_short": True,
"text": {
"content": f"**🗂️ 需求内容:**\n{content}",
"tag": "lark_md"
}
},
{
"is_short": True,
"text": {
"content": f"**📚 需求链接:**\n{myLink}",
"tag": "lark_md"
}
},
{
"is_short": False,
"text": {
"content": "",
"tag": "lark_md"
}
},
{
"is_short": True,
"text": {
"content": f"**📅 备注:**\n{comment}",
"tag": "lark_md"
}
},
{
"is_short": True,
"text": {
"content": f"**🕙 创建时间:**\n{date}",
"tag": "lark_md"
}
}
],
"tag": "div"
},
{
"tag": "hr"
},
],
"header": {
"template": "green",
"title": {
"content": f"👍【研发需求】飞书推送-【禅道ID:{objectID}】",
"tag": "plain_text"
}
}
}
}
url = f"https://open.feishu.cn/open-apis/bot/v2/hook/df9隐藏-d506-47d9-803e-872842425da9"
headers = {"Content-Type": "application/json"}
resp = requests.post(url=url, headers=headers, json=mydata, timeout=5).json()
print(resp)
print("type:\t", type)
else:
print("不是需求-不会推送")
return "不是需求-不会推送"
【完整的代码文件】myWebhook.py