fix: TG notify with direct+proxy fallback

This commit is contained in:
aedes
2026-04-22 09:08:53 +03:00
parent d7cd632076
commit f74e738409

View File

@@ -325,18 +325,29 @@ const TG_BOT_TOKEN = '8489078585:AAH3PrYNBiX-YMZA9XgHT2rE6yaK8BqwCOI';
const TG_CHAT_ID = '6606444796'; const TG_CHAT_ID = '6606444796';
const TG_MESSAGE = 'я сел за комп, 5 мин жди и начинай'; const TG_MESSAGE = 'я сел за комп, 5 мин жди и начинай';
app.post('/api/notify', async (_req, res) => { async function tgSend() {
try { // Try direct first, fall back to VPS proxy
const r = await fetch(`https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`, { const url = `https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`;
const init = {
method: 'POST', method: 'POST',
dispatcher: anthropicAgent, // through VPS proxy (TG may be geo-restricted)
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }), body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
}); };
res.json({ ok: r.ok, status: r.status }); try {
} catch (e) { const r = await fetch(url, init);
res.status(500).json({ ok: false, error: e.message }); 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')); app.listen(8108, () => console.log('sechenov :8108'));