今天嘗試用零信的 API 實(shí)現(xiàn)對(duì)持續(xù)集成過程的通知功能闯两,順便整理一下這段時(shí)間對(duì)零信 API 的使用經(jīng)驗(yàn)宴霸,隨著更多接口的使用妓柜,這篇文字會(huì)繼續(xù)更新
Github 的 webhook
這種使用方式最簡(jiǎn)單雀摘,直接在 Github 上開啟一個(gè) webhook,綁定在某個(gè)群組上苦掘,每次有人提交了代碼换帜,就會(huì)有一個(gè)自動(dòng)通知
Incoming接口
Incoming 接口是零信官方提供的消息接口,可以推送出一條漂亮的消息鹤啡,完整的 JSON 數(shù)據(jù)格式是:
{
"text": "文本",
"channel": "#頻道名字 或 @用戶名字",
"photoUrl": "圖片 URL",
"attachments": [{
"title": "標(biāo)題",
"description": "描述",
"url": "鏈接",
"color": "warning|info|primary|error|muted|success"
}],
"displayUser": {
"name": "應(yīng)用名稱",
"avatarUrl": "頭像地址"
},
"buttons": [
{
"text": "button label",
"url": "http://domain.com/foo.html",
"action": "action_1",
"callbackUrl": "http://foo.dev/inline-button-handler"
}
]
}
PHP 的代碼是:
$data = [
'text' => $text,
'displayUser' => [
'name' => '測(cè)試服務(wù)器',
'avatarUrl' => "http://oe9f7btce.bkt.clouddn.com/cat.jpg",
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://hooks.pubu.im/services/74q8q92gnxp4irl");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_exec($ch);
curl_close($ch);