How to make a Cryptocurrency Telegram bot with Node and Telegraf – DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

admin

# node # javascript # tutorial # beginners In this post we will learn how to create a cryptocurrency Telegram bot to obtain the values of the cryptocurrency we want to know using Coingecko API To make this post I was inspired by this other one How to make a cryptocurrency Telegram bot with Rust…

# node # javascript # tutorial # beginners In this post we will learn how to create a cryptocurrency Telegram bot to obtain the values of the cryptocurrency we want to know using Coingecko API

To make this post I was inspired by this other one How to make a cryptocurrency Telegram bot with Rust and Teloxide be sure to check it out, he creates very good content related to blockchain.

You can contact me by telegram if you need to hire a Full Stack developer or if you want to translate your posts from english to spanish.

You can also contact me by discord Appu#9136

You can clone the repo if you want.

Prerequisites Node.js installed you will need a Telegram account Creating Our Project open your terminal and type the following mkdir node-telegram-tut cd node-telegram-tut npm init –y code .Dependencies axios dotenv telegraf To install dependencies go to your project folder open a terminal and type the following

npm i axios dotenv telegraf

Enter fullscreen mode

Exit fullscreen mode

Now go to your package.json and add this

“scripts”: { “start”: “node ./src index.js” },

Enter fullscreen mode

Exit fullscreen mode

Project File Structure node-telegram-tut/

β”œβ”€β”€ node_modules/

β”œβ”€β”€ src/

β”‚ └── index.js

β”œβ”€β”€ .env

└── package.json

Table of Contents Setup the Telegram bot Token with BotFather Coding our Bot Creating our commands Deploying it to Heroku Conclusion 1.Setup the Telegram bot Token with BotFather To start coding our bot we first need to search for BotFather bot, this one.

After adding it we will see a list of commands, let’s click on /newbot you will be prompted to enter the name you wish to give to your bot.I named mine teletutbot , but you are free to call yours whatever you want, as long as the name is available.

After this you will receive a message with your token , now lets set a description for our bot with /setdescription , this way when you add the bot, you will see a message (like a welcome message) describing the bot’s function.

Finally you can add a picture if you want with setuserpic , i used this one

There are more commands to edit your bot, you can try them later

2.Coding our Bot Let’s start coding our bot, first let’s create a .env file in our project root folder, lets add a BOT_TOKEN var and assign it the token given to us by botfather when we created our bot.

BOT_TOKEN = paste-the-token-here

Enter fullscreen mode

Exit fullscreen mode

Now in our index.js , import telegraf, axios and dotenv

const { Telegraf } = require ( ” telegraf ” ); const axios = require ( ” axios ” ); require ( ” dotenv ” ).config ();

Enter fullscreen mode

Exit fullscreen mode

Then create a bot object from Telegraf Class and pass the BOT_TOKEN

const bot = new Telegraf ( process .env .BOT_TOKEN );

Enter fullscreen mode

Exit fullscreen mode

Finally lets create our first bot command that will be /start and then use the launch() method.

bot .

command ( ” start ” , ( ctx ) => { bot .telegram .sendMessage ( ctx .chat .

id , ” Welcome!! ” , { parse_mode : ” html ” } ); }); bot .launch ()

Enter fullscreen mode

Exit fullscreen mode

Our code so far should look like this

const { Telegraf } = require ( ” telegraf ” ); const axios = require ( ” axios ” ); require ( ” dotenv ” ).config (); const bot = new Telegraf ( process .env .

BOT_TOKEN ); bot .command ( ” start ” , ( ctx ) => { bot .telegram .sendMessage ( ctx .

chat .id , ” Welcome!! ” , { parse_mode : ” html ” } ); }); bot .launch ()

Enter fullscreen mode

Exit fullscreen mode

3.Creating our commands We don’t want a bot just to say Welcome, so we need to create more commands, for this example i’ll create a /help command to get all the available commands, a /currencies command to get all the supported currencies and a /crypto_price to get the price of the selected cryptocurrency in the desired currency

So let’s start creating our /help command.Let’s call our bot object and use the command method, as i say before we’re going to name this command help , we are going to use the sendMessage method from telegram api and we need to pass some parameters, there are two required parameters chat_id (extracted from the context) text and i will pass an optional parameter parse_mode to format the text a little bit.

bot .

command ( ” help ” , ( ctx ) => { bot .telegram .sendMessage ( ctx .chat .id , ” Command list: nn ” + ” /currencies to get all the suppported currencies.

nn ” + ‘ /crypto_price to get the value of the cryptocurrency in another currency, to use it first type the “currency” and then the “cryptocurrency” e.g.(/crypto_price usd bitcoin), can also add more currencies and cryptos separating them with commas but without spaces e.g.

(/crypto_price usd,eur,btc bitcoin,ethereum) nn ‘ + “” , { parse_mode : ” html ” } ); });

Enter fullscreen mode

Exit fullscreen mode

Now if you start your bot, and type /help you will get this as result.

Now let’s create our /currencies command Let’s go back to our code and create a new command, we will name it currencies , this will send a get request to the coingecko api and retrieve the supported currencies list.

I stored the res.data in a let that i named currencies , also wanted to send the currencies in bold, so i used a map method and returned each currency with *${currency}* , there are other ways to do it.

After that we are going to use again the sendMessage method, and this time wanted to show you that there are a Markdown parse mode.If you want to know more about it, please read the formatting options in the documentation.

bot .command ( ” currencies ” , ( ctx ) => { axios .get ( ” https://api.coingecko.com/api/v3/simple/supported_vs_currencies ” ) .

then (( res ) => { if ( res ) { let currencies = res .data ; //bold currencies text let boldedCurrencies = currencies .map (( currency ) => { return `* ${ currency } *` ; }); //send boldedCurrencies and break line bot .telegram .sendMessage ( ctx .chat .

id , ” Supported Currencies ” + ” n ” + boldedCurrencies .join ( ” n ” ), { parse_mode : ” Markdown ” } ); } }) .catch (( error ) => { console .log ( error ); }); });

Enter fullscreen mode

Exit fullscreen mode

If you try your /currencies command you should get something similar to this

Finally we will create the /crypto_price command As we did before we will name our new command crypto_price

For this one as a user we will send a message like this /crypto_price usd,eur bitcoin , so we will split the string by spaces with .split(” “) .This should split the string into three parts, the first part will be the /crypto_price , second part usd,eur and third part bitcoin , so we will create two variables currencies and cryptoCurrencies , then we will assign the values respectively.

We need to create a conditional in case the user enters the data incorrectly, or in case the user does not send any data in the command.if this is the case we need to send the user a message, in this case i want him to remember how to use the command so i added an example.

Now we are going to make the GET request to the API, we also going to check if the object from the response is empty, if its empty its because there was a spelling error, or some of the data was misplaced.

If this is the case we will answer again telling the user how to use the command

We are getting the data like this

data: { bitcoin: { usd: 21816, eur: 20872 }, ethereum: { usd: 1177.46, eur: 1126.54 } }

Enter fullscreen mode

Exit fullscreen mode

So i chose to use a for loop inside another for loop to manipulate the data, then used again the parse_mode to format the text

bot .command ( ” crypto_price ” , ( ctx ) => { let currencies = ctx .message .text .split ( ” ” )[ 1 ]; let cryptoCurrencies = ctx .message .text .split ( ” ” )[ 2 ]; if ( cryptoCurrencies === undefined || currencies === undefined ) { bot .

telegram .sendMessage ( ctx .chat .id , ” Please enter the currency and the crypto currency you want to convert to, remember to separate them with commas but without spaces e.g.(/crypto_price usd,eur,btc bitcoin,ethereum) .” , ); return ; } axios .get ( `https://api.coingecko.com/api/v3/simple/price?ids= ${ cryptoCurrencies } &vs_currencies= ${ currencies } ` ) .then (( res ) => { if ( res ) { //if res is empty if ( Object .

keys ( res .data ).length === 0 ) { bot .telegram .sendMessage ( ctx .chat .

id , ” Please enter the currency and the crypto currency you want to convert to, remember to separate them with commas but without spaces e.g.(/crypto_price usd,eur,btc bitcoin,ethereum) .” , ); return ; } const response = res .data ; for ( let cryptoCurrency in response ) { for ( let currency in response [ cryptoCurrency ]) { bot .telegram .

sendMessage ( ctx .

chat .id , ` ${ cryptoCurrency } price in ${ currency .toUpperCase ()} ➑️ ${ response [ cryptoCurrency ][ currency ]} ` , { parse_mode : ” html ” } ); } } return ; } }) .catch (( err ) => { console .

log ( err ); }); });

Enter fullscreen mode

Exit fullscreen mode

If you try /crypto_price command, you should get something like this

4.Deploying it to Heroku we need to create a server In case you want to deploy this app, we need to create a server, so let’s install express with this command npm i express and create a server in our index.js

remember to create a port constant and assign this process.env.PORT to it (heroku will give us a port value)

const express = require ( ‘ express ‘ ) //initiaalization const app = express () const port = process .env .PORT || 3000 ; app .

listen ( port , () => { console .log ( `Server is running on port ${ port } ` ); })

Enter fullscreen mode

Exit fullscreen mode

create an account This is an easy step, just go to Heroku and click on sign up

Fill the required fields and verify your account, then login and go to your apps and create a new one

Choose a name for your new app and continue to the next part

install Heroku CLI We are not going to ad a pipeline, so we can skip that part.Now for the deployment method i will use Heroku CLI

I’ll use npm install -g heroku to install it, then we need to open a terminal and type heroku cli , and you will see this message

Now let’s login by clicking the button in our browser

deploy Now lets follow the steps below, replace master by main or won’t let you git push

5.Conclusion We learned how to make a cryptocurrency telegram bot using telegraf and node.js.

I really hope you have been able to follow the post without any trouble, otherwise i apologize, please leave me your doubts or comments.

You can contact me by telegram if you need to hire a Full Stack developer.

You can also contact me by discord Appu#9136

You can clone the repo if you want.

Thanks for your time.

Top comments (2) Crown

Sort discussion: Selected Sort Option

Top Most upvoted and relevant comments will be first Latest Most recent comments will be first Oldest The oldest comments will be first Subscribe Personal Trusted User Create template Templates let you quickly answer FAQs or store snippets for re-use.

Submit Preview Dismiss Collapse

Expand

codenjobs codenjobs codenjobs Follow The worldwide network to help you find a job and candidate Location Remote Education Programming Work Remote Joined Apr 11, 2021 β€’ Aug 5 Dropdown menu

Copy link Hide Your Discord link is not working and sent you a message at LinkedIn.Can you respond in it?.

Leave a Reply

Next Post

Best Long-Term Crypto Investments (Top Picks 2022) - BitcoinEthereumNews.com

Cryptocurrencies are seen as short or long-term investments.However, this is because cryptocurrencies backed up by great projects surge and survive the test of time, thus, giving investors long-term profits.Even though investors look to trade cryptocurrencies through short-term strategies to take advantage of the market’s volatility, holding long-term is the best way to approach the crypto…
Best Long-Term Crypto Investments (Top Picks 2022) – BitcoinEthereumNews.com

Subscribe US Now