Skip to content

Commit

Permalink
add telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningRay committed Mar 11, 2022
1 parent 0fc324c commit 00fd6a1
Show file tree
Hide file tree
Showing 3 changed files with 825 additions and 12 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"private": false,
"dependencies": {
"lodash": "^4.17.21",
"node-jsonrpc-client": "^2.0.0"
"node-jsonrpc-client": "^2.0.0",
"node-telegram-bot-api": "^0.56.0"
},
"scripts": {
"build": "tsc",
Expand All @@ -17,6 +18,7 @@
"devDependencies": {
"@types/lodash": "^4.14.179",
"@types/node": "^17.0.21",
"@types/node-telegram-bot-api": "^0.56.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.2"
}
Expand Down
67 changes: 60 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { includes, pull } from "lodash";
import { includes, map, pull } from "lodash";
import { JsonRpc, JsonRpcResult } from "node-jsonrpc-client";
import { readFileSync, writeFile, writeFileSync } from 'fs';
import * as fs from 'fs';
import TelegramBot from "node-telegram-bot-api";

interface ServerDefinition {
name: string;
Expand Down Expand Up @@ -74,30 +76,79 @@ interface ConfigDefition {
upstreamPath: string;
upstreamName: string;
}
telegram?: {
botToken?: string;
}
servers: ServerDefinition[];
checkInterval: number;
}

class TelegramAlertBot {
private bot;
private chatIds: Set<number> = new Set();
private chatIdsPath = '/tmp/chatIds';
constructor(token: string) {
this.bot = new TelegramBot(token, { polling: true })

if (fs.existsSync(this.chatIdsPath)) {
const ids = readFileSync(this.chatIdsPath).toString().split(',')
for (const id of ids) {
this.chatIds.add(parseInt(id));
}
}
console.log(this.chatIds)
this.bot.onText(/\/monitor/, (msg) => {
console.log('received bot instruction')
this.addChatId(msg.chat.id);
this.bot.sendMessage(msg.chat.id, "ready to send alert of godwoken nodes");
})

this.bot.onText(/\/stop/, (msg) => {
this.removeChatId(msg.chat.id);
this.bot.sendMessage(msg.chat.id, "stop sending alert of godwoken nodes");
})
}

private addChatId(id: number) {
this.chatIds.add(id);
console.log(this.chatIds)
writeFileSync(this.chatIdsPath, Array.from(this.chatIds).join(','));
}

private removeChatId(id: number) {
this.chatIds.delete(id);
console.log(this.chatIds)
writeFileSync(this.chatIdsPath, Array.from(this.chatIds).join(','));
}

async send(message: string) {
const promises = []
for (let id of this.chatIds) {
promises.push(this.bot.sendMessage(id, message))
}
return Promise.all(promises);
}
}

class Monitor {
public tipBlockHeight: number = 0;
private checkTimer?: NodeJS.Timeout;
private servers: Server[] = [];
private primaryServer?: Server;
private bot?: TelegramAlertBot;

constructor(private config: ConfigDefition) {
this.servers = config.servers.map(s => new Server(s));
if (config.telegram && config.telegram.botToken) {
this.bot = new TelegramAlertBot(config.telegram.botToken);
}
}

public async start() {
await this.checkVersions();
await this.check();
}

// send alert to telegram.
public async alert(receiver: string, content: string) {

}

// perform a check against all backend servers.
// using the fastest server, which returns max block number, as primary server.
// if no available server found, then issue alert.
Expand All @@ -106,7 +157,9 @@ class Monitor {
const tasks = this.servers.map(s => {
return s.getBlockHeight().catch(e => {
s.state = State.down;
console.error(`${s.name} ${s.url} is down: ${e.message}`);
const msg = `${s.name} ${s.url} is down: ${e.message}`;
console.error(msg);
this.bot?.send(msg);
});
});
await Promise.all(tasks);
Expand Down
Loading

0 comments on commit 00fd6a1

Please sign in to comment.