Last active
May 13, 2022 12:04
-
-
Save Lobbyra/2ab3e18ca4be5ce5a19b0d261935361b to your computer and use it in GitHub Desktop.
Code of a AWS lambda used as proxy for a grafana to telegram alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CHAT_ID = -100424242422442442244242; | |
const TELEGRAM_TOKEN = ""; | |
const TELEGRAM_HOST = "api.telegram.org"; | |
const SPREADSHEET_ID = "" | |
const GOOGLE_CLOUD_KEY = "" | |
const nameUrlMap = new Map(); | |
const https = require('node:https'); | |
async function fetchSheet() { | |
return new Promise((resolve, reject) => { | |
const httpsOptions = { | |
host: "sheets.googleapis.com", | |
path: `/v4/spreadsheets/${SPREADSHEET_ID}?key=${GOOGLE_CLOUD_KEY}&includeGridData=true`, | |
port: 443, | |
method: 'GET', | |
}; | |
const req = https.request(httpsOptions, (res) => { | |
if (res.statusCode < 200 || res.statusCode >= 300) { | |
return reject(new Error('statusCode=' + res.statusCode)); | |
} | |
var body = []; | |
res.on('data', function(chunk) { | |
body.push(chunk); | |
}); | |
res.on('end', function() { | |
try { | |
body = JSON.parse(Buffer.concat(body).toString()); | |
} catch(e) { | |
reject(e); | |
} | |
console.log(body); | |
resolve(body.sheets[0].data[0].rowData); | |
}); | |
}); | |
req.on('error', (e) => { | |
reject(e.message); | |
}); | |
req.end(); | |
}); | |
} | |
async function sendMsg(msg) { | |
var telegramPath; | |
var httpsOptions = {}; | |
console.log(msg); | |
msg = encodeURIComponent(msg); | |
telegramPath = `/bot${TELEGRAM_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${msg}`; | |
return new Promise((resolve, reject) => { | |
const httpsOptions = { | |
host: TELEGRAM_HOST, | |
path: telegramPath, | |
port: 443, | |
method: 'POST', | |
}; | |
const req = https.request(httpsOptions, (res) => { | |
if (res.statusCode < 200 || res.statusCode >= 300) { | |
return reject(new Error('statusCode=' + res.statusCode)); | |
} | |
var body = []; | |
res.on('data', function(chunk) { | |
body.push(chunk); | |
}); | |
res.on('end', function() { | |
try { | |
body = JSON.parse(Buffer.concat(body).toString()); | |
} catch(e) { | |
reject(e); | |
} | |
resolve(body); | |
}); | |
}); | |
req.on('error', (e) => { | |
reject(e.message); | |
}); | |
// send the request | |
req.end(); | |
}); | |
} | |
async function setNameUrlMap() { | |
var colA = ""; | |
var colB = ""; | |
await fetchSheet().then((data) => { | |
for (const [i, row] of data.entries()) { | |
if (i === 0) { | |
continue ; | |
} | |
colA = row.values[0].effectiveValue.stringValue; | |
colB = row.values[1].effectiveValue.stringValue; | |
nameUrlMap.set(colB, colA); | |
} | |
}); | |
} | |
exports.handler = async (event) => { | |
var fullAlert = JSON.parse(event.body); | |
var alertPayload = fullAlert.alerts[0].valueString; | |
if (fullAlert.receiver == "") { | |
await sendMsg("GRAFANA: test"); | |
return ; | |
} | |
var regexUrlFinder = /\{.*url\=(.*)\}/gm; | |
var url = regexUrlFinder.exec(alertPayload)[1]; | |
var status = fullAlert.status; | |
var msg = "ALERTE PING "; | |
var monitoringName = ""; | |
if (status == "resolved") { | |
msg += "✅\n"; | |
} else if (status == "firing") { | |
msg += "🔥\n"; | |
} | |
await setNameUrlMap(); | |
monitoringName = nameUrlMap.get(url); | |
if (monitoringName == undefined) { | |
monitoringName = "Nom du monitoring inconnu\n"; | |
} | |
msg += `- Nom du monitoring = ${monitoringName}\n`; | |
msg += `- URL = [${url}]`; | |
console.log(nameUrlMap); | |
await sendMsg(msg).then((data) => {}) | |
const response = { | |
statusCode: 200, | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample