Skip to content

Instantly share code, notes, and snippets.

@mistymagich
Last active April 15, 2022 06:54
Show Gist options
  • Save mistymagich/1220771c434b18dd778afe2af0d8bea8 to your computer and use it in GitHub Desktop.
Save mistymagich/1220771c434b18dd778afe2af0d8bea8 to your computer and use it in GitHub Desktop.
function doPost(e: GoogleAppsScript.Events.DoPost): void {
// Chatwork APIトークン
const token = "<Chatwork APIトークン>";
// 通知先ルームID
const room_id = "<Chatwork ルームID>";
// チャットワークAPI
const client = ChatWorkClient.factory({ token: token });
// 通知内容データ
const grafanaAlert = JSON.parse(e.postData.contents);
// 通知本文
let body = "[toall]";
grafanaAlert.alerts.forEach((alert) => {
body += `[info][title][${alert.status}] ${alert.labels.alertname}[/title]\n`;
body += "Labels:\n";
for (const property in alert.labels) {
body += ` ${property}: ${alert.labels[property]}\n`;
}
if (alert.annotations) {
body += "Annotations:\n";
for (const property in alert.annotations) {
body += ` ${property}: ${alert.annotations[property]}\n`;
}
}
if (alert.silenceURL) {
body += `Silence alert: ${alert.silenceURL}\n`;
}
if (alert.dashboardURL) {
body += `Go to dashboard: ${alert.dashboardURL}\n`;
}
body += "[/info]\n";
});
// Chatworkへ送信
client.sendMessage({
room_id: room_id,
body: body,
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment