Build Telegram Bot using Bitquery APIs (using BotFather)

Telegram is one of the popular chat applications people all across the world use both for commercial and personal requirements.

Today, we will show how to create a simple price update Telegram bot using Bitquery GraphQL API. The bot will provide price updates for CAKE/USDT on Binance Smart Chain Mainnet (BSC) protocol.

Note: You can get the Github repository of the code mention below at the end of the article.

Sign up to

If you are new to Bitquery’s interface, go to GraphQL IDE.

From here, sign up with your credentials. You might get a confirmation email on your registered email address.

After you are signed up, you would be redirected to GraphQL IDE from where you can get your unique API key. This API key is needed to call Bitquery GraphQL APIs.

Developing the Telegram Bot using Python

Python is one of the programming languages which makes it easy and highly convenient for developers to make Telegram bots. However, you can choose any language in which you are comfortable.

We will make a Telegram bot that informs the user about the updated price of CAKE/USDT on the Binance Smart Chain Mainnet (BSC).

Creating a sample bot using BotFather in Telegram

First, we need to start a chat with BotFather. Initiate the conversation using the /start command.

To initialize a new bot, we need to type /newbot in the BotFather.

Next, we need to set up a name and a username for our bot.

Making the Telegram bot using Python

In the root folder of your project, open the terminal and run the following command:

pip install pyTelegramBotAPI

First of all, we need to import two major libraries into our code, namely requests and telebot. While requests plays a key role in making a POST request to Bitquery’s GraphQL API, telebot is used to create a telegram bot’s instance into our code.

Then we will write a function run_query which takes the GraphQL query that we make in our code as shown below

In the above code, notice ethereum (network: bsc) which shows that we are dealing with Binance smart chain blockchain.

The above code snippet gets the latest price for CAKE/USDT pair.

Now, before running the code you need to place your API key which you obtained from GraphQL IDE profile section in the headers variable in the above function.

quotePrice is the variable where the price returned from the query is stored.

Our next step is to create an instance of the telebot library that we installed earlier.

After that, we are supposed to make the necessary functions that would return either True or False based upon the logic, we need to work upon the message handlers for our Telegram Bot.

Then, we’ll make the bot message handlers

And for the bot to constantly check for messages sent by the users, we’ll use the polling() method as shown below

Running the Telegram Bot


Link to the screen recording

Link to the Github repository

Link to the Gists

1 Like