Skip to content

Commit

Permalink
Add rudimentary system status based on remote status.json
Browse files Browse the repository at this point in the history
  • Loading branch information
jfowl committed May 3, 2020
1 parent 0b5be86 commit 2b030d9
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 32 deletions.
17 changes: 8 additions & 9 deletions locales/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ menu_bot_down_title: "Ein Bot funktioniert nicht"
menu_bot_down_text: "Bitte wähle den Bot aus, der nicht funktioniert:"

menu_gabot_down_title: "Ein GroupAgreeBot ist kaput"
menu_gabot_down_text: "<b>Ein GroupAgreeBot geht nicht</b>\n\nVon Zeit zu Zeit geht unser Umfragen-Bot schon mal kaputt. Das liegt hauptsächlich daran, dass zu viele Leute ihn benutzen. Dann regelt Telegram ihm die Möglichkeit zu antworten ab und wir müssen warten. Wenn er also mal wieder nicht antwortet, einfach mal warten. Sollte er länger tot sein, schreib uns einfach.\nMehr dazu <a href=\"https://t.me/WJClubFAQ/5\">here</a>."
menu_gabot_down_text: "<b>Ein GroupAgreeBot geht nicht</b>\n\nVon Zeit zu Zeit geht unser Umfragen-Bot schon mal kaputt. Wir haben aber leider nicht mehr die Zeit und Energie da etwas zu tun. Eine Liste mit Alternativen findest du <a href=\"https://t.me/WJClubFAQ/8\">hier</a>."

menu_speak_to_human_title: "Ich möchte mit einem Menschen sprechen"
menu_speak_to_human_text: "Du möchtest also mit einem echten Menschen sprechen. Schreib dazu einfach etwas in diesen Chat. Es kann nur sein, dass wir etwas brauchen um dir zu antworten, aber damit musst du leben. Du bezahlst uns ja nicht für's antworten."

menu_devlist_signup_title: "I want to be on @devlist"
menu_devlist_signup_text: "<b>Rules to be enlisted on the DevList</b>\n\n1⃣ You have programmed at least ~ 3 bots and can name at least one as
a reference.\n\n2⃣ None of your work is illegal (especially piracy, child porn, etc.) or unfair to the Telegram community (bot nets, spam, fake
likes/members/views, censorship, etc.)\n\n3⃣ Your pricing is realistic and does not contain hidden fees. If you say you are going to work for
$10 per hour, you will only charge $10 per hour. If this changes, notify us, so we can change it on the list.\n\n\n<b>What do I need to
do?</b>\nPlease send us: Your name, if you are a single person or a group, your pricing, references (can be up to 3 total from bots over
channels to weblinks) and a single contact username.\n\n\nThis is how the finished post will look like:\n\n________________________\n👤 Your
Name (Country)\n\n💰 <b>Pricing:</b>\nStarting at X€ per hour (max. Y€ per hour)\n\nℹ️ <b>References:</b>\n@coolbot - Does xy
menu_devlist_signup_text: "<b>Rules to be enlisted on the DevList</b>\n\n1⃣ You have programmed at least ~ 3 bots and can name at least one as
a reference.\n\n2⃣ None of your work is illegal (especially piracy, child porn, etc.) or unfair to the Telegram community (bot nets, spam, fake
likes/members/views, censorship, etc.)\n\n3⃣ Your pricing is realistic and does not contain hidden fees. If you say you are going to work for
$10 per hour, you will only charge $10 per hour. If this changes, notify us, so we can change it on the list.\n\n\n<b>What do I need to
do?</b>\nPlease send us: Your name, if you are a single person or a group, your pricing, references (can be up to 3 total from bots over
channels to weblinks) and a single contact username.\n\n\nThis is how the finished post will look like:\n\n________________________\n👤 Your
Name (Country)\n\n💰 <b>Pricing:</b>\nStarting at X€ per hour (max. Y€ per hour)\n\nℹ️ <b>References:</b>\n@coolbot - Does xy
good\n@newschannel - Informations\n\n📮 <b>Contact:</b> @username\n________________________"

2 changes: 1 addition & 1 deletion locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ menu_bot_down_title: "A bot is not answering"
menu_bot_down_text: "Please select which bot is not working:"

menu_gabot_down_title: "A GroupAgreeBot does not work"
menu_gabot_down_text: "<b>One of the GroupAgreeBots is not working:</b>\n\nOur beloved @GroupAgreeBot (and it's siblings) do have some serious issues and we don't have much time to fix them.\nIf it does not answer you right now, please just re-try later today.\n\nRead more <a href=\"https://t.me/WJClubFAQ/5\">here</a>."
menu_gabot_down_text: "<b>One of the GroupAgreeBots is not working:</b>\n\nOur beloved @GroupAgreeBot (and it's siblings) do have some serious issues and we don't have much time to fix them.\nWe have curated a list of alternatives <a href=\"https://t.me/WJClubFAQ/8\">here</a>."

menu_other_bot_down_title: "Some other bot does not work"
menu_other_bot_down_text: "From time to time a bot can stop answering / working. Why? Well, a bot is just another Telegram user (with special abilities, like those buttons below messages). But instead of a human, a program is chatting with you. This program needs to be run on some computer and can crash from time to time. If the bot does not answer for a long time, it may just not be running at the moment. In this case (unless it is a groupagreebot) just tell us about it."
Expand Down
25 changes: 9 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ bot.use(i18n.middleware())
// Setup menu handling
const menu = require('./menu.js')

bot.start(ctx => {
ctx.replyWithHTML(ctx.i18n.t('support_start'), Markup.inlineKeyboard([
Markup.callbackButton('Coke', 'Coke'),
Markup.callbackButton('Dr Pepper', 'Dr Pepper'),
Markup.callbackButton('Pepsi', 'Pepsi')
]).extra())
})

