In this tutorial, we will be building a telegram crypto price bot that will let us check the Price difference of a token between the Binance and Kucoin exchange.
So let’s get started, shall we?
Generate Binance API Key
For generating the Binance API key we need to Signup for a Binance account and turn the 2FA ON, inside the Profile tab simply choose the API Management option.
After that, we choose the name for our API and click on Create API button.
As a result, it will generate our API KEY and API SECRET which we will save somewhere to be used later on.
Generate Kucoin API key
Next, we need to generate Kucoin’s API key. For that, we have to Signup for a Kucoin account and turn the 2FA ON.
After that, we choose the API Management option from our Profile tab.
Now, we choose the Create API option inside the API Management tab.
Then, we choose the API Name and API Passphrase.
Then, we save the API, API PASSPHRASE and SECRET KEY somewhere, we will need them later on.
Create our telegram price bot
Now that, we have successfully generated our Binance and Kucoin API credentials, it’s time to create our Telegram Bot.
For that, we need to search for BotFather in the telegram application.
Then, we send /newbot command to the BotFather account.
After that, we choose the name of the bot with the bot as a suffix which will provide us with our API key.
Integrate all the parts
Now that, we have successfully fulfilled all our requirements we are good to go for the integration of the Binance and Kucoin APIs with our BOT.
At first, we have to create a new Project and initialize it using.
npm init -y
After that, we need to install some dependencies.
npm install dotenv kucoin-node-api node-binance-api node-telegram-bot-api
Then, we have to create our “.env” file where we will put in all the required API credentials.
Content of .env for our telegram crypto price bot
KUCOIN_API=YOUR_KUCOIN's_API_KEY
KUCOIN_SECRET_KEY=YOUR_KUCOIN's_SECRET_KEY
KUCOIN_PASS_PHRASE=YOUR_KUCOIN's_PASSPHRASE
BINANCE_API=YOUR_BINANCE's_API_KEY
BINANCE_SECRET_KEY=YOUR_BINANCE's_SECRET_KEY
TELEGRAM_BOT_TOKEN=TELEGRAM's_BOT_API_KEY
Binance Configuration
Firstly, we will be configuring the Binance API and fetching the token/coin data from Binance using the binanceData function.
const Binance = require("node-binance-api");
// ---------------Binance API-------------
const binance = new Binance().options({
APIKEY: process.env.BINANCE_API,
APISECRET: process.env.BINANCE_SECRET_KEY,
});
// ----------Binance Data-----------------
async function binanceData(token) {
try {
const ticker = await binance.prices();
return Promise.resolve(ticker[`${token}USDT`]);
} catch (error) {
return Promise.reject(error);
}
}
Kucoin Configuration
Secondly, we will be configuring the Kucoin API and fetching the data from kucoin using the kucoinData function.
const api = require("kucoin-node-api");
// ---------------Kucoin API---------------
const config = {
apiKey: process.env.KUCOIN_API,
secretKey: process.env.KUCOIN_SECRET_KEY,
passphrase: process.env.KUCOIN_PASS_PHRASE,
environment: "live",
};
api.init(config);
// -----------Kucoin Data-----------------
function kucoinData(token) {
return api.getFiatPrice({
base: "USD",
currencies: [token],
});
}
Telegram price BOT Configuration
Thirdly, let’s configure our Telegram BOT.
const TelegramBot = require("node-telegram-bot-api");
const token = process.env.TELEGRAM_BOT_TOKEN;
const bot = new TelegramBot(token, { polling: true });
Now, let’s create a function that calculates the price difference and profitability percentage of the token/coin between the exchanges.
For instance, we have named it diffAndPercent.
function diffAndPercent(a, b) {
if (a > b) return [a - b, ((a - b) / a) * 100];
else return [b - a, ((b - a) / b) * 100];
}
Then, we will be creating a function named data which takes a Token symbol as an argument and returns Token’s Price, price difference and profitability percentage on between the exchanges.
So following will be the code logic for the data function.
// -----------------Data------------------
const data = async (token) => {
try {
let [binancePrice, kucoinPrice] = await Promise.all([
binanceData(token),
kucoinData(token),
]);
if (binancePrice && kucoinPrice?.data?.token) {
kucoinPrice = kucoinPrice.data[token]
const [difference, percentage] = diffAndPercent(binancePrice, kucoinPrice);
return Promise.resolve({
direction: +(binancePrice < kucoinPrice),
binancePrice,
kucoinPrice,
difference,
percentage
});
} else throw new Error("Error Occured...");
} catch (error) {
return Promise.reject(error);
}
};
At last, let’s integrate all these inside our app.js file.
//----------Importing Module.--------------
require("dotenv").config();
const Binance = require("node-binance-api");
const api = require("kucoin-node-api");
const TelegramBot = require("node-telegram-bot-api");
const token = process.env.TELEGRAM_BOT_TOKEN;
const bot = new TelegramBot(token, { polling: true });
function diffAndPercent(a, b) {
if (a > b) {
return [a - b, ((a - b) / a) * 100];
} else {
return [b - a, ((b - a) / b) * 100];
}
}
// ---------------Kucoin API---------------
const config = {
apiKey: process.env.KUCOIN_API,
secretKey: process.env.KUCOIN_SECRET_KEY,
passphrase: process.env.KUCOIN_PASS_PHRASE,
environment: "live",
};
api.init(config);
// ---------------Binance API-------------
const binance = new Binance().options({
APIKEY: process.env.BINANCE_API,
APISECRET: process.env.BINANCE_SECRET_KEY,
});
// ----------Binance Data-----------------
async function binanceData(token) {
try {
const ticker = await binance.prices();
return Promise.resolve(ticker[`${token}USDT`]);
} catch (error) {
return Promise.reject(error);
}
}
// -----------Kucoin Data-----------------
function kucoinData(token) {
return api.getFiatPrice({
base: "USD",
currencies: [token],
});
}
// -----------------Data------------------
const data = async token => {
try {
let [binancePrice, kucoinPrice] = await Promise.all([
binanceData(token),
kucoinData(token),
]);
if (binancePrice && kucoinPrice?.data?.token) {
kucoinPrice = kucoinPrice.data[token]
const [difference, percentage] = diffAndPercent(binancePrice, kucoinPrice);
return Promise.resolve({
direction: +(binancePrice < kucoinPrice),
binancePrice,
kucoinPrice,
difference,
percentage
});
} else throw new Error("Error Occured...");
} catch (error) {
return Promise.reject(error);
}
};
// -----------Telegram Bot Logic------------
bot.on("message", async (msg) => {
const { chat: { id }, text } = msg;
const msgText = text.toUpperCase().replace("/", "");
try {
const { binancePrice, kucoinPrice, difference, percentage } = await data(msgText);
bot.sendMessage(id, `Binance Price is : ${binancePrice}\nKucoin's Price is : ${kucoinPrice}\nDifference : ${difference.toFixed(4)}\nProfitability : ${percentage.toFixed(2)}%`);
} catch (error) {
bot.sendMessage(id, `${text} is not a valid token`);
}
});
Learn promise.all here
Test our price checker bot
Now that we are done with our coding section, let’s test our BOT.
For that, we need to start our application using.
node app.js
Conclusion
If you have followed us along with the tutorial then you have successfully implemented your telegram crypto price bot. If you have found it useful don’t forget to appreciate it in the comment section below. Also, if you have any queries, feel free to put that in the comments below.
Thank you.
I have tried to put /USDT, /DAI in telegram when running the bot, but it prompts me : /DAI is not a valid token ?
Strange but putting /DAI worked when I tested on my system. But /USDT isn’t valid. This is because our base currency is USDT for Binance and USD(USDT) for Kucoin. As there is no such pair as USDT/USDT or USD/USDT on either exchange hence maybe the error.