How to Create an Ethereum Price & Metadata Telegram Bot with ZettaBlock
A step-by-step tutorial on how to build a bot that obtains the Metadata and Price of crypto tokens on the Ethereum blockchain
In this article, I will be showing you how I built a Telegram Bot that obtains Token Metadata and price from the Ethereum Blockchain through ZettaBlock’s GraphQL API.
With this Telegram bot, you can check the metadata and price of any token on the ETH blockchain in seconds!
You’ll be learning:
How to Create a SQL Query with ZettaBlock
How to Build Your Telegram Bot
How to Write and Deploy the Backend Code for Your Bot
First, let’s start by understanding our graphQL API platform, ZettaBlock.
What is ZettaBlock?
ZettaBlock is an enterprise-grade full-stack Web3 infrastructure for indexing and analytics, joining on-chain and off-chain data.
ZettaBlock provides developers with simple, scalable API endpoints to access and query on-chain data as well as off-chain data and enables seamless integration of this data into your applications - both via customizable APIs and embedded charts.
It enables you to build real-time, public-facing, reliable GraphQL APIs via SQL in minutes - all without worrying about data processing in your front-end or back-end.
In short, applications built on top of ZettaBlock only need to interact with a handful of common and user-friendly APIs that provide efficient access to numerous previously siloed data sources.
Currently, ZettaBlock supports Ethereum, Polygon, Arbitrum, Aptos, Solana, Ripple XRP, and IoTex. With plans for expanding to more chains
Check out ZettaBlock’s documentation to learn more
STEP 1: Create Your ZettaBlock Account
Now that we understand what ZettaBlock’s doing for us, let’s create our Account with ZettaBlock to create our SQL Query.
Visit ZettaBlock's build workspace then click "Continue with Google" to sign in with your Google Account
You will be prompted to a new "Workspace".
STEP 2: Create a SQL query
How to Create an SQL Query with ZettaBlock
To build a GraphQL API, we need to create a SQL query to access the data we want to integrate into our Telegram Bot. Hence, we will need to visit ZettaBlock's Query Builder and create a SQL query right there.
Here are the Steps:
To create a new SQL query, click "Create" from your workspace dashboard. Then click "Query Builder".
You'll be prompted to the main interface where we'll create our SQL query.
For this tutorial, we will use a model SQL code that will enable us to get the metadata of ERC20 tokens on Ethereum.
Next, we name our SQL (1) then we select the prices
database, which contains abstraction price info processed by ZettaBlock already, then choose the usd
table which contains the token metadata and price (2).
Then, we copy and Paste the code below into the query window;
SELECT
p.name,
p.symbol,
p.slug,
p.ethereum_token_address,
p.price,
p.minute,
t.decimals,
p.data_creation_date
FROM
prices.usd p
INNER join ethereum_mainnet.ethereum_erc20_tokens t
ON LOWER(p.ethereum_token_address) = LOWER(t.contract_address)
WHERE
data_creation_date >= date('2022-01-01')
Your workspace should look like this now:
Now, Run the Query. Do note that this process might take a couple of minutes. So you'd need to be patient.
STEP 3: Create a GraphQL API
Once the query results are completed, click the Create API button on the top right of the window and move to the next step.
Next, we can customize the API, by setting the Data Refresh Interval which decides how often the underlying data will be refreshed for the API. We’d like our API to be refreshed every 24 hours, so we choose the24hrs
in the option list.
Then we can choose the index fields, which can make the fields faster to query. We’d like to turn on the name
, symbol
, slug
, ethereum_token_address
, minute
, data_creation_date
Once we have chosen the index fields, our next phase is to Enable Incremental Data Refresh
To optimize the API cost and performance, we’d like to enable the incremental data refresh option by turning on the toggle.
The first step is to set the primary key, a unique and not null key in the API table. When ZettaBlock processes the incremental data refresh, it will use this key to overwrite stale partitioned data. The primary key here is a set: name, ethereum_token_address, minute
.
The second step is to provide the Incremental SQL Code, which will be used to partially refresh the data, and could greatly reduce the cost by controlling how much data to scan and how to scan.
We can copy and paste the API transformation code into the Incremental SQL Code console.
Then, we need to modify the time condition to: data_creation_date >= DATE_ADD('day', -4, current_date)
, which will make the future refresh update only scan through the last 4 days of data.
Everything's now set, we can now click the Create API button to launch our GraphQL API.
P.S: This process will take a few minutes.
Explore the API
Once the indexing process is complete, click the Info bar to check the API info.
From the pop-up window, you can copy your API Key and GraphQL API ID. We will need these two variables while integrating our Bot with ZettaBlock.
You can copy your API KEY from "API Management" and then copy your API ID from the URL (as shown below). The ID begins with the string sq_xxxxx
STEP 4: Build Your Telegram Bot
Now that we have our Query ready, we're ready to build our Telegram Bot.
For this tutorial, we'll be building our Bot with BotFather
Go to BotFather on Telegram, then click Start.
Enter the command /newbot
Give your bot a name and username. Note that the username must be unique and can't easily be changed. But you can change your bot name anytime.
Once you're done, you will be given an API token. Keep it safe, we'd be needing it for our next step.
STEP 5: Connect Your Bot to your SQL Query
Let's now write some backend instructions for our bot and connect it to the SQL Query.
Requirements
To properly integrate this, please ensure:
You have correctly implemented steps (1-4) above
You have
node.js
installed on your ComputerYou have a basic knowledge of Javascript
Write Your Code
In your terminal, run the command below to clone the repository and install the necessary dependencies.
$ yarn init
$ git clone https://github.com/Zettablock/ethereum-token-metadata-bot.git
$ cd ethereum-token-metadata-bot
$ yarn install
P.S: Feel free to tweak the bot to your preference
Add your Bot Configurations
Create a .env
file in the root directory of the project. Then, copy the contents of .env.example
into the .env
file, and fill in the values for the variables.
The .env
file example looks like this:
API_ID = 'sq_bce0000af5014d96ac0995578cd7a403' # Change this ID to your own API ID (Copy this from your ZettaBlock workspace)
BOT_TOKEN = 'YOUR_BOT_TOKEN' #The one you got from BotFather
X_API_KEY = 'YOUR_X_API_KEY'
STEP 6: Deploy Your Bot🚀
Deploy Locally
Now activate the bot by running the command below in your terminal:
node bot.js
STEP 7: Launch the Bot on Telegram
Visit your Bot on Telegram, and type /token ${symbol}
to query the MATIC token price and metadata.
Enter the command /token MATIC
.
Your bot should work like this:
Congratulations 🎉
You have now successfully built a Telegram bot that queries the Ethereum blockchain through ZettaBlock API!!!🥳
Kindly drop me your feedback on this.
Further Reading
To learn more:
Check out ZettaBlock's Documentation
Check out ZettaBlock's Telegram Bot Tutorial
Your Favorite Dev,
Mide Sofek