Gitlab 发送企微机器人消息 - 图1

Gitlab 配置

image.png :::info => 高级用法 开启鉴权 :::

  1. - Gitlab 配置好之后 Secret token
  2. - 在服务端接收推送时验证

实现中转站服务

:::warning 基本需求说明:

  • 可以 接受 gitlab 的 event 推送
  • 可以 发送 消息到微信服务器
  • 稳定 :::

我采用的方式, 腾讯云函数;薅羊毛的方式

  1. import os
  2. import json
  3. from flask import Flask, jsonify, render_template, request, url_for, send_from_directory
  4. from werkzeug.utils import secure_filename
  5. import requests
  6. IS_SERVERLESS = bool(os.environ.get('SERVERLESS'))
  7. print(IS_SERVERLESS)
  8. app = Flask(__name__)
  9. # send_msg_wecom
  10. def msg_send_wecom(md_content):
  11. """msg_send_wecom
  12. {
  13. "msgtype": "markdown",
  14. "markdown": {
  15. "content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
  16. >类型:<font color=\"comment\">用户反馈</font>
  17. >普通用户反馈:<font color=\"comment\">117例</font>
  18. >VIP用户反馈:<font color=\"comment\">15例</font>"
  19. }
  20. }
  21. """
  22. url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
  23. webhooks = ["xxxxxxxxxxxxxxxxxxxxxxxxxxx"] # 多个 webhook 需要推送时
  24. headers = {'Content-Type': 'application/json'}
  25. message = {
  26. "msgtype": "markdown",
  27. "markdown": {
  28. "content": md_content
  29. }
  30. }
  31. for webhook in webhooks:
  32. try:
  33. requests.post(url=url+webhook, headers=headers, json=message)
  34. except Exception as e:
  35. print(e)
  36. @app.route("/from_gitlab_webhook", methods=['POST'])
  37. def say_hello():
  38. data = request.get_json()
  39. event_type = data['object_kind']
  40. resp = {
  41. 'event': event_type,
  42. }
  43. md_content = ""
  44. if event_type == 'push':
  45. resp['commit_msg'] = data['commits'][0]['message'].replace("\n", "")
  46. resp['author'] = data['user_username']
  47. resp['project_name'] = data['project']['name']
  48. resp['project_url'] = data['project']['web_url']
  49. # fit md_content
  50. md_content += "## 文档站 <font color=\"info\">{}</font> 通知: \n\n".format(resp['event'])
  51. md_content += "- 项目: [{}]({}) \n".format(resp['project_name'],resp['project_url'])
  52. md_content += "- 更新人: *{}* \n".format(resp['author'])
  53. md_content += "- 提交信息: {} \n".format(resp['commit_msg'])
  54. elif event_type == 'build':
  55. resp['author'] = data['user']['username']
  56. resp['project_name'] = data['repository']['name']
  57. resp['project_url'] = data['repository']['homepage']
  58. resp['commit_msg'] = data['commit']['message'].replace("\n", "")
  59. resp['build_status'] = data['build_status']
  60. resp['build_duration'] = int(data['build_duration']) + 1
  61. if resp['build_status'] == 'failed':
  62. md_content += "## 文档站 {} <font color=\"warning\">{}</font> 通知: \n\n".format(resp['event'], resp['build_status'])
  63. md_content += "- 项目: [{}]({}) \n".format(resp['project_name'],resp['project_url'])
  64. md_content += "- 请 **{}** 关注最近提交的内容 \n".format(resp['author'])
  65. md_content += "- 提交信息: {} \n".format(resp['commit_msg'])
  66. msg_send_wecom(md_content)
  67. print("INFO: " + json.dumps(resp))
  68. return json.dumps(resp)
  69. @app.route("/")
  70. def index():
  71. return {"code": 200, "msg": "it's ok!"}
  72. # 启动服务,监听 9000 端口,监听地址为 0.0.0.0
  73. app.run(debug=IS_SERVERLESS != True, port=9000, host='0.0.0.0')

创建企微群机器人

成功创建机器人后,会获取到一个 用于推送消息的 webhook Key
image.png :::info => 高级用法 针对推送来源进行 IP 限制 ::: image.png

消息呈现展示

image.png