|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Telegram messaging adapter. |
| 6 | + * |
| 7 | + * Uses the Telegram Bot API via long polling (no external dependencies). |
| 8 | + */ |
| 9 | + |
| 10 | +const https = require("https"); |
| 11 | + |
| 12 | +module.exports = function createAdapter(config) { |
| 13 | + const TOKEN = process.env[config.credentials.token_env]; |
| 14 | + const ALLOWED = process.env[config.credentials.allowed_env] |
| 15 | + ? process.env[config.credentials.allowed_env].split(",").map((s) => s.trim()) |
| 16 | + : null; |
| 17 | + |
| 18 | + let offset = 0; |
| 19 | + |
| 20 | + function tgApi(method, body) { |
| 21 | + return new Promise((resolve, reject) => { |
| 22 | + const data = JSON.stringify(body); |
| 23 | + const req = https.request( |
| 24 | + { |
| 25 | + hostname: "api.telegram.org", |
| 26 | + path: `/bot${TOKEN}/${method}`, |
| 27 | + method: "POST", |
| 28 | + headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(data) }, |
| 29 | + }, |
| 30 | + (res) => { |
| 31 | + let buf = ""; |
| 32 | + res.on("data", (c) => (buf += c)); |
| 33 | + res.on("end", () => { |
| 34 | + try { resolve(JSON.parse(buf)); } catch { resolve({ ok: false, error: buf }); } |
| 35 | + }); |
| 36 | + }, |
| 37 | + ); |
| 38 | + req.on("error", reject); |
| 39 | + req.write(data); |
| 40 | + req.end(); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + name: "telegram", |
| 46 | + |
| 47 | + async start(onMessage) { |
| 48 | + const me = await tgApi("getMe", {}); |
| 49 | + if (!me.ok) { |
| 50 | + throw new Error(`Failed to connect to Telegram: ${JSON.stringify(me)}`); |
| 51 | + } |
| 52 | + |
| 53 | + const botName = `@${me.result.username}`; |
| 54 | + |
| 55 | + async function poll() { |
| 56 | + try { |
| 57 | + const res = await tgApi("getUpdates", { offset, timeout: 30 }); |
| 58 | + if (res.ok && res.result?.length > 0) { |
| 59 | + for (const update of res.result) { |
| 60 | + offset = update.update_id + 1; |
| 61 | + const msg = update.message; |
| 62 | + if (!msg?.text) continue; |
| 63 | + |
| 64 | + const channelId = String(msg.chat.id); |
| 65 | + if (ALLOWED && !ALLOWED.includes(channelId)) continue; |
| 66 | + |
| 67 | + const userName = msg.from?.first_name || "someone"; |
| 68 | + |
| 69 | + await onMessage({ |
| 70 | + channelId, |
| 71 | + userName, |
| 72 | + text: msg.text, |
| 73 | + async sendTyping() { |
| 74 | + await tgApi("sendChatAction", { chat_id: channelId, action: "typing" }).catch(() => {}); |
| 75 | + }, |
| 76 | + async reply(text) { |
| 77 | + await tgApi("sendMessage", { |
| 78 | + chat_id: channelId, |
| 79 | + text, |
| 80 | + reply_to_message_id: msg.message_id, |
| 81 | + parse_mode: "Markdown", |
| 82 | + }).catch(() => |
| 83 | + tgApi("sendMessage", { chat_id: channelId, text, reply_to_message_id: msg.message_id }), |
| 84 | + ); |
| 85 | + }, |
| 86 | + }); |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (err) { |
| 90 | + console.error("Poll error:", err.message); |
| 91 | + } |
| 92 | + setTimeout(poll, 100); |
| 93 | + } |
| 94 | + |
| 95 | + poll(); |
| 96 | + return botName; |
| 97 | + }, |
| 98 | + }; |
| 99 | +}; |
0 commit comments