- Token Support: Send and receive both ETH and ERC20 tokens (USDC)
- User-to-User Transfers: Send crypto to other Telegram users using their usernames
- Balance Checking: Check your wallet’s current balance
This guide extends our basic Telegram bot implementation. If you haven’t gone
through that
guide, consider
starting there for a simpler introduction.
Overview
This enhanced Telegram bot provides a complete crypto remittance solution with:- EVM-compatible wallet creation and management
- ETH and USDC transfers to addresses or Telegram users
- Balance checking for multiple tokens
- Message signing capabilities
- Dynamic’s server wallets for secure transaction signing
- Supabase for user-wallet association and username lookups
- Grammy for Telegram bot interaction framework
- Viem for blockchain interactions and token transfers
Setup
First, follow the setup instructions in the basic Telegram Bot tutorial to set up:- Your Dynamic account and environment
- A Telegram bot using BotFather
- A Supabase project
- Node.js or Bun development environment
Start with the Starter Project
For this guide, we’ll use a starter project that already has the basic structure set up:Implementation
Now let’s enhance our basic bot by adding stablecoin support and user-to-user transfers. We’ll build on top of the starter repository by adding new features to the existing structure.1. Add Token Support
First, create atokens.ts
file in the utils folder to handle token operations. This file defines the supported tokens (ETH and USDC), their contract addresses, and helper functions for formatting amounts and checking balances:
utils/tokens.ts
2. Add Error Handling Utilities
Create anerrorHandling.ts
file in the utils folder to handle various error scenarios. This file provides a structured approach to error handling with typed errors, user-friendly error messages, and logging functionality:
utils/errorHandling.ts
3. Store Usernames in Supabase
Our implementation requires storing Telegram usernames in the Supabase database when users create their wallets. This enables direct lookups for user-to-user transfers without needing to call Telegram’s API. Make sure your Supabase wallets table includes ausername
column:
getWallet
utility stores the username when creating a wallet:
4. Enhance Send Command with Token Support
Enhance the existingsend.ts
file to support sending various tokens. This command allows users to send ETH or ERC20 tokens (like USDC) to any valid blockchain address, with proper validation and error handling:
commands/send.ts
5. Create User-to-User Transfer Feature
Add a new command for sending crypto directly to other Telegram users by creatingsendToUser.ts
in the commands folder. This command is what makes our remittance bot special - it allows users to send tokens to other Telegram users by their username instead of requiring wallet addresses:
commands/sendToUser.ts
send
command, but using the recipient’s address.
6. Add Balance Checking Feature
Create abalance.ts
file in the commands folder to allow users to check their token balances. This command queries both ETH and USDC balances and formats them nicely for the user:
commands/balance.ts
getTokenBalance
utility function to retrieve both ETH and USDC balances for the user’s wallet address that we created earlier.
7. Update Bot File to Connect All Components
Update the mainindex.ts
file to include all your new commands and functionality. This file ties everything together by registering all the commands and setting up the command menu that users will see in Telegram:
index.ts
Running the Bot
To run your stablecoin remittance Telegram bot:
Conclusion
You’ve successfully built a Telegram bot that enables crypto remittances using Dynamic’s server wallet. This solution makes cryptocurrency accessible to anyone with a Telegram account, without requiring technical blockchain knowledge. For the complete source code, visit our GitHub repository. For additional help or to join the community:- Join our Slack community
- Check out Dynamic’s documentation
- Follow us on X