From 702f736c9d7eb06f7f44c3b11f772ded3d69e407 Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Mon, 22 May 2023 22:09:16 +0200 Subject: [PATCH] Created the telegram.js integration --- server/integrations/telegram.js | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 server/integrations/telegram.js diff --git a/server/integrations/telegram.js b/server/integrations/telegram.js new file mode 100644 index 00000000..aff53dc6 --- /dev/null +++ b/server/integrations/telegram.js @@ -0,0 +1,39 @@ +const axios = require("axios"); +const {replaceVariables} = require("../util/variables"); + +const defaults = { + finished: "✨ *A speedtest is finished*\nšŸ“ `Ping`: %ping% ms\nšŸ”¼ `Upload`: %upload% Mbps\nšŸ”½ `Download`: %download% Mbps", + failed: "āŒ *A speedtest has failed*\n`Reason`: %error%" +} + +const postWebhook = async (token, chatId, message, triggerActivity) => { + axios.post(`https://api.telegram.org/bot${token}/sendMessage`, { + text: message, chat_id: chatId, parse_mode: "markdown" + }).then(() => "").catch((error) => console.log("Could not send a webhook request to Telegram: " + error.message)); +} + +module.exports = (registerEvent) => { + registerEvent('testFinished', async (integration, data, activity) => { + if (integration.data.send_finished) + await postWebhook(integration.data.token, integration.data.chat_id, + replaceVariables(integration.data.finished_message || defaults.finished, data), activity) + }); + + registerEvent('testFailed', async (integration, error, activity) => { + if (integration.data.send_failed) + await postWebhook(integration.data.token, integration.data.chat_id, + replaceVariables(integration.data.failed_message || defaults.failed, {error}), activity) + }); + + return { + icon: "fa-brands fa-telegram", + fields: [ + {name: "token", type: "text", required: true}, + {name: "chat_id", type: "text", required: true}, + {name: "send_finished", type: "boolean", required: false}, + {name: "finished_message", type: "textarea", required: false, placeholder: "Speedtest: %ping% ms, %download% Mbps, %upload% Mbps"}, + {name: "send_failed", type: "boolean", required: false}, + {name: "error_message", type: "textarea", required: false, placeholder: "Speedtest failed: %error%"} + ] + }; +} \ No newline at end of file