telegramConnector.js•1.79 kB
// src/services/telegramConnector.js
const TelegramBot = require('node-telegram-bot-api');
// Read the secret token DIRECTLY from the environment variables
const token = process.env.TELEGRAM_API_TOKEN;
let bot;
async function initialize() {
if (!token) { // Check if the token was provided in the .env file
console.warn('⚠️ Telegram Connector: TELEGRAM_API_TOKEN not found in .env file. Skipping initialization.');
return false;
}
console.log('Initializing Telegram Connector...');
bot = new TelegramBot(token, { polling: true }); // Polling for messages
const me = await bot.getMe();
console.log(`✅ Telegram Connector is ready! Logged in as @${me.username}`);
return true;
}
// 2. Tools Definition: The "menu" of actions for the AI.
const tools = [
{
name: 'telegram.sendMessage',
description: 'Sends a text message to a specified Telegram user or group chat ID.',
parameters: {
type: 'object',
properties: {
chatId: { type: 'string', description: 'The unique identifier for the target chat.' },
message: { type: 'string', description: 'The text of the message to send.' }
},
required: ['chatId', 'message']
}
}
// You could add more tools here like 'telegram.sendPhoto', 'telegram.createInviteLink', etc.
];
// 3. Tool Implementations: The code that actually performs the actions.
const implementations = {
'telegram.sendMessage': async ({ chatId, message }) => {
const response = await bot.sendMessage(chatId, message);
return `Message sent to Telegram chat ${chatId} with message ID ${response.message_id}.`;
}
};
module.exports = {
initialize,
tools,
implementations
};