From f74e73840952b6ce4529f34a88abf705b6e0f882 Mon Sep 17 00:00:00 2001 From: aedes Date: Wed, 22 Apr 2026 09:08:53 +0300 Subject: [PATCH] fix: TG notify with direct+proxy fallback --- server.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index 976fd0e..db845f5 100644 --- a/server.js +++ b/server.js @@ -325,18 +325,29 @@ const TG_BOT_TOKEN = '8489078585:AAH3PrYNBiX-YMZA9XgHT2rE6yaK8BqwCOI'; const TG_CHAT_ID = '6606444796'; const TG_MESSAGE = 'я сел за комп, 5 мин жди и начинай'; -app.post('/api/notify', async (_req, res) => { +async function tgSend() { + // Try direct first, fall back to VPS proxy + const url = `https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`; + const init = { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }), + }; try { - const r = await fetch(`https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`, { - method: 'POST', - dispatcher: anthropicAgent, // through VPS proxy (TG may be geo-restricted) - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }), - }); - res.json({ ok: r.ok, status: r.status }); - } catch (e) { - res.status(500).json({ ok: false, error: e.message }); + const r = await fetch(url, init); + return { ok: r.ok, status: r.status, via: 'direct' }; + } catch (e1) { + try { + const r = await fetch(url, { ...init, dispatcher: anthropicAgent }); + return { ok: r.ok, status: r.status, via: 'proxy' }; + } catch (e2) { + return { ok: false, error: `direct: ${e1.message}; proxy: ${e2.message}` }; + } } +} + +app.post('/api/notify', async (_req, res) => { + res.json(await tgSend()); }); app.listen(8108, () => console.log('sechenov :8108'));