import TelegramBot from 'node-telegram-bot-api';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { executeMCPTool } from './handlers.js';
interface UserSession {
mcpClient: Client;
transport: any;
availableTools: any[];
}
// Transfer command handlers
export function setupTransferHandlers(bot: TelegramBot, userSessions: Map<number, UserSession>) {
// /transfer <to> <amount>
bot.onText(/\/transfer ([^\s]+) ([0-9.]+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const toAddress = match?.[1];
const amount = match?.[2];
if (!toAddress || !amount) {
bot.sendMessage(chatId, '❌ Usage: `/transfer <address> <amount>`\n\nExample: `/transfer 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb 1.5`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
bot.sendMessage(chatId, '⏳ Processing transfer...');
const result = await executeMCPTool(session, 'transfer', {
to: toAddress,
amount: amount
});
bot.sendMessage(chatId, `✅ *Transfer Successful!*\n\n${result.content[0]?.text || 'Transfer completed'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error processing transfer: ${error}`);
}
});
}
// Deploy command handlers
export function setupDeployHandlers(bot: TelegramBot, userSessions: Map<number, UserSession>) {
// /deploy erc20 <name> <symbol>
bot.onText(/\/deploy erc20 ([^\s]+) ([^\s]+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const tokenName = match?.[1];
const tokenSymbol = match?.[2];
if (!tokenName || !tokenSymbol) {
bot.sendMessage(chatId, '❌ Usage: `/deploy erc20 <name> <symbol>`\n\nExample: `/deploy erc20 MyToken MTK`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
bot.sendMessage(chatId, '⏳ Deploying ERC20 token...');
const result = await executeMCPTool(session, 'deploy_erc20_token', {
name: tokenName,
symbol: tokenSymbol,
initialSupply: '1000000' // Default 1M tokens
});
bot.sendMessage(chatId, `✅ *ERC20 Token Deployed!*\n\n${result.content[0]?.text || 'Deployment completed'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error deploying ERC20: ${error}`);
}
});
// /deploy erc721 <name> <symbol>
bot.onText(/\/deploy erc721 ([^\s]+) ([^\s]+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const nftName = match?.[1];
const nftSymbol = match?.[2];
if (!nftName || !nftSymbol) {
bot.sendMessage(chatId, '❌ Usage: `/deploy erc721 <name> <symbol>`\n\nExample: `/deploy erc721 MyNFT MNFT`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
bot.sendMessage(chatId, '⏳ Deploying ERC721 NFT...');
const result = await executeMCPTool(session, 'deploy_erc721', {
name: nftName,
symbol: nftSymbol
});
bot.sendMessage(chatId, `✅ *ERC721 NFT Deployed!*\n\n${result.content[0]?.text || 'Deployment completed'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error deploying ERC721: ${error}`);
}
});
}
// Network and info handlers
export function setupNetworkHandlers(bot: TelegramBot, userSessions: Map<number, UserSession>) {
// /network
bot.onText(/\/network/, async (msg) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
const result = await executeMCPTool(session, 'get_network_info');
bot.sendMessage(chatId, `🌐 *Network Information:*\n\n${result.content[0]?.text || 'Unable to fetch network info'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error getting network info: ${error}`);
}
});
// /gas
bot.onText(/\/gas/, async (msg) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
const result = await executeMCPTool(session, 'get_gas_price');
bot.sendMessage(chatId, `⛽ *Current Gas Price:*\n\n${result.content[0]?.text || 'Unable to fetch gas price'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error getting gas price: ${error}`);
}
});
}
// Token management handlers
export function setupTokenHandlers(bot: TelegramBot, userSessions: Map<number, UserSession>) {
// /token info <address> - Get ERC20 token information
bot.onText(/\/token info (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const tokenAddress = match?.[1]?.trim();
if (!tokenAddress) {
bot.sendMessage(chatId, '❌ Please provide a token address:\n\n`/token info 0xTokenAddress`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
const result = await executeMCPTool(session, 'get_token_info', { tokenAddress });
bot.sendMessage(chatId, `🪙 *Token Information:*\n\n${result.content[0]?.text || 'Unable to fetch token info'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error getting token info: ${error}`);
}
});
// /token mint <address> <amount> - Mint ERC20 tokens
bot.onText(/\/token mint (.+) (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const tokenAddress = match?.[1]?.trim();
const amount = match?.[2]?.trim();
if (!tokenAddress || !amount) {
bot.sendMessage(chatId, '❌ Please provide token address and amount:\n\n`/token mint 0xTokenAddress 1000`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
bot.sendMessage(chatId, '⏳ Minting tokens...');
const result = await executeMCPTool(session, 'mint_tokens', {
tokenAddress,
amount
});
bot.sendMessage(chatId, `✅ *Tokens Minted!*\n\n${result.content[0]?.text || 'Tokens minted successfully'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error minting tokens: ${error}`);
}
});
}
// NFT management handlers
export function setupNFTHandlers(bot: TelegramBot, userSessions: Map<number, UserSession>) {
// /nft info <address> [tokenId] - Get ERC721 NFT information
bot.onText(/\/nft info (.+?)(?: (.+))?$/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const nftAddress = match?.[1]?.trim();
const tokenId = match?.[2]?.trim();
if (!nftAddress) {
bot.sendMessage(chatId, '❌ Please provide an NFT contract address:\n\n`/nft info 0xNFTAddress`\nor\n`/nft info 0xNFTAddress 1`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
const params: any = { nftAddress };
if (tokenId) {
params.tokenId = tokenId;
}
const result = await executeMCPTool(session, 'get_nft_info', params);
bot.sendMessage(chatId, `🖼️ *NFT Information:*\n\n${result.content[0]?.text || 'Unable to fetch NFT info'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error getting NFT info: ${error}`);
}
});
// /nft mint <address> <to> <tokenURI> - Mint NFT
bot.onText(/\/nft mint (.+) (.+) (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const userId = msg.from?.id || 0;
const nftAddress = match?.[1]?.trim();
const to = match?.[2]?.trim();
const tokenURI = match?.[3]?.trim();
if (!nftAddress || !to || !tokenURI) {
bot.sendMessage(chatId, '❌ Please provide NFT address, recipient, and token URI:\n\n`/nft mint 0xNFTAddress 0xRecipient https://metadata.url`', {
parse_mode: 'Markdown'
});
return;
}
try {
const session = userSessions.get(userId);
if (!session) {
bot.sendMessage(chatId, '❌ Please start the bot first with /start');
return;
}
bot.sendMessage(chatId, '⏳ Minting NFT...');
const result = await executeMCPTool(session, 'mint_nft', {
nftAddress,
to,
tokenURI
});
bot.sendMessage(chatId, `✅ *NFT Minted!*\n\n${result.content[0]?.text || 'NFT minted successfully'}`, {
parse_mode: 'Markdown'
});
} catch (error) {
bot.sendMessage(chatId, `❌ Error minting NFT: ${error}`);
}
});
}