Telegram sender says what happened
Success logs tg-ok with the chat, thread and message id; a refusal logs tg-fail with Telegram's own description; network errors and timeouts are logged instead of swallowed. Without this a feed that fails is indistinguishable from a quiet day, which is how the first grouped purchase posts could not be confirmed from the log on 25 September. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -1151,8 +1151,16 @@ async function telegramSend(chatId, text, threadId) {
|
||||
if (!sc.telegramBotToken || !chatId) return;
|
||||
const body = JSON.stringify(Object.assign({ chat_id: chatId, text, parse_mode: 'HTML', disable_web_page_preview: true }, threadId ? { message_thread_id: Number(threadId) } : {}));
|
||||
await new Promise((resolve) => {
|
||||
const rq = https.request({ hostname: 'api.telegram.org', path: '/bot' + sc.telegramBotToken + '/sendMessage', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }, timeout: 10000 }, r => { r.resume(); r.on('end', resolve); });
|
||||
rq.on('error', () => resolve()); rq.on('timeout', () => { rq.destroy(); resolve(); }); rq.end(body);
|
||||
const rq = https.request({ hostname: 'api.telegram.org', path: '/bot' + sc.telegramBotToken + '/sendMessage', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }, timeout: 10000 }, r => {
|
||||
// say what happened: a feed that fails silently is indistinguishable from a quiet day (2026-09-25)
|
||||
let raw = ''; r.setEncoding('utf8'); r.on('data', d => { raw += d; });
|
||||
r.on('end', () => { let j = null; try { j = JSON.parse(raw); } catch (e) {}
|
||||
if (j && j.ok) console.log('tg-ok chat=' + chatId + (threadId ? ' thread=' + threadId : '') + ' message_id=' + (j.result && j.result.message_id));
|
||||
else console.error('tg-fail chat=' + chatId + (threadId ? ' thread=' + threadId : '') + ' http=' + r.statusCode + ' ' + ((j && j.description) || raw.slice(0, 160)));
|
||||
resolve(); });
|
||||
});
|
||||
rq.on('error', e => { console.error('tg-fail chat=' + chatId + ' network: ' + e.message); resolve(); });
|
||||
rq.on('timeout', () => { console.error('tg-fail chat=' + chatId + ' timeout'); rq.destroy(); resolve(); }); rq.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user