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_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'));
|
||||
|
||||
Reference in New Issue
Block a user