当前位置:   article > 正文

Python 开发一个飞书GitLab提交机器人_python-feishu

python-feishu

程序效果展示

每一个review 按钮 对应一个提交详情 文件差异 点击即可查看
一个push中的多次提交会合并到一个panel里
在这里插入图片描述

EXE配置:

编辑 bin 目录下 conf.json文件
在这里插入图片描述

config.json 预览

在这里插入图片描述

{
    
    "WEB_HOOK":"https://open.feishu.cn/open-apis/bot/v2/hook/2c26ff64-6a70-4b09-96f2-aa036cfe8832",
    "SECRET":"G9rWIBOKysswilBqKREjoc",

    "GITLAB_HOME":"http://192.168.2.50:8090/",
    "GITLAB_TOKEN":"WBUrYRUySkWV2qj418U5",

    "LISTEN_PROJ":"Makeover-Client"

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

第一步 在飞书群组添加机器人

在这里插入图片描述
在这里插入图片描述

第二步 Web_Hook 和 签名

在这里插入图片描述
在这里插入图片描述

第三步 仓库Home地址配置

进入到你的项目仓库里 将网页地址中 ip+端口号 的copy下来

例如 http://192.168.2.50:8090/31/helloworld-client

那么 Home地址就是 http://192.168.2.50:8090/
在这里插入图片描述
在这里插入图片描述

仓库权限Token配置

在这里插入图片描述
点击生成后 上方后出现Token码 Copy到机器人 conf.json配置中
在这里插入图片描述
在这里插入图片描述

项目名称配置

在这里插入图片描述
在这里插入图片描述

运行exe 大功告成

https://download.csdn.net/download/qq_39162566/87447917
在这里插入图片描述

Exe下载

https://download.csdn.net/download/qq_39162566/87447917
运行环境 python 3.x

发送飞书消息源码

import base64
import hmac
import json
import os
import time
from hashlib import sha256
import requests

envDict = json.load(open("./bin/conf.json"))


def SendMsg( text,user="",proj="",branch="",web_urls = [] ):
    timestamp = str(round(time.time()))
    secret = envDict["SECRET"]
    url = envDict["WEB_HOOK"]

    key = f'{timestamp}\n{secret}'
    key_enc = key.encode('utf-8')
    msg = ""
    msg_enc = msg.encode('utf-8')
    hmac_code = hmac.new(key_enc, msg_enc, digestmod=sha256).digest()
    sign = base64.b64encode(hmac_code).decode('utf-8')
    
    # text content
    # payload_message = {
    #     "timestamp": timestamp,
    #     "sign": sign,
    #     "msg_type": "text",
    #     "content": {
    #         "text": text
    #     }
    # }

    # review window
    patchset_msg = '**Subject :** ' + text + '\n' +\
               '**Owner  :** ' + user + '\n' +\
               '**Project :** ' + proj + '\n' +\
               '**Branch :** ' + branch

    payload_message = {
    "msg_type": "interactive",
    "timestamp": timestamp,
    "sign": sign,
    "card": {
        "config": {
            "wide_screen_mode": True
        },
        "elements": [
            {
                "tag": "div",
                "text": {
                    "content": patchset_msg,
                    "tag": "lark_md"
                }
            },
            {
                "tag": "hr"
            },
            {
                "actions": [
                    {
                        "tag": "button",
                        "text": {
                            "content": "Review " + web_urls[0][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[0][0]
                    }
                ],
                "tag": "action"
            }
        ],
        "header": {
            "template": "blue",
            "title": {
                "content": "GitLab Commit",
                "tag": "plain_text"
                }
            }
        }
    }

    #补充额外的提交review按钮
    if len( web_urls ) > 1:
        actions = payload_message['card']['elements'][2]['actions']
        for i in range(1,len(web_urls),1):
            action = {
                        "tag": "button",
                        "text": {
                            "content":  "Review " + web_urls[i][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[i][0]
                    }
            actions.append(action)
    headers = {
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
    print(response.text)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

飞书消息 面板设计

在这里插入图片描述
每一个review 按钮 对应一个提交详情 文件差异 点击即可查看

 patchset_msg = '**Subject :** ' + text + '\n' +\
               '**Owner  :** ' + user + '\n' +\
               '**Project :** ' + proj + '\n' +\
               '**Branch :** ' + branch

    payload_message = {
    "msg_type": "interactive",
    "timestamp": timestamp,
    "sign": sign,
    "card": {
        "config": {
            "wide_screen_mode": True
        },
        "elements": [
            {
                "tag": "div",
                "text": {
                    "content": patchset_msg,
                    "tag": "lark_md"
                }
            },
            {
                "tag": "hr"
            },
            {
                "actions": [
                    {
                        "tag": "button",
                        "text": {
                            "content": "Review " + web_urls[0][1],
                            "tag": "plain_text"
                        },
                        "type": "primary",
                        "url": web_urls[0][0]
                    }
                ],
                "tag": "action"
            }
        ],
        "header": {
            "template": "blue",
            "title": {
                "content": "GitLab Commit",
                "tag": "plain_text"
                }
            }
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/572979
推荐阅读
相关标签
  

闽ICP备14008679号