There are roughly 400 transactions happening per second on the Solana blockchain. These include token transfers, NFT sales, token swaps, bids, DAO votes, bets and lots more. But with so much data, how do you keep track of changes that you care about? That's where webhooks come in. Webhooks allow you to track these changes and stay up to-date on the things you care about. This guide will show you how to set up a Telegram bot to get on-chain updates sent directly to a user or channel using a Cloudflare worker.
Steps to set up a Telegram Bot
- Set up the Telegram channel and the Telegram bot.
- Create a Cloudflare worker to act as a connection between the webhook that is sending updates and the Telegram bot.
- Create a Helius webhook.
- Edit the addresses the webhook tracks via the Helius Webhook API. This step is only needed for tracking entire NFT collections.
Set Up a Telegram Bot
Telegram bots are a great way to automate certain tasks, especially updates. Here are the steps to set one up.
- Start a Conversation with BotFather: Search for 'BotFather' in the Telegram search bar and click on it to begin a conversation.
- Create a New Bot: Type and send '/newbot'. BotFather will now guide you through naming your bot and giving it a username.
- Access Token: Once done, BotFather will provide an access token. Note it down securely as we will be using this later on in the tutorial.
For channel updates:
- Create a Telegram Channel: Go to the menu (☰) and select 'New Channel'.
- Invite the Bot: Once you've set up the channel, click on the channel's name, select 'Administrators' and add your bot as one.
- Find the Chat ID:
- To determine your channel's chat ID, follow these steps:
- Send a Message: First, send a message to the channel you've created.
- Use the Telegram API: Navigate to https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates in your browser, replacing <YOUR_BOT_TOKEN> with the token you received from BotFather.
- Locate the Chat ID: In the returned data, you'll find a field named chat and inside it, an id which is the chat ID of your channel.
- Remember to note down this chat ID securely, as you'll need it for further configurations.
Create a Cloudflare worker
Cloudflare workers allow you to deploy code that is hosted online and will run permanently so as to constantly be on alert for updates from your web hook.
- Log into Cloudflare: Start by logging into your Cloudflare account. If you don’t have one yet, you can sign up for free.
- Access the Workers section: From the Cloudflare dashboard, navigate to the "Workers" tab.
- Create a new Worker: Click on the “Create a Worker” button. This will take you to the Worker Editor.
- Edit the Worker Code:
- In the editor, you'll see a script template. Replace the default code with the provided Cloudflare code for your Telegram bot.
- Locate the variables for the BOT_TOKEN and CHAT_ID within the code. These are placeholders and will be used to send messages via your bot to the correct chat.
- Configure Environment Variables:
- In the Cloudflare Workers dashboard, navigate to the "Settings" tab of your worker.
- Here, you can add your bot’s token, the chat token, and your Helius API key as environment variables. This helps keep sensitive data out of your main code and secure.
- Ensure that you name these environment variables correctly to match what's expected in your code (e.g., BOT_TOKEN, CHAT_ID, etc.).
- Deploy the Worker: After making sure your code and environment variables are correctly set, click on the “Save and Deploy” button. Your worker will be deployed and assigned a unique public URL.
- Note Down the Public URL: After deployment, make sure to note down the public URL assigned to your worker. This URL is what you'll be using as the webhook endpoint for your Telegram bot.
Cloudflare Worker code:
Set up a Helius Webhook
Helius webhooks bridge the gap between changes on the Solana blockchain and your Telegram channel. In this example I will be setting up a webhook to track all transactions that a certain wallet makes.
- Sign Up: Visit dev.helius.xyz and create a free account.
- Create a Webhook: Within your dashboard, opt to create a new webhook. You'll have a choice to decide its type - Raw, Enhanced, Discord, or Account. For specific categorised updates, 'Enhanced' is recommended. When choosing this, specify the updates you're interested in, e.g., NFT_SALES or NFT_LISTING. A comprehensive list is available here.
- Supply Worker Link and Solana Address: Ensure you provide the link to your deployed Cloudflare worker. Also, mention the specific Solana account address (could be a user's wallet or a list of mints from a collection) you wish to monitor.
How to track NFT collections (Sales Bot Setup)
If you want to track a certain collection to get updates when something happens to an NFT in that collection you will need to add all the NFT mint addresses as accounts on you webhook. To do this you will need to make use of the Helius webhook endpoint to edit the webhook that you initially made.Once this is set up you will be able to track sales, listings, bids, burns and any other transaction that an NFT is involved in.
The first thing you will need is a hash/mint list for the collection you want to track. You can use the following code to get a mint list using DAS. All you will need to do is add in your Helius API key and replace the creatorAddress with the address of the first verified creator of an NFT. You can find the first verified creator by going on your favourite explorer, looking for the metadata and finding the address under the creators section:
Once you have this you can run the script by opening your terminal, navigating to the directory with the script in it and running node snapshot.js. This will create a JSON file called addresses.json in that directory. Alternatively you can use a tool like the Magic Eden snapshot tool to get a hash list.
Once these changes have been made you can run this script from your terminal with node webhookUpdate.js. After running this code you should receive confirmation in the terminal.
Once the webhook has been updated you can go back the the Cloudflare worker and edit the code to return details specific to NFTs. The code that is needed is already included, all you would need to do is comment out the section for sending TRANSFER updates and uncomment the section for sending NFT updates.
When an event that you are tracking happens on chain the webhook will send an update to the Cloudflare worker. This update will be in json format and will typically contain a lot of information such as all the accounts involved, changes in each account and info relating to the type of event. In the code provided we extract the parts that we believe to be important.
If you would like to customise the message that is sent or change the data that is extracted from the webhook update you can make changes to the messageToSend variable. If you would like to track something outside of transfers and NFT events you can still do so. You can modify the Cloudflare code to send the entire requestBody. This will forward the full webhook JSON response to your Telegram bot, allowing you to view the response structure. You can then view and decide which sections of this you would like to include in your message.
With this setup, your Telegram channel becomes a hub for real-time updates from the Solana blockchain, all thanks to Helius webhooks. We hope you have found this guide useful, if you have any questions or issues please feel free to message the team on Discord or Twitter.