Dev Hands-On: Creating our cryptocurrency bot showing the coin price in Telegram!

By Eroshi | devnewbies | 30 May 2020


Hi guys! It's me, andrecules, again!

Today we're gonna apply a telegram bot showing crypto prices using commands! In the end of this article i will leave the github of the code!

OBS: THIS BOT IS DEPRECATED THO, I REDIRECTED TO A NEW VERSION OF THIS BOT(even more improved and updated) ON THE BOTTOM OF THIS ARTICLE.

First things first

As i said in this article, you'll need a bit of prominence in node.js, APIs and javascript, if you don't know it, its for beginners so you can create following my steps too!

 

Cryptocurrency's price

So we will need to get this information about bitcoin for example, i need to feed my bot with this information!

So this ticker for example came from CoinGecko API, and we will use them to get our crypto's price information!

 

Botfather

The first part is going to botfather to create your new bot, as the same path in this article!

After getting your new token Bot from botfather, we will create our bot in the vscode.

Programming our Bot

Install telegraf as I did this article and you will do the base for every Bot created, the /start command for example:

const Telegraf = require("telegraf");

//instantiate Telegraf with our token got in the BtFather
//normally this is not appropriate, you will need to store the token in the server
//but is just an example for everyone
const bot = new Telegraf("the token for our BOT");

//our command /start
bot.command("start", (msg) => msg.reply(`Hello ${msg.from.username}`));

//start polling
bot.startPolling();

This is the "hello world" example that I did in the last article, simply talking, is the most basic telegram that we can create.

project folder If everything was fine in the last article, this is your folder project!

And your package.json will be like this:

packagejson

Our scripts part will be the node command to start our bot! Your bot just will work if you run in the terminal:

yarn

You'll need to go to terminal where your bot is to "yarn start" or "npm start" it'll deppends what package manager in nodejs you will use.

tip: if you are in visual code as I do, you can use ctrl + shift + ' to open the terminal inside the vscode.

So, this will run your start bot, and now we will go on!

 

Commands in our Bot

The most useful way to interact with our bot is using Commands:

When we created the hello world, we dealt with /start command from the Bot and interacted with the user!

Every commands came with this "/" beside the real command, and we can listen for it in our telegram bot

...

//our command /start
bot.command("start", (msg) => msg.reply(`Hello ${msg.from.username}`));
//this will create more reactive commands "/bitcoin, /ethereum etc"

bot.command("bitcoin", (msg) => msg.reply(`Price for bitcoin: `));
bot.command("litecoin", (msg) => msg.reply(`Price for litecoin: `));
bot.command("ethereum", (msg) => msg.reply(`Price for ethereum: `));
bot.command("tezos", (msg) => msg.reply(`Price for Tezos: `));


//start polling
bot.startPolling();

That's it now we created more 4 commands in our bot that we will be interact with the CoinGecko API! 

Now you will need to re-run(ctrl+c in the terminal running our node index.js) in the terminal because our changes in the index.js code will have not up to telegram and yarn start again!

Now it'll work: /start, /bitcoin, /litecoin, /ethereum and /basic-attention-token in our BOT!

Amazing!

Dealing with the crypto prices in the Bot

So we will use coingecko API, but what the heck is API?

API will give you information to use in your own system, the most simple case example is the coingecko, they can give US the JSON with our information: The price of the cryptocurrency that we need.

JSON is the best way to send and get serialized data from another system if the API is free, you can read more about JSON here.

To get data from CoinGecko we will need to use AXIOS api, to fetch data from outside!

npm install axios 
or
yarn add axios

yarn add axios

We will do the simple: each command have your own cryptocurrency, so we will get each of them in their reply to the user in this way:

bot.command("bitcoin", async (ctx) => {
  //we will use async await from js language to get our price information!!!
  try {
    //axios will use the url to get usd price for bitcoin
    const { data } = await axios.get(
      "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
    );
    //you will see the json if you console in the terminal!
    console.log(data);
    //answer our user!
    ctx.reply(`Price for bitcoin: U$${data.bitcoin.usd}`);
  } catch (e) {
    console.log(e);
  }
});

 

Now you can do the same with each command litecoin, ethereum, tezos etc, whatever commands your bot will have, just changing the link "bitcoin" for their id, obs: basic attention token needs to be united as basic-attention-token, and I will be put all code done in my github in the final of the article to you see the final code ;)

 

We are using API from CoinGecko getting the crypto coin price information from axios https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd this url it came from their documentation here. You can do a lot of things with their information and for free, just put a kind of "powered coingecko" to support them :)

There is another great ways to do this guy, but for beginners this is everything we need to know for now! Is a lot of information! But its pretty amazing to see your bot in action!

obs: Don't forget to stop and run your bot, npm start/yarn start or your bot will stop to answer you... In the future we will do a server to "serve" our bot from outside of our localhost pc!

 

Botfather and the commands

So, now we have our commands, but how we can show it to the user? Simple! We can go back to botfather and put their commands to show up when our user try to interact with out Bot

 

my bots

 

aa136af6898b25c71884cee2c3d7627b450b1633b4fe47c6ce6f4486cde296c3.png

This is the list of my own bots, you will have the bot that you create, so you can choose the bot you are developing now! It'll depends the token you chose! By the way, you can see the token again here, and do a lot of things!

 

ddf7493b7c8340c8b02114e640de35e88cdf6c77b8f86811052586226a28360e.png

Go to edit bot!

e7dc155f7ff2014640be8f83adacbcd748b3b821d633507f685c344c9daa302c.png

You can edit everything before in the botfather, your bot name, description, botpic(its image), and commands to user interact!

383f2449ab7eb450b039b9612bcc362186419c668df89eae9dd423860dd4ec36.png

And that's it you send to him and he will show their commands to the user! Awesome!

 

39b8ffaf985b6837d36d24aa4a2ebbc2e48c97ccc40f1ea18015fe41f4e79074.png

Now you can show your commands to the user!

 

this is the old repo on github as example of this article. this code has buggy on github so use the new one if you wanna host your own bot.

use the newest upgraded code here!

 

 

How do you rate this article?

5


Eroshi
Eroshi

Front end developer and Crypto evangelist, but not finacial advisor! Follow me in twitter: @andrecrjr


devnewbies
devnewbies

Web development, Front-end, Telegram Bot, Javascript in a newbie way, follow me to good development things!

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.