fix: TG notify with direct+proxy fallback
This commit is contained in:
31
server.js
31
server.js
@@ -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 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 {
|
try {
|
||||||
const r = await fetch(`https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`, {
|
const r = await fetch(url, init);
|
||||||
method: 'POST',
|
return { ok: r.ok, status: r.status, via: 'direct' };
|
||||||
dispatcher: anthropicAgent, // through VPS proxy (TG may be geo-restricted)
|
} catch (e1) {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
try {
|
||||||
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
|
const r = await fetch(url, { ...init, dispatcher: anthropicAgent });
|
||||||
});
|
return { ok: r.ok, status: r.status, via: 'proxy' };
|
||||||
res.json({ ok: r.ok, status: r.status });
|
} catch (e2) {
|
||||||
} catch (e) {
|
return { ok: false, error: `direct: ${e1.message}; proxy: ${e2.message}` };
|
||||||
res.status(500).json({ ok: false, error: e.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'));
|
||||||
|
|||||||
Reference in New Issue
Block a user