chore(deps): upgrade node-telegram-bot-api 0.66 -> 2.1.0 (via adapter v1->v2), audit 0 vuln
This commit is contained in:
1 parent
3e913d8ceb
commit
c6b39ae2bc
4 files changed
+92
-1841
No files matched your search
@@ -1,4 +1,4 @@
|
||||
const TelegramBot = require('node-telegram-bot-api');
|
||||
const { CompatBot } = require('./telegramCompat');
|
||||
const { getSetting, getNowLocal } = require('../config/settingsManager');
|
||||
const { logger } = require('../config/logger');
|
||||
const customerSvc = require('./customerService');
|
||||
@@ -38,7 +38,7 @@ function initTelegram() {
|
||||
return;
|
||||
}
|
||||
|
||||
bot = new TelegramBot(token, { polling: true });
|
||||
bot = new CompatBot(token);
|
||||
|
||||
// ── Wrapper aman: bila Telegram gagal parse Markdown (mis. underscore pada
|
||||
// username pelanggan), kirim ulang otomatis TANPA parse_mode.
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Adapter node-telegram-bot-api v1 -> v2.
|
||||
* v2 (>=2.0) tidak kompatibel dengan v1 (bot.onText, bot.sendMessage(chatId, text, opts), ...).
|
||||
* Adapter ini menyediakan permukaan API v1 yang dipakai services/telegramBot.js,
|
||||
* sehingga file itu tak perlu dirombak. Terisolasi di sini agar mudah dirawat.
|
||||
*/
|
||||
const { Bot } = require('node-telegram-bot-api');
|
||||
|
||||
class CompatBot {
|
||||
constructor(token) {
|
||||
this.token = token;
|
||||
this._bot = new Bot(token);
|
||||
this._msgHandlers = [];
|
||||
this._textHandlers = [];
|
||||
this._cbHandlers = [];
|
||||
this._errHandlers = [];
|
||||
|
||||
// v2 = middleware (bukan EventEmitter). Satu middleware meneruskan ke semua handler v1.
|
||||
this._bot.on('message', (ctx) => this._dispatchMessage(ctx));
|
||||
this._bot.on('callback_query', (ctx) => this._dispatchCallback(ctx));
|
||||
|
||||
// v1 `{ polling: true }` autostart; ditunda agar handler selesai didaftarkan lebih dulu.
|
||||
setImmediate(() => this.startPolling());
|
||||
}
|
||||
|
||||
_run(handler, args) {
|
||||
try {
|
||||
const r = handler(...args);
|
||||
if (r && typeof r.catch === 'function') r.catch((e) => this._emitError(e));
|
||||
} catch (e) {
|
||||
this._emitError(e);
|
||||
}
|
||||
}
|
||||
|
||||
_dispatchMessage(ctx) {
|
||||
const msg = ctx.message;
|
||||
if (!msg) return;
|
||||
for (const h of this._msgHandlers) this._run(h, [msg]);
|
||||
if (typeof msg.text === 'string') {
|
||||
for (const { re, h } of this._textHandlers) {
|
||||
const m = msg.text.match(re);
|
||||
if (m) this._run(h, [msg, m]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_dispatchCallback(ctx) {
|
||||
const q = ctx.callbackQuery;
|
||||
if (!q) return;
|
||||
for (const h of this._cbHandlers) this._run(h, [q]);
|
||||
}
|
||||
|
||||
_emitError(e) {
|
||||
const err = e instanceof Error ? e : new Error(String(e));
|
||||
if (this._errHandlers.length) {
|
||||
for (const h of this._errHandlers) { try { h(err); } catch (_) {} }
|
||||
} else {
|
||||
console.error('Telegram error:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
on(kind, handler) {
|
||||
if (kind === 'message') this._msgHandlers.push(handler);
|
||||
else if (kind === 'callback_query') this._cbHandlers.push(handler);
|
||||
else if (kind === 'polling_error' || kind === 'error') {
|
||||
this._errHandlers.push(handler);
|
||||
this._bot.catch((err) => this._emitError(err));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
onText(re, handler) { this._textHandlers.push({ re, h: handler }); return this; }
|
||||
|
||||
sendMessage(chatId, text, opts) { return this._bot.api.sendMessage({ chat_id: chatId, text, ...(opts || {}) }); }
|
||||
editMessageText(text, opts) { return this._bot.api.editMessageText({ text, ...(opts || {}) }); }
|
||||
answerCallbackQuery(id, opts) { return this._bot.api.answerCallbackQuery({ callback_query_id: id, ...(opts || {}) }); }
|
||||
getMe() { return this._bot.api.getMe(); }
|
||||
deleteWebHook() { return this._bot.api.deleteWebhook({}); }
|
||||
stopPolling() { try { this._bot.stop(); } catch (e) {} }
|
||||
startPolling() { this._bot.startPolling().catch((e) => this._emitError(e)); }
|
||||
isPolling() { return this._bot.isRunning(); }
|
||||
}
|
||||
|
||||
module.exports = { CompatBot };
|
||||
Reference in new issue
Block a user