Build a Telegram Bot for human-readable alerts
In this guide, you will learn how to create a Telegram bot that sends human-readable alerts about transactions happening on-chain. You can customize this bot for any EVM-compatible blockchain, and you don’t need any specific knowledge about EVM transaction decoding and interpretation.
Guide
Step 0: Prerequisites
- An installed Bun (see installation guide here)
- An Alchemy account (sign up here)
- An Etherscan account (sign up here)
- A Telegram account
Step 1: Clone the Repository
Clone the Bot repository from GitHub and install project dependecies:
Step 2: Add Etherescan and Alchemy API Keys
Copy and rename the .env.example
file to .env
, then paste the Alchemy and Etherescan API keys into the ALCHEMY_API_KEY
and ETHERSCAN_API_KEY
variables.
We use the Alchemy API key to monitor new transactions happening on the blockchain, and the Etherscan API key (from the free plan) to fetch contract ABIs and avoid hitting rate limits. The Etherscan API could be optional if the transactions you are interested in do not interact with many contracts, but since we are testing AAVE V3 it will have many interactions.
Step 3: Create a New Bot on Telegram
- Obtain a bot token: Start a chat with the BotFather bot in Telegram, write
/newbot
into the chat, follow the instructions, and copy the bot token. Paste its value into theTELEGRAM_BOT_TOKEN
variable in the.env
file. - Obtain a chat ID: Get the chat ID of the chat where the bot should send notifications. Start a chat with your bot by pressing the
/start
command. Then open to the linkhttps://api.telegram.org/bot<YourBOTToken>/getUpdates
, whereYourBotToken
is the token you copied from the BotFather. From thechat
object, copy theid
and put it into theTELEGRAM_CHAT_ID
variable in the.env
file. Check this StackOverflow answer for more details.
Step 4: Setup the Decoder
Now, let’s go through the code. To begin using the Loop Decoder, we need to set up the following components:
RPC Provider
We’ll create a function that returns an object with aPublicClient
based on the chain ID. In this example, we are going to support the Ethereum Mainnet.
The constants.ts
file contains the RPC URL and configuration:
The getPublicClient
returns an RPC client based on the chaind ID and the config from constants.ts
:
ABI Loader
To avoid making unecessary calls to third-party APIs, Loop Decoder uses an API that allows cache. For this example, we will keep it simple and use an in-memory cache. We will also use some strategies to download contract ABIs from Etherscan and 4byte.directory. You can find more information about the strategies in the Strategies reference.
Create a cache for contract ABI:
Contract Metadata Loader
Create an in-memory cache for contract meta-information. Using ERC20RPCStrategyResolver
we will automatically retrieve token meta information from the contract such as token name, decimals, symbol, etc.
Loop Decoder instance
Finally, we’ll create a new instance of the LoopDecoder class.
Step 5: Decode and Interpret a Transaction
After creating the decoder
object in the decoder.ts
file, we can use its decodeTransaction
method to decode transactions by hash:
The decodeTransaction
method returns a DecodedTransaction
object, which you can inspect using our playground. Here is an example of an AAVE V3 transaction.
To interpret a decoded transaction, the interpreter.ts
file contains a transformEvent
function that transforms the DecodedTransaction
and adds an action description. You can test the transformEvent
function by putting it into the Interpretation field on the playground with the AAVE V3 example, and pressing “Interpret”.
Step 6: Create a Contract Subscription
The bot is set to monitor AAVE V3 transactions on the Ethereum mainnet by default. Update the contract address and chain ID in the constants.ts
file:
In the program’s entry point, we start a WebSocket subscription for new transactions using Alchemy’s WebSocket RPC.
Step 7: Handle a new Transaction
The handleTransaction
function is responsible for decoding incoming alerts and sending a Telegram message. You can customize the bot message or add extra information by tweaking the botMessage variable.
Step 8: Start the Bot
Use the following command to start the server locally:
Your Telegram bot is now set up and will monitor blockchain transactions and send alerts to the specified chat or channel.
Conculsion
In this guide, you’ve learned how to create a Telegram bot that monitors blockchain transactions and sends human-readable alerts. By using the Loop Decoder library, you can easily set up a bot to track specific contract addresses and chains without needing in-depth knowledge of EVM transaction decoding.
Let us know on X/Twitter (@3loop_io) if you encounter any problems or have any questions, we’d love to help you!
Happy coding!