Project-Image-Uploader/scripts/telegram-test.js
matthias.lotz 86ace42fca feat: Add Telegram Bot standalone test (Phase 1)
- Add FEATURE_PLAN-telegram.md with 6-phase implementation roadmap
- Add scripts/README.telegram.md with step-by-step setup guide
- Add scripts/telegram-test.js standalone test script
- Add scripts/.env.telegram.example template for credentials
- Update .gitignore to protect Telegram credentials

Phase 1 complete: Bot setup, privacy mode configuration, and successful test message delivery.
2025-11-29 20:05:40 +01:00

167 lines
5.8 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* Telegram Bot Standalone Test Script
*
* Testet die grundlegende Funktionalität des Telegram Bots
* ohne Integration in das Backend.
*
* Phase 1: Bot Setup & Standalone-Test
*
* Usage:
* node telegram-test.js
*
* Prerequisites:
* - .env.telegram mit TELEGRAM_BOT_TOKEN und TELEGRAM_CHAT_ID
* - npm install node-telegram-bot-api dotenv
*/
require('dotenv').config({ path: '.env.telegram' });
const TelegramBot = require('node-telegram-bot-api');
// =============================================================================
// Configuration
// =============================================================================
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const CHAT_ID = process.env.TELEGRAM_CHAT_ID;
// =============================================================================
// Validation
// =============================================================================
console.log('🔧 Lade Telegram-Konfiguration...');
if (!BOT_TOKEN) {
console.error('❌ FEHLER: TELEGRAM_BOT_TOKEN ist nicht definiert!');
console.error('');
console.error('Bitte .env.telegram erstellen und Bot-Token eintragen:');
console.error('');
console.error(' TELEGRAM_BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz');
console.error(' TELEGRAM_CHAT_ID=-1001234567890');
console.error('');
console.error('Siehe: scripts/README.telegram.md');
process.exit(1);
}
if (!CHAT_ID) {
console.error('❌ FEHLER: TELEGRAM_CHAT_ID ist nicht definiert!');
console.error('');
console.error('Bitte .env.telegram erstellen und Chat-ID eintragen:');
console.error('');
console.error(' TELEGRAM_BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz');
console.error(' TELEGRAM_CHAT_ID=-1001234567890');
console.error('');
console.error('Chat-ID ermitteln: https://api.telegram.org/bot<TOKEN>/getUpdates');
console.error('Siehe: scripts/README.telegram.md');
process.exit(1);
}
console.log('✅ Konfiguration geladen!');
console.log('');
// =============================================================================
// Bot Initialization
// =============================================================================
console.log('🤖 Verbinde mit Telegram Bot...');
// Create bot instance (polling disabled for one-off script)
const bot = new TelegramBot(BOT_TOKEN, { polling: false });
// =============================================================================
// Main Test Function
// =============================================================================
async function runTest() {
try {
// Step 1: Verify bot connection
const botInfo = await bot.getMe();
console.log('✅ Telegram Bot erfolgreich verbunden!');
console.log('');
console.log('Bot-Details:');
console.log(` Name: ${botInfo.first_name}`);
console.log(` Username: @${botInfo.username}`);
console.log(` ID: ${botInfo.id}`);
console.log('');
// Step 2: Prepare test message
const timestamp = new Date().toLocaleString('de-DE', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const message = `
🤖 Telegram Bot Test
Dies ist eine Test-Nachricht vom Werkstatt Image Uploader Bot.
Status: ✅ Erfolgreich verbunden!
Zeitstempel: ${timestamp}
---
Dieser Bot sendet automatische Benachrichtigungen für den Image Uploader.
`.trim();
// Step 3: Send test message
console.log(`📤 Sende Test-Nachricht an Chat ${CHAT_ID}...`);
const sentMessage = await bot.sendMessage(CHAT_ID, message);
console.log('✅ Nachricht erfolgreich gesendet!');
console.log('');
console.log(`Message-ID: ${sentMessage.message_id}`);
console.log('');
console.log('🎉 Test erfolgreich abgeschlossen!');
console.log('');
console.log('➡️ Nächste Schritte:');
console.log(' 1. Telegram-Gruppe öffnen und Nachricht prüfen');
console.log(' 2. Verschiedene Nachrichtenformate testen (siehe README.telegram.md)');
console.log(' 3. Phase 2 starten: Backend-Integration');
} catch (error) {
console.error('❌ FEHLER beim Senden der Nachricht:');
console.error('');
if (error.response && error.response.body) {
const errorData = error.response.body;
console.error(`Error Code: ${errorData.error_code}`);
console.error(`Description: ${errorData.description}`);
console.error('');
// Helpful error messages
if (errorData.error_code === 401) {
console.error('💡 Lösung: Bot-Token ist ungültig oder falsch.');
console.error(' → BotFather öffnen und Token prüfen');
console.error(' → .env.telegram aktualisieren');
} else if (errorData.error_code === 400 && errorData.description.includes('chat not found')) {
console.error('💡 Lösung: Chat-ID ist falsch oder ungültig.');
console.error(' → Chat-ID erneut ermitteln: https://api.telegram.org/bot<TOKEN>/getUpdates');
console.error(' → Neue Nachricht in Gruppe senden, dann getUpdates aufrufen');
console.error(' → .env.telegram aktualisieren');
} else if (errorData.error_code === 403) {
console.error('💡 Lösung: Bot hat keine Berechtigung, in diese Gruppe zu schreiben.');
console.error(' → Bot zur Gruppe hinzufügen');
console.error(' → Bot als Admin hinzufügen (empfohlen)');
}
} else {
console.error(error.message);
}
console.error('');
console.error('📖 Siehe Troubleshooting: scripts/README.telegram.md');
process.exit(1);
}
}
// =============================================================================
// Execute Test
// =============================================================================
runTest();