Covalent Telegram Bot. This time, java based

peloclick
9 min readJun 27, 2021

Covalent has changed the way we can retrieve and display blockchain data without having the need to store it or having to interact with different rpcs or oracles that, in some cases, don’t even offer trustable information.

In this little tutorial we will be explaining how to use Covalent’s API potential by building a Telegram bot that will give us our wallet balance of ETH, USDT and UNI.

When finished, we will be able to interact with it like this:

The basic principles of a bot are quite simple: Telegram’s API lets us to create a bot even from their phone application. Once created, we will have a unique Token that we will use to interact with that bot. Thus, it is telegram that manages the messages that any user sends to our bot, forwarding the different requests made by the user to our backend service, that is interacting with Covalent API.

On the same way, the answer being given by our backend service, will be forward to the chat being used by the user. Now, let’s get into details!

Creating the telegram bot

On our phone, we will look for a bot named ‘BotFather’ that will, indeed, guide us in the creation of the bot. We have to give it a name and a username (this one has to be unique and must be ending with ‘bot’). When we’re done, we have to save the token id being given by BotFather since that is what will let us interact with it from our backend service:

Developing our backend service

As mentioned in the intro, we need to develop a service being able to accept requests (R/R) with a certain payload (POST) in an endpoint. Then process that payload and cook an answer in the proper format. In our case, we chose Spring Boot and java as frameworks for our development.

When a user writes a message in the chat with the bot we have created in the previous step, telegram’s API will forward that message to our backend service, including a body in JSON format with the following structure:

The structure of the answer body our service should be offering should be like this:

As we can see, we have to generate a JSON with an answer, giving the contents in the “text” field, as well as the “chat_id” where the telegram’s API will send our answer to.

To make our development, we have to use a spring boot framework compatible IDE. We will use Eclipse, but Visual Studio Code or Theia can be used as well. We can download the needed tools here: https://spring.io/tools

We create then our new Spring project:

The assistant will ask for the following:

  • Name of the application
  • Java version being used
  • Spring boot we want to use
  • Different libraries to be added to the project

This will generate a pom.xml file with all that information, that we can edit at any time. In our case we chose java 1.8 and the latest Spring boot version available, as well as the following libraries:

  • Spring-boot-starter-web
  • Spring-boot-devtools
  • Spring-boot-test

Once we are done, we can structure the project in this way:

For our goal, the three main files are:

  • pom.xml defining the libraries that are going to be used in our project
  • CovalentRestController.java where we will be designing the logic of our application
  • ConnectionServiceImpl.java with the methods to execute HTTP requests
  • IConnectionService.java implementing the class ConnectionServiceImpl
  • SpringBootCovalentTelegramApplication.java being the entrance gate to our application, automatically generated and not needing any changes from our side

So, finally, the contents of our pom.xml are like this:

As we can see, the needed libraries are:

  • spring-boot-starter-web
  • spring-boot-devtools
  • spring-boot-test
  • okhttp3
  • okio
  • jcifs

The libraries okhttp3, okio and jcifs are used by the ConnectionServiceImpl class to execute the different requests.

So, now let’s check the code of ConnectionServiceImpl.java:

And of IConnectionServiceImpl.java:

And then the contents of our SpringBootCovalentTelegramApplication.java:

Now let’s move to CovalentRestController.java. The method of this class does the following:

  • It gets the requests coming from the Telegram API, with the payload of the request in JSON format, parsed as a map
  • It processes the text sent by the user, identifying the 3 parameters it needs according to the user’s input: “balance symbol address”
  • It executes the request to the service get token balances for address of Covalent’s API, using those 3 parameters we have just mentioned: symbol, address and chainId. It is highly recommended to check out the official Covalent documentation: https://www.covalenthq.com/docs/api/#get-/v1/{chain_id}/address/{address}/balances_v2/ As the documentation explains, we need a Covalent API key to use its services, adding it as a parameter in our requests.
  • It processes the response of Covalent API, extracting and calculating the needed information to generate the text message that will be bounced back to the user on Telegram.
  • Sends the response to the user in Telegram that requested it through the chat_id, generating it in the proper format. To know more about Telegram API the official docs are here: https://core.telegram.org/
  • As mentioned in those Telegram docs, to give the response back on a bot, we need the token associated to that bot, adding it to the request we are going to generate to reply to the users’ input:

