? How to create a Telegram Crypto Bot in Javascript – Telegram Group

? How to create a Telegram Crypto Bot in Javascript

Initializing the Node.js project

mkdir cryptobot
cd cryptobot
npm init
npm install binance-api-node node-telegram-bot-api dotenv

Getting the Crypto prices from Binance

import Binance from 'binance-api-node'
import dotenv from 'dotenv'
import { formatMoney } from './utils/money.js'

dotenv.config()

// API keys can be generated here https://www.binance.com/en/my/settings/api-management
const binanceClient = Binance.default({
apiKey: process.env.BINANCE_API_KEY,
apiSecret: process.env.BINANCE_API_SECRET,
})

const cryptoToken1 = 'BTC'
const cryptoToken2 = 'USDT'

binanceClient
.avgPrice({ symbol: `${cryptoToken1}${cryptoToken2}` }) // example, { symbol: "BTCUSTD" }
.then((avgPrice) => {
console.log(formatMoney(avgPrice['price']))
})
.catch((error) =>
console.log(`Error retrieving the price for ${cryptoToken1}${cryptoToken2}: ${error}`)
)

Creating the Telegram Bot

const bot = new TelegramBot(process.env.TELEGRAMM_BOT_TOKEN, { polling: true })
import Binance from 'binance-api-node'
import TelegramBot from 'node-telegram-bot-api'
import dotenv from 'dotenv'
import { formatMoney } from './utils/money.js'

dotenv.config()

// API keys can be generated here https://www.binance.com/en/my/settings/api-management
const binanceClient = Binance.default({
apiKey: process.env.BINANCE_API_KEY,
apiSecret: process.env.BINANCE_API_SECRET,
})

// The bot token can be obtained from BotFather https://core.telegram.org/bots#3-how-do-i-create-a-bot
const bot = new TelegramBot(process.env.TELEGRAMM_BOT_TOKEN, { polling: true })

// Matches "/price [symbol]"
bot.onText(/\/price (.+)/, (msg, data) => {
const chatId = msg.chat.id

bot.sendMessage(chatId, 'Wait...')

// data[1] can be single token (i.e. "BTC") or pair ("ETH BTC")
const [cryptoToken1, cryptoToken2 = 'USDT'] = data[1].split(' ')

binanceClient
.avgPrice({ symbol: `${cryptoToken1}${cryptoToken2}`.toUpperCase() }) // example, { symbol: "BTCUSTD" }
.then((avgPrice) => {
bot.sendMessage(chatId, formatMoney(avgPrice['price']))
})
.catch((error) =>
bot.sendMessage(
chatId,
`Error retrieving the price for ${cryptoToken1}${cryptoToken2}: ${error}`
)
)
})

bot.on('message', (msg) => {
const chatId = msg.chat.id

switch (msg.text) {
case '/start':
bot.sendMessage(chatId, 'Hi there! I am Alice Crypto Bot.')
break

default:
break
}
})

Ten articles before and after

How to Make a Telegram Pokédex Bot Using Python – Telegram Group

How my Telegram bot “Vaccination finder” helped community and scaled for large scale users. Problems to Solutions — journey ! – Telegram Group

How to restart a python program from inside – Telegram Group

How to use the MOONCLONE Bot. To being using MOONCLONE BOT open up… – Telegram Group

First telegram bot in python.. Lets Create our first echo telegram bot… – Telegram Group

Telegram Bot integration with Jenkins – Telegram Group

Je vais booster vos abonnés/ventes grâce à une grosse promotion sur Telegram – Telegram Group

PyCryptoBot with Telegram. I received a feature request in the… – Telegram Group

RSA with telegram bot using Python – Telegram Group

Creating a Telegram bot with python – Telegram Group