| 1 | /** |
| 2 | * Telegram public-channel scraper. |
| 3 | * |
| 4 | * Fetches the public web preview at `https://t.me/s/<channel>` (no auth, |
| 5 | * no Bot API token, no MTProto) and extracts the most recent posts from |
| 6 | * the HTML. Only channels that have web preview enabled are reachable |
| 7 | * this way — most well-known OSINT channels do. |
| 8 | * |
| 9 | * The scraper parses with regex rather than a full DOM library to avoid |
| 10 | * pulling in `jsdom` / `cheerio` just for a handful of fields. The Telegram |
| 11 | * widget markup is stable and well-formed enough for this to be safe. |
| 12 | */ |
| 13 | |
| 14 | export interface TelegramPost { |
| 15 | /** `<channel>/<post-id>` — unique across the scraper. */ |
| 16 | id: string; |
| 17 | /** Channel username (without `@`). */ |
| 18 | channel: string; |
| 19 | /** Plain-text body with HTML stripped and `<br>` converted to newlines. */ |
| 20 | text: string; |
| 21 | /** Permalink to the post on t.me. */ |
| 22 | url: string; |
| 23 | /** ISO-8601 timestamp from the `<time datetime=...>` attribute. */ |
| 24 | datetime: string; |
| 25 | /** View counter as displayed (e.g. `"40.9K"`), or `undefined` if absent. */ |
| 26 | views?: string; |
| 27 | } |
| 28 | |
| 29 | const POST_WRAP_RE = /<div class="tgme_widget_message_wrap[^"]*"[\s\S]*?<\/div>\s*<\/div>/g; |
| 30 | const TEXT_RE = /<div class="tgme_widget_message_text[^"]*"[^>]*>([\s\S]*?)<\/div>/; |
| 31 | const DATE_HREF_RE = /<a class="tgme_widget_message_date"[^>]*href="(https:\/\/t\.me\/[^"]+)"/; |
| 32 | const TIME_RE = /<time[^>]*datetime="([^"]+)"/; |
| 33 | const VIEWS_RE = /<span class="tgme_widget_message_views">([^<]+)<\/span>/; |
| 34 | const POST_DATA_RE = /data-post="([^"]+)"/; |
| 35 | |
| 36 | function stripHtml(html: string): string { |
| 37 | return html |
| 38 | .replace(/<br\s*\/?>/gi, '\n') |
| 39 | .replace(/<a\s[^>]*>([\s\S]*?)<\/a>/gi, '$1') |
| 40 | .replace(/<[^>]+>/g, '') |
| 41 | .replace(/ /g, ' ') |
| 42 | .replace(/&/g, '&') |
| 43 | .replace(/</g, '<') |
| 44 | .replace(/>/g, '>') |
| 45 | .replace(/"/g, '"') |
| 46 | .replace(/'/g, "'") |
| 47 | .replace(/\n{3,}/g, '\n\n') |
| 48 | .trim(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Fetches and parses a single channel. Returns up to `limit` of the most |
| 53 | * recent posts (Telegram's web preview returns them oldest-first, so we |
| 54 | * slice from the tail). |
| 55 | */ |
| 56 | export async function fetchChannel( |
| 57 | channel: string, |
| 58 | limit = 20 |
| 59 | ): Promise<TelegramPost[]> { |
| 60 | const safe = channel.replace(/^@/, '').trim(); |
| 61 | if (!/^[a-zA-Z][a-zA-Z0-9_]{2,31}$/.test(safe)) { |
| 62 | throw new Error(`Invalid Telegram channel name: ${channel}`); |
| 63 | } |
| 64 | |
| 65 | const res = await fetch(`https://t.me/s/${safe}`, { |
| 66 | signal: AbortSignal.timeout(10_000), |
| 67 | headers: { |
| 68 | // Telegram serves the widget HTML to ordinary browser UAs. A bare |
| 69 | // `node-fetch` UA sometimes gets a stripped response. |
| 70 | 'User-Agent': 'Mozilla/5.0 (compatible; OsirisOSINT/1.0; +https://osirisai.live)', |
| 71 | Accept: 'text/html,application/xhtml+xml', |
| 72 | }, |
| 73 | }); |
| 74 | if (!res.ok) throw new Error(`t.me/${safe} HTTP ${res.status}`); |
| 75 | const html = await res.text(); |
| 76 | |
| 77 | const posts: TelegramPost[] = []; |
| 78 | const wrappers = html.match(POST_WRAP_RE) || []; |
| 79 | for (const wrap of wrappers) { |
| 80 | const textMatch = wrap.match(TEXT_RE); |
| 81 | const timeMatch = wrap.match(TIME_RE); |
| 82 | const linkMatch = wrap.match(DATE_HREF_RE); |
| 83 | const dataMatch = wrap.match(POST_DATA_RE); |
| 84 | const viewsMatch = wrap.match(VIEWS_RE); |
| 85 | |
| 86 | // Skip media-only posts (no text body) — geoparse needs words. |
| 87 | if (!textMatch || !timeMatch || !linkMatch || !dataMatch) continue; |
| 88 | |
| 89 | posts.push({ |
| 90 | id: dataMatch[1], |
| 91 | channel: safe, |
| 92 | text: stripHtml(textMatch[1]), |
| 93 | url: linkMatch[1], |
| 94 | datetime: timeMatch[1], |
| 95 | views: viewsMatch?.[1]?.trim(), |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | // Telegram returns oldest-first; the freshest posts are at the tail. |
| 100 | return posts.slice(-limit).reverse(); |
| 101 | } |