Using session signers to create Telegram trading bots for users
Session signers allow your app to create Telegram bots or other agents that can execute transactions on behalf of users. You can use session signers to execute transactions given natural language commands from users in Telegram, or execute transactions on the user’s behalf while they are offline.You can also configure session signers such that your Telegram bot or agent has specific permissions via policies, such that the agent can only execute certain transaction types.At a high-level, you can use session signers to create Telegram bots and other trading agents like so.
Looking to build a Telegram trading bot without a web or mobile app to start? View our overall
Telegram trading bot guide to learn how to set up your bot for different
configurations.
When your users login to your app with Telegram or link a Telegram account, create a wallet for them. Store a mapping between the ID of the created wallet and the user’s Telegram ID so that you can determine the user’s wallet within the bot’s code.
3
Add a session signer to the user's wallet
After the wallet has been created, add a session signer to the user’s wallet, which the bot can use to transact on the user’s behalf.Make sure to store the private key(s) associated with your signer ID securely in your server. Your Telegram bot or agent will need this to execute transaction requests.Follow the linked quickstart below to learn how to add a session signer to the user’s wallet.
Finally, the bot can use the session signer to execute transactions on the user’s behalf when prompted. For instance, you might implement a /transact command that takes input on the user to transact on their behalf.
Make sure to configure your Privy client with the private key for the session signer (authorization key) you created in the Dashboard.
Copy
Ask AI
bot.onText(/\/transact/, async (msg) => { // Custom logic to infer the transaction to send from the user's message const transaction = getTransactionDetailsFromMsg(msg); // Determine user's wallet ID from their Telegram user ID const user = await privy.getUserByTelegramUserId(msg.from.id); const wallet = user?.linkedAccounts.find((account): account is WalletWithMetadata => (account.type === 'wallet' && account.walletClientType === 'privy')); const walletId = wallet?.id; if (!walletId) throw new Error('Cannot determine wallet ID for user'); // Send transaction await privy.walletApi.solana.sendTransaction({walletId, ...transaction});});