mirror of
https://github.com/gnmyt/myspeed.git
synced 2026-05-02 01:40:42 -05:00
39 lines
1.9 KiB
JavaScript
39 lines
1.9 KiB
JavaScript
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%"}
|
|
]
|
|
};
|
|
} |