How to create a telegram bot in Python using the Covalent API (Part 2)

Tatyana Korableva
Covalent
Published in
4 min readJun 6, 2021

Hello everyone, here I will continue to teach you how to create a telegram bot in Python. In the previous article , I described how to create a bot and how to add a useful function to check the balance of your ETH wallet using the Covalent API. In this article, we will consider how to add a feature to track changes in the wallet balance and a feature to display NFT pictures.

I will describe the entire code again, since it will be more difficult to explain where additional lines need to be added to the already existing code from the first article.

The first thing we need to do is import the required libraries. Let’s register Telebot and Pandas Libraries and threading and os.path modules for processing data.

The Pandas Python library is the perfect tool for anyone doing data analysis using the Python programming language.
Telebot Libraries is a simple but extensible Python implementation for the Telegram Bot API.
The threading module greatly simplifies the work with threads and allows you to program the launch of several operations at the same time.
Os.path is a nested module in the os module and implements some useful functions for working with paths.

import requests
import telebot
import pandas as pd
import threading
import os.path

Now you need to declare a url variable and assign the Covalent API address to it. Instead of the YOUR_COVALENT_API_KEY variable, you need to substitute the key that you received in the first step in the previous article.

url = "https://api.covalenthq.com"
chain_id = "1"
my_key = "YOUR_COVALENT_API_KEY"

Next, we need to create the bot variable. Instead of the YOUR_TELEGRAM_BOT_TOKEN variable, you need to substitute your api token from step 2 in the previous article.

# CovalentBot
telegramm_token = "YOUR TELEGRAM_BOT_TOKEN"
bot = telebot.TeleBot(telegramm_token)

Now we will create a keyboard that the bot will show you at startup. Create a variable keyboard1, into which we write telebot.types.ReplyKeyboardMarkup(). This function calls the keyboard. So that the created buttons do not look large, you just need to register True in ReplyKeyboardMarkup(). Well, if you want the keyboard to hide as soon as the user presses on it, then write the second True.

Next, we will create rows, but remember that there can be no more than 12 rows! In order to create them, we write keyboard1.row (). Put «💰Balance» , «Show NFT image», «Show adr», «Add adr», «Del adr».

keyboard1 = telebot.types.ReplyKeyboardMarkup(True, True)
keyboard1.row('💰Balance', 'Show NFT image')
keyboard1.row('Add adr', 'Show adr', 'Del adr')

Let’s create a dictionary for users’ wallets.

users_addr = {}

We create a function to check changes in the wallet balance. If the number of “coins” has changed, then a message is displayed about the balance, what was and what has become.

Create a separate thread to check the balance once a minute.

Now, to call the keyboard, add reply_markup = keyboard1 to the function for sending a message at startup. Here’s what you should get:

Next, let’s set the following logic:

  • when you click on the “Add adr” button, our bot will request the address of your ETH wallet;
  • when you click on the “💰Balance” button, our bot using the Covalent API will request the balance of your wallet and display it on the screen;
  • when you click on the “Show adr” button, the address of the ETH wallet will be shown;
  • when you press the “Del adr” button, the address will be deleted.
  • when you press the “Show NFT image” button, NFT images with their brief description will be displayed;
  • the wallet balance is monitored immediately, as soon as the address is entered, and if the balance has changed, two messages will be displayed: the first is a new balance, the second is an old balance.

For this example, I limited myself to displaying the first 5 pictures. If desired, you can increase their number.

get_nft_token_ids = f”/v1/{chain_id}/tokens/{address}/nft_token_ids/?page-size=5&key={my_key}”

We initialize the thread in which we will check the balance of the wallet.

event = threading.Event()
thread = threading.Thread(target=worker, args=(event,))
thread.start()

Now we will write bot.polling (). This is necessary so that the bot does not turn off immediately, but works and checks if there is a new message on the server. You should put this at the end of the code.

bot.polling()

As a result, we have the following bot.

When we created this telegram bot, we used several Covalent API functions:

  1. get_token_balances_for_address
  2. get_nft_token_ids
  3. get_external_nft_metadata

As a result, after completing all the steps listed above, you can get a working bot with useful functions.

It is important to provide for the bot to be launched on some server for smooth operation.

To learn more about Covalent, visit covalenthq.com

Join the Discussion in our Communities:

Twitter : @Covalent_HQ
Telegram : https://t.me/CovalentHQ
Discord : https://discord.gg/M4aRubV
Web : https://www.covalenthq.com/

--

--