nodejs program to fetch bitcoin price and send message in telegram when bitcoin price is dropped by 0.5% or increase by 0.5%

 same  as the last blogspot just some function is added



const express = require('express');
const app = express();
const ccxt = require('ccxt')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const { Console } = require('console');
const { BroadcastChannel } = require('worker_threads');
const io = new Server(server);
const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot('your telegram token', {polling: true});








app.use(express.static('public'));
app.use(express.urlencoded({extended:false}))
app.use(express.json());

app.get('/sa', (req, res) => {
  res.send("sd");

});

io.on('connection', async (fun) => {
    i = 1;
    s = 2000;
    console.log('started')
     console.log(fun.id)
     const exchange = new ccxt.binance()

     // Load the markets
     let  real = 1
     await exchange.loadMarkets()
     const ticker = await exchange.fetchTicker('BTC/USDT')
     let price_up = ticker.last
     console.log(price_up)
     let down = price_up -  (price_up * 0.05);
     let above = price_up +  (price_up * 0.05);
    console.log(down + " down")
    console.log(above + " above")
    //  let pri = price_up - per;
    //  console.log(pri)
    const chatId = your chat id
;
     fun.on('chat message', async (msg) => {
                const ticker = await exchange.fetchTicker('BTC/USDT')
                // console.log(pri)
                if (ticker.last >= above) {
               
                        // console.log(msg)
     
                    bot.sendMessage(chatId,'price increased by 5% | checked by number is '+ down +' | and current price is  '  + ticker.last);
       
                    console.log('price increased by 5% | checked by number is '+ down +' | and current price is  '  + ticker.last)
                }
                else if (ticker.last <= down) {
   
                        // console.log(msg)
                       
                        bot.sendMessage(chatId,'price decreased by 0.05% |checked by number '+above+' | current price is ' + ticker.last);
   
                    console.log('price decreased by 0.05% |checked by number '+above+' | current price is ' + ticker.last)
                }
               
                // Get the ticker for the BTC/USDT market
                    if(ticker.last < real) {
                        // console.log("BTC/USDT PRICE - " + ticker.last + "  " + i + "  price drop")
                    }
                    else if (ticker.last > real ){
                        // console.log("BTC/USDT PRICE - " + ticker.last + "  " + i + "  price increase")
                    }
                    real = ticker.last
               
                // Log the last price
                const data = {
                    name: ticker.last,
                    surname:s
                }
               
               
                // console.log("check for dat first" , i)
                // console.log("check ", s)
                i++;    
                s++;
                io.emit('chat message', data);
             
         });
         
         fun.on('endchat_1', function (){
            console.log('ended')
            fun.disconnect(fun.id);
        });
         
         fun.on('chat_2', (msg) => {
            console.log('chat_2')
            for (let i = 0; i < 10000; i++) {
                io.emit('chat_2', msg + "  " + i + " chat_2" );
                // console.log( io.emit('chat_2', msg + "  " + i + " chat_2" ))
            }
        });
     });
   
     const telegrammsg = () => {
        // bot.onText(/\/echo (.+)/, (msg, match) => {  
        //     const chatId = 524546863;
        //     const resp = match[1];
        //     bot.sendMessage(chatId, resp);
           
        //   });
         
         
     }

     telegrammsg();



// async function getBitcoinPrice() {
//     // Initialize the Binance exchange object
//     const exchange = new ccxt.binance()
 
//     // Load the markets
//     await exchange.loadMarkets()
 
//     // Get the ticker for the BTC/USDT market
//     const ticker = await exchange.fetchTicker('BTC/USDT')
 
//     // Log the last price
//     console.log(ticker.last)
//   }
 
// setInterval(getBitcoinPrice, 10000)
//   getBitcoinPrice()
 
server.listen(3000, () => {
  console.log('listening on *:3000');
});

Comments

All time Best Post

Write a program that declares a class named Person. It should have instance variables to record name, age and salary. Use new operator to create a Person object. Set and display its instance variables.

IMAGE CRUD OPERATION USING EXPRESS MONGOOSE MULTER

connect websocket to binance api and store t he true value in the db

CRUD APP WITH SORT IN NODE EXPRESS AND MONGODB

express session login system using node express mongodb

how to delete image from the folder directly from a Request (MULTER)

calculate rsi

java program for 1 to 10 print using while loop

How to fetch last one minute volume of BTC in CCXT Nodejs.