| import 'dotenv/config'; | |
| function required(name) { | |
| const value = String(process.env[name] || '').trim(); | |
| if (!value) { | |
| throw new Error(`${name} is required`); | |
| } | |
| return value; | |
| } | |
| async function main() { | |
| const token = required('TELEGRAM_BOT_TOKEN'); | |
| const secret = required('TELEGRAM_WEBHOOK_SECRET'); | |
| const baseUrl = required('HF_BACKEND_BASE_URL').replace(/\/+$/, ''); | |
| const webhookUrl = `${baseUrl}/api/telegram/webhook/${secret}`; | |
| const response = await fetch(`https://api.telegram.org/bot${token}/setWebhook`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ | |
| url: webhookUrl, | |
| secret_token: secret, | |
| drop_pending_updates: false, | |
| allowed_updates: ['message', 'edited_message'], | |
| }), | |
| }); | |
| const payload = await response.json().catch(() => ({})); | |
| if (!response.ok || payload?.ok === false) { | |
| throw new Error(payload?.description || `setWebhook failed (${response.status})`); | |
| } | |
| console.log( | |
| JSON.stringify( | |
| { | |
| ok: true, | |
| webhook_url: webhookUrl, | |
| telegram_result: payload.result || true, | |
| }, | |
| null, | |
| 2 | |
| ) | |
| ); | |
| } | |
| main().catch((error) => { | |
| console.error('Failed to set Telegram webhook:', error?.message || String(error)); | |
| process.exit(1); | |
| }); | |