引言 目前工作中使用禅道进行项目管理(需求、缺陷等),为了更好的将禅道的信息通知给指定的人员。我们可以利用禅道提供的WebHook功能,但是禅道本身的WebHook是发送的text格式的内容。 我们可以单独利用Flask开发一个Webhook来自定义格式(飞书:卡片、Post等)进行“飞书/钉钉-机器人推送”。

1.代码实现

1.Flask编写WebHook服务

  1. from flask import Flask, request
  2. from flask_cors import CORS
  3. import json, requests
  4. app = Flask(__name__)
  5. # CORS(app)
  6. @app.route("/")
  7. # @app.post("/webhook")
  8. @app.route("/webhook", methods=['GET', 'POST'])
  9. def hello():
  10. # {'objectType': 'story', 'objectID': '6', 'product': ',1,', 'execution': '0', 'action': 'closed', 'actor': 'admin',
  11. # 'date': '2022-01-26 13:28:39', 'comment': '',
  12. # 'text': 'admin关闭了研发需求 [#6::21ssw](http://127.0.0.1/zentao/story-view-6.html)'}
  13. # 'action': 'opened'
  14. # 'action': 'commented'
  15. # 'action': 'changed'
  16. print("start")
  17. # print(request.method) #['GET', 'POST']
  18. myRquestData = request.get_json()
  19. if myRquestData is not None:
  20. print(myRquestData)
  21. feishuNotice(myRquestData) # 嵌入-飞书推送
  22. # {'objectType': 'story', 'objectID': '8', 'product': ',1,', 'execution': '0',
  23. # '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)'}
  24. return json.dumps(myRquestData, indent=4, ensure_ascii=False)
  25. else:
  26. print('无请求数据')
  27. return '无请求数据'

2.飞书推送机器人

  1. def feishuNotice(myRquestData=None):
  2. type = myRquestData['objectType']
  3. if type=='story':
  4. text = myRquestData['text']
  5. title = text.split(" ")[0]
  6. content = text.split(" ")[1]
  7. objectID = myRquestData['objectID']
  8. myLink_index = int(content.index('('))
  9. myLink = content[myLink_index + 1:-1]
  10. print(myLink)
  11. actor = myRquestData['actor']
  12. date = myRquestData['date']
  13. comment = myRquestData['comment']
  14. mydata = {
  15. "msg_type": "interactive",
  16. "card": {
  17. "elements": [
  18. {
  19. "fields": [
  20. {
  21. "is_short": True,
  22. "text": {
  23. "content": f"**👤 创建人:**\n{actor}",
  24. "tag": "lark_md"
  25. }
  26. },
  27. {
  28. "is_short": False,
  29. "text": {
  30. "content": "",
  31. "tag": "lark_md"
  32. }
  33. },
  34. {
  35. "is_short": True,
  36. "text": {
  37. "content": f"**🗂️ 需求内容:**\n{content}",
  38. "tag": "lark_md"
  39. }
  40. },
  41. {
  42. "is_short": True,
  43. "text": {
  44. "content": f"**📚 需求链接:**\n{myLink}",
  45. "tag": "lark_md"
  46. }
  47. },
  48. {
  49. "is_short": False,
  50. "text": {
  51. "content": "",
  52. "tag": "lark_md"
  53. }
  54. },
  55. {
  56. "is_short": True,
  57. "text": {
  58. "content": f"**📅 备注:**\n{comment}",
  59. "tag": "lark_md"
  60. }
  61. },
  62. {
  63. "is_short": True,
  64. "text": {
  65. "content": f"**🕙 创建时间:**\n{date}",
  66. "tag": "lark_md"
  67. }
  68. }
  69. ],
  70. "tag": "div"
  71. },
  72. {
  73. "tag": "hr"
  74. },
  75. ],
  76. "header": {
  77. "template": "green",
  78. "title": {
  79. "content": f"👍【研发需求】飞书推送-【禅道ID:{objectID}】",
  80. "tag": "plain_text"
  81. }
  82. }
  83. }
  84. }
  85. url = f"https://open.feishu.cn/open-apis/bot/v2/hook/df9隐藏-d506-47d9-803e-872842425da9"
  86. headers = {"Content-Type": "application/json"}
  87. resp = requests.post(url=url, headers=headers, json=mydata, timeout=5).json()
  88. print(resp)
  89. print("type:\t", type)
  90. else:
  91. print("不是需求-不会推送")
  92. return "不是需求-不会推送"

【完整的代码文件】myWebhook.py

2.禅道WebHook的配置

image.png

3.效果展示

image.png

image.png