bot.help(ctx => {
ctx.replyWithHTML(ctx.i18n.t('help'))
Expand All @@ -32,30 +25,30 @@ bot.help(ctx => {


// Handle menu switches
bot.action(/menu\/(.+)/, ctx => {
bot.action(/menu\/(.+)/, async ctx => {
const menuId = ctx.match[1]
if (menu.exists(menuId)) {
const {text, extra} = menu.get(menuId, ctx)
ctx.editMessageText(text, extra)
ctx.answerCbQuery()
const {text, extra} = await menu.get(menuId, ctx)
await ctx.editMessageText(text, extra)
await ctx.answerCbQuery()
} else {
console.error('Attempt to access non-existant menu', menuId)
return ctx.answerCbQuery(`There is no menu named ${menuId}!`)
await ctx.answerCbQuery(`There is no menu named ${menuId}!`)
}
})


bot.inlineQuery((query, ctx) => {
bot.inlineQuery(async (query, ctx) => {

if ([127782573, 147631594].includes(ctx.from.id)) {
ctx.i18n.locale('en')
const results = [menu.getInline('start', ctx),menu.getInline('bot_down', ctx)]
ctx.answerInlineQuery(results, {
const results = [await menu.getInline('start', ctx), await menu.getInline('bot_down', ctx)]
await ctx.answerInlineQuery(results, {
cache_time :1,
is_personal : true
})
} else {
ctx.answerInlineQuery([], {})
await ctx.answerInlineQuery([], {})
}


Expand Down
18 changes: 14 additions & 4 deletions menu.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const menus = require('./menus.json')
const {getStatusForTelegram} = require('./simplemonitor-status.js')

exports.getInline = (key, ctx) => {
exports.getInline = async (key, ctx) => {
const m = menus[key]
const buttons = []
Object.assign(buttons, m.buttons)
Expand All @@ -12,12 +13,16 @@ exports.getInline = (key, ctx) => {
}])
}

let statusText = '';
if (m.include_status) {
statusText = '\n' + await getStatusForTelegram();
}
return {
type: 'article',
id: 'menu_'+key,
title: ctx.i18n.t('menu_'+key+'_title'),
input_message_content: {
message_text: ctx.i18n.t('menu_'+key+'_text'),
message_text: ctx.i18n.t('menu_'+key+'_text')+statusText,
parse_mode: 'HTML',
disable_web_page_preview: true
},
Expand All @@ -27,7 +32,7 @@ exports.getInline = (key, ctx) => {
}
}

exports.get = (key, ctx) => {
exports.get = async (key, ctx) => {
const m = menus[key]
const buttons = []
Object.assign(buttons, m.buttons)
Expand All @@ -38,8 +43,13 @@ exports.get = (key, ctx) => {
callback_data: 'menu/'+val
}])
}

let statusText = '';
if (m.include_status) {
statusText = '\n' + await getStatusForTelegram();
}
return {
text: ctx.i18n.t('menu_'+key+'_text'),
text: ctx.i18n.t('menu_'+key+'_text')+statusText,
extra: {
parse_mode: 'HTML',
disable_web_page_preview: true,
Expand Down
3 changes: 2 additions & 1 deletion menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"other_bot_down": {
"buttons": [],
"linkedMenus": ["start"]
"linkedMenus": ["start"],
"include_status": true
},
"devlist_signup": {
"buttons": [],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"telegraf": "^3.27.1",
"telegraf-i18n": "^6.4.0"
"telegraf-i18n": "^6.4.0",
"node-fetch": "^2.6.0"
}
}
66 changes: 66 additions & 0 deletions simplemonitor-status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const fetch = require('node-fetch');

async function downloadStatus() {
const URL = process.env.STATUS_JSON_URL;
const result = await fetch(URL);
return await result.json();
}

function reformatStatusList(rawStatusObject) {
const monitors = rawStatusObject.monitors;
const hosts = [
'ping-uberspace-wjbots',
'ping-uberspace-alioth',
];
const bots = [
'service-telegram-bot-album-creator',
'service-telegram-bot-getids',
'service-telegram-bot-wjclub-support',
'service-telegram-bot-removekeyboard',
'service-telegram-channel-webfail',
]

return {
hosts: hosts.map(host => reformatStatus(monitors[host], host)),
bots: bots.map(bot => reformatStatus(monitors[bot], bot))
}
}

function reformatStatus(status, name) {
if (status === undefined) {
return {
name: name.replace('ping-','').replace('service-',''),
ok: false,
emoji: '❓',
down_since: 'unknown'
}
}

const ok = status.status === 'OK';
return {
name: name.replace('ping-','').replace('service-',''),
ok: ok,
emoji: ok ? '✅' : '❌',
down_since: ok ? '' : (status.first_failure_time + '')
}
}


function status2TelegramHtml(reformattedStatus, generatedAt) {
const {hosts, bots} = reformattedStatus;
return '\nStatus of our systems:\n<u>Servers:</u>\n'
+ hosts
.map(({name, ok, emoji, down_since}) => emoji + ' ' + name + (ok?'':' ('+down_since+')'))
.join('\n')
+ '\n\n<u>Bots & Channels:</u>\n'
+ bots
.map(({name, ok, emoji, down_since}) => emoji + ' ' + name + (ok?'':' ('+down_since+')'))
.join('\n')
+ '\n\n<i>updated: '+generatedAt+'</i>'
}


exports.getStatusForTelegram = async function() {
const freshStatus = await downloadStatus();
return status2TelegramHtml(reformatStatusList(freshStatus), freshStatus.generated);
}

0 comments on commit 2b030d9

Please sign in to comment.