Skip to content

Commit

Permalink
add flushing
Browse files Browse the repository at this point in the history
  • Loading branch information
21e8 committed Dec 7, 2024
1 parent 3a2de31 commit f36e3f1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "0xalice-tgram-bot",
"version": "0.2.4",
"version": "0.2.5",
"description": "Batched Telegram notification bot for 0xAlice",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">=14.0.0"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -37,6 +40,7 @@
"author": "VBase",
"license": "MIT",
"dependencies": {
"node-fetch": "^3.3.2",
"typescript": "^4.9.5"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export class MessageBatcher {
);
}

public async flush(): Promise<void> {
for (const chatId of this.queues.keys()) {
await this.processBatch(chatId);
}
}

public destroy(): void {
if (this.processInterval) {
clearInterval(this.processInterval);
Expand All @@ -76,3 +82,4 @@ export class MessageBatcher {
this.queues.clear();
}
}

43 changes: 23 additions & 20 deletions src/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
Message,
MessageProcessor,
} from './types';
import fetch from 'node-fetch';

const LEVEL_EMOJIS: Record<NotificationLevel, string> = {
info: 'ℹ️',
Expand All @@ -30,27 +31,29 @@ export class TelegramBatcher implements MessageProcessor {
});

const text = formattedMessages.join('\n\n');
const url = `https://api.telegram.org/bot${this.config.botToken}/sendMessage`;
await this.sendMessage(text);
}

private async sendMessage(text: string): Promise<void> {
if (this.config.development) {
console.log('[Telegram]', text);
return;
}

try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_id: this.config.chatId,
text,
parse_mode: 'HTML',
}),
});

if (!response.ok) {
const error = await response.text();
console.error('Failed to send Telegram message:', error);
}
} catch (error) {
console.error('Error sending Telegram message:', error);
const url = `https://api.telegram.org/bot${this.config.botToken}/sendMessage`;
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: this.config.chatId,
text,
parse_mode: 'HTML',
}),
});
if (!response.ok) {
throw new Error(
`Failed to send message to Telegram: ${response.statusText}`
);
}
}
}
39 changes: 39 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,11 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

data-uri-to-buffer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

debug@^4.1.0, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.4:
version "4.4.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.4.0.tgz#2b3f2aea2ffeb776477460267377dc8710faba8a"
Expand Down Expand Up @@ -1565,6 +1570,14 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

file-entry-cache@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.1.tgz#211b2dd9659cb0394b073e7323ac3c933d522027"
Expand Down Expand Up @@ -1616,6 +1629,13 @@ flatted@^3.2.9:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.2.tgz#adba1448a9841bec72b42c532ea23dbbedef1a27"
integrity sha512-AiwGJM8YcNOaobumgtng+6NHuOqC3A7MixFeDafM3X9cIUM+xUXoS5Vfgf+OihAYe20fxqNM9yPBXJzRtZ/4eA==

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -2450,6 +2470,20 @@ nise@^6.1.1:
just-extend "^6.2.0"
path-to-regexp "^8.1.0"

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -2965,6 +2999,11 @@ walker@^1.0.8:
dependencies:
makeerror "1.0.12"

web-streams-polyfill@^3.0.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b"
integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==

which@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
Expand Down

0 comments on commit f36e3f1

Please sign in to comment.