|
| 1 | +import { existsSync, readFileSync } from 'node:fs'; |
| 2 | +import { basename, resolve } from 'node:path'; |
| 3 | + |
| 4 | +const TELEGRAM_CAPTION_LIMIT = 1024; |
| 5 | +const TELEGRAM_PHOTO_LIMIT = 10 * 1024 * 1024; |
| 6 | + |
| 7 | +function fail(message) { |
| 8 | + console.error(message); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +function loadReleaseEnvironment() { |
| 13 | + const filename = resolve('.env.release.local'); |
| 14 | + if (!existsSync(filename)) return; |
| 15 | + |
| 16 | + for (const rawLine of readFileSync(filename, 'utf8').split(/\r?\n/u)) { |
| 17 | + const line = rawLine.trim(); |
| 18 | + if (!line || line.startsWith('#')) continue; |
| 19 | + const separator = line.indexOf('='); |
| 20 | + if (separator < 1) continue; |
| 21 | + const key = line.slice(0, separator).trim(); |
| 22 | + let value = line.slice(separator + 1).trim(); |
| 23 | + if ( |
| 24 | + value.length >= 2 |
| 25 | + && ((value.startsWith('"') && value.endsWith('"')) |
| 26 | + || (value.startsWith("'") && value.endsWith("'"))) |
| 27 | + ) value = value.slice(1, -1); |
| 28 | + if (!(key in process.env)) process.env[key] = value; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function parseArguments(argv) { |
| 33 | + const options = { dryRun: false }; |
| 34 | + for (let index = 0; index < argv.length; index += 1) { |
| 35 | + const argument = argv[index]; |
| 36 | + if (argument === '--dry-run') { |
| 37 | + options.dryRun = true; |
| 38 | + continue; |
| 39 | + } |
| 40 | + if (!argument.startsWith('--')) fail(`Unexpected argument: ${argument}`); |
| 41 | + const value = argv[index + 1]; |
| 42 | + if (!value || value.startsWith('--')) fail(`Missing value for ${argument}`); |
| 43 | + options[argument.slice(2)] = value; |
| 44 | + index += 1; |
| 45 | + } |
| 46 | + return options; |
| 47 | +} |
| 48 | + |
| 49 | +loadReleaseEnvironment(); |
| 50 | +const options = parseArguments(process.argv.slice(2)); |
| 51 | +const channel = options.channel; |
| 52 | +const captionFile = options.caption; |
| 53 | +const imageFile = options.image; |
| 54 | + |
| 55 | +if (!channel || !captionFile || !imageFile) { |
| 56 | + fail('Usage: node scripts/publish-telegram-release.mjs --channel @channel --caption notes.txt --image release.png [--dry-run]'); |
| 57 | +} |
| 58 | + |
| 59 | +const captionPath = resolve(captionFile); |
| 60 | +const imagePath = resolve(imageFile); |
| 61 | +if (!existsSync(captionPath)) fail(`Caption file not found: ${captionFile}`); |
| 62 | +if (!existsSync(imagePath)) fail(`Image file not found: ${imageFile}`); |
| 63 | + |
| 64 | +const caption = readFileSync(captionPath, 'utf8').trim(); |
| 65 | +const image = readFileSync(imagePath); |
| 66 | +if (!caption) fail('Caption must not be empty'); |
| 67 | +if (caption.length > TELEGRAM_CAPTION_LIMIT) { |
| 68 | + fail(`Caption is ${caption.length} characters; Telegram allows ${TELEGRAM_CAPTION_LIMIT}`); |
| 69 | +} |
| 70 | +if (image.length > TELEGRAM_PHOTO_LIMIT) { |
| 71 | + fail(`Image is ${image.length} bytes; Telegram sendPhoto allows ${TELEGRAM_PHOTO_LIMIT}`); |
| 72 | +} |
| 73 | + |
| 74 | +if (options.dryRun) { |
| 75 | + console.log(JSON.stringify({ |
| 76 | + channel, |
| 77 | + captionCharacters: caption.length, |
| 78 | + imageBytes: image.length, |
| 79 | + })); |
| 80 | + process.exit(0); |
| 81 | +} |
| 82 | + |
| 83 | +const token = process.env.TELEGRAM_BOT_TOKEN; |
| 84 | +if (!token) fail('TELEGRAM_BOT_TOKEN is missing from .env.release.local'); |
| 85 | + |
| 86 | +const form = new FormData(); |
| 87 | +form.set('chat_id', channel); |
| 88 | +form.set('caption', caption); |
| 89 | +form.set('photo', new Blob([image], { type: 'image/png' }), basename(imagePath)); |
| 90 | + |
| 91 | +const response = await fetch(`https://api.telegram.org/bot${token}/sendPhoto`, { |
| 92 | + method: 'POST', |
| 93 | + body: form, |
| 94 | +}); |
| 95 | +const payload = await response.json(); |
| 96 | +if (!response.ok || !payload.ok) { |
| 97 | + fail(`Telegram publication failed: ${payload.description || response.statusText}`); |
| 98 | +} |
| 99 | + |
| 100 | +const channelName = channel.replace(/^@/u, ''); |
| 101 | +console.log(`https://t.me/${channelName}/${payload.result.message_id}`); |
0 commit comments