Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F12944963
D13383.id38682.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
13 KB
Subscribers
None
D13383.id38682.diff
View Options
diff --git a/web/alias-server/config.js b/web/alias-server/config.js
--- a/web/alias-server/config.js
+++ b/web/alias-server/config.js
@@ -10,6 +10,13 @@
},
connectionUrl: 'mongodb://localhost:27017',
},
+ telegram: {
+ maxLength: 4096,
+ tgMsgOptions: {
+ parse_mode: 'MarkdownV2',
+ disable_web_page_preview: true,
+ },
+ },
unconfirmedBlockheight: 100000000,
express: { port: 5000 },
aliasConstants: {
diff --git a/web/alias-server/package.json b/web/alias-server/package.json
--- a/web/alias-server/package.json
+++ b/web/alias-server/package.json
@@ -6,7 +6,8 @@
"scripts": {
"test": "mocha",
"mocks": "node scripts/generateMocks.js",
- "live": "node scripts/aliasTestsLive.js"
+ "live": "node scripts/aliasTestsLive.js",
+ "alert": "node scripts/sendAdminAlert.js"
},
"keywords": [
"ecash",
diff --git a/web/alias-server/scripts/sendAdminAlert.js b/web/alias-server/scripts/sendAdminAlert.js
new file mode 100644
--- /dev/null
+++ b/web/alias-server/scripts/sendAdminAlert.js
@@ -0,0 +1,16 @@
+const { sendAdminAlert } = require('../telegram');
+
+const msg = 'Admin alert test';
+
+async function testAdminAlert(msg) {
+ const adminNotified = await sendAdminAlert(msg);
+ if (adminNotified) {
+ // Exit success
+ process.exit(0);
+ } else {
+ // Exit in error condition
+ process.exit(1);
+ }
+}
+
+testAdminAlert(msg);
diff --git a/web/alias-server/secrets.js.sample b/web/alias-server/secrets.js.sample
deleted file mode 100644
--- a/web/alias-server/secrets.js.sample
+++ /dev/null
@@ -1,6 +0,0 @@
-module.exports = {
- telegram: {
- botId: 'botIdFromTelegramBotfather',
- channelId: 'channelIdCanFindInTelegramWebThenPrefaceWith100',
- },
-};
diff --git a/web/alias-server/secrets.sample.js b/web/alias-server/secrets.sample.js
new file mode 100644
--- /dev/null
+++ b/web/alias-server/secrets.sample.js
@@ -0,0 +1,7 @@
+module.exports = {
+ telegram: {
+ botId: 'botIdFromTelegramBotfather',
+ announcementChatId: 'channelIdCanFindInTelegramWebThenPrefaceWith100',
+ adminAlertChatId: 'ifGroupChatAndNotChannelDoNotPrefaceWith100',
+ },
+};
diff --git a/web/alias-server/telegram.js b/web/alias-server/telegram.js
--- a/web/alias-server/telegram.js
+++ b/web/alias-server/telegram.js
@@ -1,14 +1,21 @@
const secrets = require('./secrets');
+const config = require('./config');
const TelegramBot = require('node-telegram-bot-api');
-const { botId, channelId } = secrets.telegram;
+const { botId, announcementChatId, adminAlertChatId } = secrets.telegram;
+const { telegram } = config;
+const { maxLength, tgMsgOptions } = telegram;
// Create a bot that uses 'polling' to fetch new updates
const telegramBot = new TelegramBot(botId, { polling: true });
module.exports = {
- returnTelegramBotSendMessagePromise: async function (msg, options) {
+ returnTelegramBotSendMessagePromise: async function (
+ msg,
+ chatId = announcementChatId,
+ options = tgMsgOptions,
+ ) {
return new Promise((resolve, reject) => {
- telegramBot.sendMessage(channelId, msg, options).then(
+ telegramBot.sendMessage(chatId, msg, options).then(
result => {
resolve(result);
},
@@ -18,4 +25,32 @@
);
});
},
+ sendAdminAlert: async function sendTestTgMsg(
+ msg,
+ chatId = adminAlertChatId,
+ options = tgMsgOptions,
+ ) {
+ let adminNotified = false;
+ if (msg.length > maxLength) {
+ console.log(
+ `Error msg of ${msg.length}-char exceeds the Telegram API limit of ${maxLength}. Sending short default msg.`,
+ );
+ // Might be some useful info here
+ const msgSummary = msg.slice(0, maxLength - 200);
+ msg =
+ `Admin alert thrown by alias-server. Please check logs.` +
+ '\n\n' +
+ `Summary:\n\n` +
+ msgSummary;
+ }
+ let tgMsgSuccess;
+ try {
+ tgMsgSuccess = await telegramBot.sendMessage(chatId, msg, options);
+ adminNotified = true;
+ console.log(`Admin notification successfully sent.`);
+ } catch (err) {
+ console.log(`Error sending admin notification`, err);
+ }
+ return adminNotified;
+ },
};
diff --git a/web/alias-server/websocket.js b/web/alias-server/websocket.js
--- a/web/alias-server/websocket.js
+++ b/web/alias-server/websocket.js
@@ -11,7 +11,10 @@
removeUnconfirmedTxsFromTxHistory,
generateReservedAliasTxArray,
} = require('./utils');
-const { returnTelegramBotSendMessagePromise } = require('./telegram');
+const {
+ returnTelegramBotSendMessagePromise,
+ sendAdminAlert,
+} = require('./telegram');
const { chronik } = require('./chronik');
const axios = require('axios');
@@ -48,6 +51,12 @@
? log(`New block found: ${wsMsg.blockHash}`)
: log(`Checking for new aliases on startup`);
+ // Test admin alert
+ const blockFoundAdminAlerted = await sendAdminAlert(
+ `Block found testing aliases`,
+ );
+ log(`blockFoundAdminAlerted`, blockFoundAdminAlerted);
+
// Get the valid aliases already in the db
let validAliasesInDb;
try {
@@ -58,19 +67,22 @@
.project({ _id: 0 })
.toArray();
log(`${validAliasesInDb.length} valid aliases in database`);
- } catch (error) {
- return log(
- `Error in determining validAliasesInDb, notifying admin and exiting parseWebsocketMessage()`,
- error,
- );
- /*
- If this happens,
- - Notify Admin (TODO)
+ } catch (err) {
+ /*
+ - Notify Admin
- Do not finish this function. Leave alias info unchanged
(will be in error state or not displaying the most recent aliases)
- Not displaying the most recent aliases is acceptable if the API does display
the blockheight at which info is valid
*/
+ const errMsg = `Error in determining validAliasesInDb, notifying admin and exiting parseWebsocketMessage()`;
+ log(errMsg, err);
+ const adminNotifyMsg = `${errMsg}\n\n${err}`;
+ const adminNotified = await sendAdminAlert(adminNotifyMsg);
+ if (!adminNotified) {
+ log(`Admin notification failed to send`);
+ }
+ return;
}
// Get the valid aliases already in the db
@@ -88,19 +100,23 @@
);
}
serverState = serverStateArray[0];
- } catch (error) {
- return log(
- `Error in determining serverState, notifying admin and exiting parseWebsocketMessage()`,
- error,
- );
+ } catch (err) {
/*
If this happens,
- - Notify Admin (TODO)
+ - Notify Admin
- Do not finish this function. Leave alias info unchanged
(will be in error state or not displaying the most recent aliases)
- Not displaying the most recent aliases is acceptable if the API does display
the blockheight at which info is valid
*/
+ const errMsg = `Error in determining serverState, notifying admin and exiting parseWebsocketMessage()`;
+ log(errMsg, err);
+ const adminNotifyMsg = `${errMsg}\n\n${err}`;
+ const adminNotified = await sendAdminAlert(adminNotifyMsg);
+ if (!adminNotified) {
+ log(`Admin notification failed to send`);
+ }
+ return;
}
let mostRecentAlias,
@@ -148,9 +164,8 @@
log(
`Fetched ${confirmedTxHistoryInDb.length} confirmed transactions at alias registration address from database`,
);
- } catch (error) {
- log(`Error in determining confirmedTxHistoryInDb`, error);
-
+ } catch (err) {
+ log(`Error in determining confirmedTxHistoryInDb`, err);
log(`Assuming no cached tx history`);
confirmedTxHistoryInDb = [];
}
@@ -305,12 +320,17 @@
`Updated serverState.processedConfirmedTxs from ${processedConfirmedTxs} to ${updatedProcessedConfirmedTxs}.`,
);
} catch (err) {
- log(
- `Error in db.collection(${config.database.collections.serverState}).update(${serverStateQuery}, ${serverStateUpdate})`,
- err,
- );
// If this isn't updated, the server will process too many txs next time
- // Acceptable, no need for failure or admin notification
+ // Let the admin know. This won't impact parsing but will cause processing too many txs
+ const errMsg = `Error in db.collection(${config.database.collections.serverState}).update(${serverStateQuery}, ${serverStateUpdate})`;
+ log(errMsg, err);
+ const adminNotifyMsg = `${errMsg}\n\n${err}`;
+ const adminNotified = await sendAdminAlert(
+ adminNotifyMsg,
+ );
+ if (!adminNotified) {
+ log(`Admin notification failed to send`);
+ }
}
} else {
// If you are writing serverState to the db for the first time
@@ -325,11 +345,17 @@
updatedProcessedConfirmedTxs,
});
} catch (err) {
- log(
- `Error in db.collection(${config.database.collections.serverState}).insertOne({processedConfirmedTxs: ${updatedProcessedConfirmedTxs}}), err`,
- );
// If this isn't updated, the server will process too many txs next time
- // Acceptable, no need for failure or admin notification
+ // Let the admin know. This won't impact parsing but will cause processing too many txs
+ const errMsg = `Error in db.collection(${config.database.collections.serverState}).insertOne({processedConfirmedTxs: ${updatedProcessedConfirmedTxs}}`;
+ log(errMsg, err);
+ const adminNotifyMsg = `${errMsg}\n\n${err}`;
+ const adminNotified = await sendAdminAlert(
+ adminNotifyMsg,
+ );
+ if (!adminNotified) {
+ log(`Admin notification failed to send`);
+ }
}
}
} else {
@@ -415,16 +441,9 @@
`"${alias}"\n` +
`\n` +
`[address](${config.blockExplorer}/address/${address}) | [tx](${config.blockExplorer}/tx/${txid})`;
- // Configure msg parse settings
- let tgMsgOptions = {
- parse_mode: 'markdown',
- disable_web_page_preview: true,
- };
+
const tgBotMsgPromise =
- returnTelegramBotSendMessagePromise(
- tgMsg,
- tgMsgOptions,
- );
+ returnTelegramBotSendMessagePromise(tgMsg);
tgBotMsgPromises.push(tgBotMsgPromise);
}
/*
@@ -442,7 +461,7 @@
);
} catch (err) {
log(
- `Error sending Telegram Bot message for aliases`,
+ `Error sending Telegram Bot alias announcement msg`,
err,
);
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Feb 6, 16:33 (17 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5082705
Default Alt Text
D13383.id38682.diff (13 KB)
Attached To
D13383: [alias-server] Support admin notifications with telegram bot
Event Timeline
Log In to Comment