Create a new bot on telegram and get bot token, learn more here
1
Create a new bot
Create a new chat with @BotFather
Create a new bot with the command /newbot
Choose a name for your bot
Choose a username for your bot
Safely store your bot token. It should be in the format of, BOT_ID:BOT_SECRET
2
Set up Node.js Telegram bot server
For this example, we will use Node.js to create a bot server, and specifically use the node-telegram-bot-api library.
Install the Telegram bot API library:
npm install node-telegram-bot-api
Create a new file called bot.js and add the following code:
const TelegramBot = require('node-telegram-bot-api');// replace the value below with the Telegram token you receive from @BotFatherconst token = 'YOUR_TELEGRAM_BOT_TOKEN';// Create a bot that uses 'polling' to fetch new updatesconst bot = new TelegramBot(token, { polling: true });
Your app is now ready to receive messages from Telegram!
If your app does not have access to managed wallets, request managed wallets access on your
dashboard here
Privy managed wallets provide a secure way to manage wallets on your backend, allowing you to create and control wallets programmatically. Learn more about getting started with managed wallets.
In this trading bot example, we will create a single wallet for each user that interacts with our bot. We can use the id associated from the sender to verifiably identify the user. This value comes directly from Telegram’s servers and is only sent from verified users.
Create a new wallet for every user that interacts with your bot.
1
Create Telegram bot command
Create a Telegram bot command that will be used to start the bot.
bot.onText(/\/start/, (msg) => { const userId = msg.from.id; bot.sendMessage(msg.chat.id, `Hello from the trading bot!`);});
2
Create a wallet
Create a wallet with the createWallet function. Learn more about the createWallet function here.
bot.onText(/\/start/, async (msg) => { const userId = msg.from.id; const wallet = await privy.walletApi.createWallet({chainType: 'solana'}); bot.sendMessage(msg.chat.id, `Hello from the trading bot! Your wallet address is ${wallet.address}`);});
3
Save wallet to database
Save the wallet id to your database and associate it with the user’s id.
bot.onText(/\/start/, async (msg) => { const userId = msg.from.id; const wallet = await privy.walletApi.createWallet({chainType: 'solana'}); // EXAMPLE: Save wallet id to your database with userId as the key await db.wallets.set(userId.toString(), wallet.id); bot.sendMessage(msg.chat.id, `Hello from the trading bot! Your wallet address is ${wallet.address}`);});
4
(optional) Block users from creating multiple wallets
Block users from creating multiple wallets by checking if a wallet already exists for the user.
bot.onText(/\/start/, async (msg) => { const userId = msg.from.id; // EXAMPLE: Get wallet id from database using userId as the key const walletId = await db.wallets.get(userId.toString()); if (walletId) { bot.sendMessage(msg.chat.id, `You already have a wallet!`); return; } const wallet = await privy.walletApi.createWallet({chainType: 'solana'}); // EXAMPLE: Save wallet id to your database with userId as the key await db.wallets.set(userId.toString(), wallet.id); bot.sendMessage(msg.chat.id, `Hello from the trading bot! Your wallet address is ${wallet.address}`);});
To swap tokens in our trading bot, we will be utilizing the Jupiter Ultra API. With just a few lines of code, we can allow users to swap tokens.
1
Create a swap command
Take in the token address and amount from the user /swap <token_address> <amount>. We will validate the token address and amount before proceeding.
bot.onText(/\/swap (.+) (.+)/, async (msg, match) => { const userId = msg.from.id; const tokenMint = match[1]; // First capture group is token address. (optional) validate this is a valid solana token address const amount = parseFloat(match[2]); // Second capture group is amount. (optional) validate this is a valid number // EXAMPLE: Get wallet id from database using userId as the key const walletId = await db.wallets.get(userId.toString()); const wallet = await privy.walletApi.getWallet({id: walletId}) bot.sendMessage(msg.chat.id, `Your wallet address is ${wallet?.address || 'No wallet found'}`);});
2
Create order
We will use a helper function getJupiterUltraOrder to create an order with the Jupiter Ultra API.
/** * Gets a Jupiter Ultra swap order * @param {Object} params - The parameters for the order * @param {string} params.inputMint - The mint address of the input token * @param {string} params.outputMint - The mint address of the output token * @param {string} params.amount - The amount to swap in lamports/smallest denomination * @param {string} params.taker - The wallet address that will execute the swap * @returns {Promise<Object>} The order details including transaction and requestId */ async function getJupiterUltraOrder({inputMint, outputMint, amount, taker}) { try { const response = await axios({ method: 'get', maxBodyLength: Infinity, url: `${JUPITER_ULTRA_API_URL}/order`, params: { inputMint, outputMint, amount, taker, slippageBps: 200 // 2% slippage tolerance }, headers: { 'Accept': 'application/json' } }); return response.data; } catch (error) { console.error('Error getting Jupiter Ultra order:', error); throw error; } }
Call this helper function in the /swap command.
bot.onText(/\/swap (.+) (.+)/, async (msg, match) => { const userId = msg.from.id; const tokenMint = match[1]; // First capture group is token address. const amount = parseFloat(match[2]); // Second capture group is amount. // ... const lamports = Math.floor(amount * 1e9); const order = await getJupiterUltraOrder({ inputMint: SOL_MINT, // SOL mint address outputMint: tokenMint, amount: lamports.toString(), taker: wallet.address }); // ...});
3
Sign transaction
Sign the transaction with the signTransaction function. Learn more about the signTransaction function here.
bot.onText(/\/swap (.+) (.+)/, async (msg, match) => { const userId = msg.from.id; const tokenMint = match[1]; // First capture group is token address. const amount = parseFloat(match[2]); // Second capture group is amount. // ... const transaction = VersionedTransaction.deserialize(Buffer.from(order.transaction, 'base64')); const {signedTransaction} = await privy.walletApi.solana.signTransaction({ walletId: wallet.id, transaction: transaction }); // ...});
4
Execute transaction
Send the transaction with the executeJupiterUltraOrder function. Learn more here.
bot.onText(/\/swap (.+) (.+)/, async (msg, match) => { const userId = msg.from.id; const tokenMint = match[1]; // First capture group is token address. const amount = parseFloat(match[2]); // Second capture group is amount. // ... const executeResult = await executeJupiterUltraOrder( Buffer.from(signedTransaction.serialize()).toString('base64'), order.requestId ); // ... bot.sendMessage( msg.chat.id, `✅ Swap successful!\n` + `Transaction: https://solscan.io/tx/${executeResult.signature}\n` + `You swapped ${amount} SOL for approximately ${order.outAmount / 1e9} tokens` ); // ...});
Your app has now successfully swapped tokens! Checkout our full Github example for more details here