Golangは、指定されたスクリプトの実行をリモートでトリガーするためにWebhookをデプロイします。
ダウンロード
GITHUBアドレス:https://github.com/adnanh/webhook
https://github.com/adnanh/webhook/releasesは、対応する最新バージョンを直接ダウンロードします。
webhook構成
vim webhook.json
使用する場合は備考を削除してください
[
{
"id": "4444", // 这里填写 http://server/hooks/{ID} 对应的是{ID} 值。也是唯一值。
"execute-command": "./test4.sh", // 须要执行的shell文件。
"command-working-directory": "./", // 执行的工作目录。
"pass-arguments-to-command": // 指定获取 post请求参数
[
{
"source": "payload", // 这里是固定,获取请求信息
"name": "text.content" // 这里text.content 是获取 json里面 text:content 里面对应的值。 在测试里面能更直观查看。
}
],
"trigger-rule": // 指定数据格式才执行。
{
"match":
{
"type": "value",
"value": "lswzw", // 这里做的是匹配固定值。
"parameter":
{
"source": "payload", // 获取请求信息
"name": "msgtype" // 获取请求里 json msgtype:对应的参数
}
}
}
},
// 下面是创建一个2222 这个接口, 请求后执行不同的指令。
{
"id": "2222",
"execute-command": "./test2.sh",
"command-working-directory": "./",
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "text.content"
}
],
"trigger-rule":
{
"match":
{
"type": "value",
"value": "lswzw",
"parameter":
{
"source": "payload",
"name": "msgtype"
}
}
}
}
]
実行するスクリプト構成
実行するシェルは次のとおりです。任意のシェルコンテンツを自分で作成できます。$ 1は、渡される最初のパラメーターです。
test2.sh
#!/bin/bash
echo $1 >> test
test4.sh
#!/bin/bash
echo $1 >> test
起動
nohup ./webhook -port 9000 -hotreload -hooks webhook.json -verbose > webhook.log 2>&1 &
注:-hotreloadは、jsonファイルを直接変更して保存した後、構成ファイルを自動的に再読み込みします。サービスを再起動する必要はありません。
コールテスト
curl -H "Content-Type:application/json" -X POST --data '{
"msgtype": "lswzw",
"text":{
"content":"this is lswzw 2222!"
}
}' http://127.0.0.1:9000/hooks/2222
curl -H "Content-Type:application/json" -X POST --data '{
"msgtype": "lswzw",
"text":{
"content":"this is lswzw 4444!"
}
}' http://127.0.0.1:9000/hooks/4444
実行後。生成されたテストファイルをチェックすると、コンテンツから渡された値が表示されます。