Now, let’s move to the real world and depploy our application

Deploying our service using Heroku:

We can deploy our application wherever we want. We will be using in our case Heroku, since they let their users deploy Spring Boot applications for free (with some restriction, not posing a problem for us in this case). We gotta create an account first on:

https://id.heroku.com/login

Once created, we can check our deployed apps in:

https://dashboard.heroku.com/apps

In the Heroku website it is explained how to deploy a Spring Boot application:

https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku

meaning:

  • We have to download and install Heroku Cli
  • We will open our command window and will log into our heroku account. For that we will execute heroku login
  • We will move in our system to the Spring Boot App folder.
  • Then we need to create a Git repository. If we don’t have GIT in our computer, we can download it here: https://git-scm.com/downloads
  • We execute git init
  • We execute then git add .
  • Finally we execute git commit –m “first commit”
  • For the first deployment we need to execute heroku create
  • From now on, for each “push” we do, it will mean that the service gets deployed on Heroku. For that we will execute git push heroku master
  • We can execute many deployments, doing new “commits” with our project, and after that, the corresponding “push”

If the deployment has been done correctly we will see something similar to this:

- Counting objects: 31, done.
- Delta compression using up to 4 threads.
- Compressing objects: 100% (19/19), done.
- Writing objects: 100% (31/31), 4.69 KiB | 0 bytes/s, done.
- Total 31 (delta 5), reused 0 (delta 0)
- remote: Compressing source files… done.
- remote: Building source:
- remote:
- remote: — — -> Building on the Heroku-20 stack
- remote: — — -> Using buildpack: heroku/java
- remote: — — -> Java app detected
- remote: — — -> Installing JDK 1.8… done
- remote: — — -> Executing Maven
- remote: $ ./mvnw -DskipTests clean dependency:list install
- remote: /tmp/build_4589cddc
- remote: [INFO] Scanning for projects…
- remote: [INFO]
- remote: [INFO] — — — < com.springboot.covalent.telegram.app:spring-boot-heroku > — — — -
- remote: [INFO] Building spring-boot-heroku 0.0.1-SNAPSHOT
- remote: [INFO] — — — — — — — — — — — — — — — — [ jar ] — — — — — — — — — — — — — — — — -
- remote: [INFO]
- remote: [INFO] — — maven-clean-plugin:3.0.0:clean (default-clean) @ spring-boot-heroku — -
- remote: [INFO]
- remote: [INFO] — — maven-dependency-plugin:3.0.2:list (default-cli) @ spring-boot-heroku — -
- remote: [INFO]
- remote: [INFO] — — maven-resources-plugin:3.0.1:resources (default-resources) @ spring-boot-heroku — -
- remote: [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
- remote: [INFO] Copying 1 resource
- remote: [INFO] Copying 0 resource
- remote: [INFO]
- remote: [INFO] — — maven-compiler-plugin:3.7.0:compile (default-compile) @ spring-boot-heroku — -
- remote: [INFO] Changes detected — recompiling the module!
- remote: [INFO] Compiling 4 source files to /tmp/build_4589cddc/target/classes
- remote: [INFO] /tmp/build_4589cddc/src/main/java/com/springboot/covalent/telegram/app/controllers/CovalentRestController.java: /tmp/build_4589cddc/src/main/java/com/springboot/covalent/telegram/app/controllers/CovalentRestController.java uses unchecked or unsafe operations.
- remote: [INFO] /tmp/build_4589cddc/src/main/java/com/springboot/covalent/telegram/app/controllers/CovalentRestController.java: Recompile with -Xlint:unchecked for details.
- remote: [INFO]
- remote: [INFO] — — maven-resources-plugin:3.0.1:testResources (default-testResources) @ spring-boot-heroku — -
- remote: [INFO] Using ‘UTF-8’ encoding to copy filtered resources.
- remote: [INFO] skip non existing resourceDirectory /tmp/build_4589cddc/src/test/resources
- remote: [INFO]
- remote: [INFO] — — maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ spring-boot-heroku — -
- remote: [INFO] Changes detected — recompiling the module!
- remote: [INFO] Compiling 1 source file to /tmp/build_4589cddc/target/test-classes
- remote: [INFO]
- remote: [INFO] — — maven-surefire-plugin:2.21.0:test (default-test) @ spring-boot-heroku — -
- remote: [INFO] Tests are skipped.
- remote: [INFO]
- remote: [INFO] — — maven-jar-plugin:3.0.2:jar (default-jar) @ spring-boot-heroku — -
- remote: [INFO] Building jar: /tmp/build_4589cddc/target/spring-boot-heroku-0.0.1-SNAPSHOT.jar
- remote: [INFO]
- remote: [INFO] — — spring-boot-maven-plugin:2.0.3.RELEASE:repackage (default) @ spring-boot-heroku — -
- remote: [INFO]
- remote: [INFO] — — maven-install-plugin:2.5.2:install (default-install) @ spring-boot-heroku — -
- remote: [INFO] Installing /tmp/build_4589cddc/target/spring-boot-heroku-0.0.1-SNAPSHOT.jar to /tmp/codon/tmp/cache/.m2/repository/com/springboot/covalent/telegram/app/spring-boot-heroku/0.0.1-SNAPSHOT/spring-boot-heroku-0.0.1-SNAPSHOT.jar
- remote: [INFO] Installing /tmp/build_4589cddc/pom.xml to /tmp/codon/tmp/cache/.m2/repository/com/springboot/covalent/telegram/app/spring-boot-heroku/0.0.1-SNAPSHOT/spring-boot-heroku-0.0.1-SNAPSHOT.pom
- remote: [INFO] — — — — — — — — — — — — — — — — — — — — — — — —
- remote: [INFO] BUILD SUCCESS
- remote: [INFO] — — — — — — — — — — — — — — — — — — — — — — — —
- remote: [INFO] Total time: 6.069 s
- remote: [INFO] Finished at: 2021–06–22T19:08:51Z
- remote: [INFO] — — — — — — — — — — — — — — — — — — — — — — — —
- remote: — — -> Discovering process types
- remote: Procfile declares types -> (none)
- remote: Default types for buildpack -> web
- remote:
- remote: — — -> Compressing…
- remote: Done: 67.3M
- remote: — — -> Launching…
- remote: Released v7
- remote: https://covalent-telegram-app.herokuapp.com/ deployed to Heroku
- remote:
- remote: Verifying deploy… done.
- To https://git.heroku.com/covalent-telegram-app.git
7decd05..e5515fa master -> master

So, on our Heroku dashboard we can check the URL of ours service:

https://dashboard.heroku.com/apps

Then we move to the service we’ve just deployed under settings, domains:

Linking our service with our Telegram bot:

Now that our application is deployed, we have to associate the bot we created in the beginning with the service we have just deployed. To do that, we just need to run this request on any browser:

https://api.telegram.org/bot<TOKEN>/setWebhook?url=

For example:

https://api.telegram.org/botXXXXXXXXX/setWebhook?url=https://xxxxxxxx.com/

Then, it will be our browser that will give us the results of the linking process.

So, as we can see, with very few lines of code, we can build and run indusrtry-level backend services that use Covalent’s API to our needs.

Take care and keep enjoying Covalent API! :)

--

--