Skip to content

Instantly share code, notes, and snippets.

@perryraskin
Last active March 18, 2021 00:43
Show Gist options
  • Save perryraskin/db793c0edf0486068c6db0cd87aaa5fe to your computer and use it in GitHub Desktop.
Save perryraskin/db793c0edf0486068c6db0cd87aaa5fe to your computer and use it in GitHub Desktop.
HOOBS Status Checker
/*
1) Install node-fetch via npm or yarn in the same directory as this file.
2) Create a bot in Telegram via chatting with @BotFather and copy the bot token he provides.
3) Start the chat, then go to https://api.telegram.org/bot<token>/getUpdates in a browser.
4) Send a message to the bot, refresh the webpage, and you should see your chat id in the json response.
5) Fill in BOT_TOKEN and CHAT_ID below.
6) Fill in HOOBS_USERNAME, HOOBS_PASSWORD, and HOOBS_SERVER_URL (e.g. http://192.168.1.100) below.
6) Run this on your HOOBS server and HOOBS will never be down again.
*/
const fetch = require("node-fetch");
const BOT_TOKEN = "";
const CHAT_ID = "";
const TOKEN = null;
const HOOBS_USERNAME = "";
const HOOBS_PASSWORD = "";
const HOOBS_SERVER_URL = "";
function sendTelegram(text) {
const apiUrl = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&parse_mode=Markdown&text=${text}`;
fetch(apiUrl);
}
async function getAuthToken(token) {
let tempToken = token;
if (!tempToken) {
const data = {
username: HOOBS_USERNAME,
password: HOOBS_PASSWORD,
remember: true,
};
const res = await fetch(`${HOOBS_SERVER_URL}/api/auth`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const json = await res.json();
return json.token;
} else return token;
}
async function getStatus() {
const AUTH_TOKEN = await getAuthToken(TOKEN);
fetch(`${HOOBS_SERVER_URL}/api/service`, {
method: "GET",
headers: {
Authorization: AUTH_TOKEN,
},
})
.then((res) => res.json())
.then((data) => {
console.log(data);
const message = `running: ${data.running}\nstatus: ${data.status}`;
if (!data.running) {
sendTelegram("HOOBS STOPPED");
start();
}
});
}
async function start() {
console.log("START HOOBS");
const AUTH_TOKEN = await getAuthToken(TOKEN);
fetch(`${HOOBS_SERVER_URL}/api/service/start`, {
method: "POST",
headers: {
Authorization: AUTH_TOKEN,
},
})
.then((res) => res.json())
.then((data) => {
console.log(data);
sendTelegram("HOOBS STARTED");
});
}
setInterval(() => {
getStatus();
}, 60000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment