mac 选中gerrit url 发送钉钉机器人请求 通知群里code reviewed

123 阅读1分钟

这里我们采用新建快速操作来实现,脚本采用AppleScript来实现

image.png

on run {input, parameters}

try

set selectedText to input as string

set accessToken to "you_ding_ding_robot_token"

set dingtalkURL to "https://oapi.dingtalk.com/robot/send?access_token=" & accessToken

-- 这里判断是否要追加http,chrome的不会带上,safari会带上
if not (selectedText starts with "http://" or selectedText starts with "https://") then

set selectedText to "http://" & selectedText

end if

set contentText to "Gerrit reviewed: " & selectedText

-- 构造body
set jsonData to "{\"msgtype\": \"text\",

                          \"text\": {

                              \"content\": \"" & contentText & "\"

                          },

                          \"at\": {

                              \"atMobiles\": [\"这里填要@的人的手机号\"],

                              \"isAtAll\": false

                          }}"

-- 这里我们需要获取下当前连接的wifi名,是公司内网的话,要走代理才行,这个卡了我好久,有一天我连自己的热就发送成功了

set wifiName to getCurrentWiFiName()

set shouldUseProxy to false

display notification "获取到的wifi名:" & wifiName with title "操作中..."

-- Check if current WiFi name matches the specific name that requires proxy

if wifiName is "your_dest_wifi_name" then

set shouldUseProxy to true

end if

if shouldUseProxy then

-- Execute CURL command

set proxyAddress to "10.18.10.19"

set proxyPort to "8282"

set curlCmd to "curl -x http://" & proxyAddress & ":" & proxyPort & " --location \"" & dingtalkURL & "\" --header 'Content-Type: application/json' --data " & quoted form of jsonData

    set response to do shell script curlCmd

-- Display response (for testing/debugging purposes)

    display notification "Response: " & response with title "钉钉通知"

    else

        set curlCmd to "curl --location \"" & dingtalkURL & "\" --header 'Content-Type: application/json' --data " & quoted form of jsonData

        set response to do shell script curlCmd

        display notification "Response: " & response with title "钉钉通知"

    end if

    on error errMsg
    display dialog "Error: " & errMsg
    end try
end run

on getCurrentWiFiName()
    try
    set currentWiFi to do shell script "networksetup -getairportnetwork en0 | awk -F': ' '{print $2}'"
    return currentWiFi
    on error
    return ""
    end try
end getCurrentWiFiName

选中gerrit网址右键服务就自动发消息到钉钉群了如图 image.png