GCP/Apps Script

Apps Script로 Upbit api를 이용해 코인 시세를 매일 아침에 Telegram으로 조간 브리핑 받기

whistory 2023. 1. 3. 08:59
반응형

 

나는 비트코인이 물려있다.

빠른 시간에 탈출을 해야 한다.

그렇기 때문에 매일 아침 상황이 어떤지 확인하고 싶다.

 

upbit에서 제공하는 api를 이용한다.

 

 

 

 

 

function upbitMorningBriefing () {
  const coinArr = ["BTC","ETH", "XRP"];
  var message = "";
  for (i in coinArr) {
    const url = `https://api.upbit.com/v1/ticker?markets=KRW-${coinArr[i]}`;
    const response = UrlFetchApp.fetch(url, {
      method : "GET",
      header:{
        "contentType" : "application/json"
      }
    });

    const json = response.getContentText();
    const returndData = JSON.parse(json);
    const data = returndData[0];

    if ( i < 1 ) {
      message += `${data.trade_date_kst} ${data.trade_time_kst} 조간 브리핑\\r\\n\\r\\n`;
    }
    message += `종목\\t: ${data.market}\\r\\n현재가\\t: ${addComma(data.trade_price)}\\r\\n오픈가\\t: ${addComma(data.opening_price)}\\r\\n최고고가\\t: ${addComma(data.high_price)}\\r\\n최저가\\t: ${addComma(data.low_price)}\\r\\n추세\\t: ${data.change}\\r\\n\\r\\n`;
  }
  sendTelegramMsg(message);
}
/**
 * Telegram send messages
 */
function sendTelegramMsg(message) {
  const token = 'telegram token here';
  const chat_id = 'telegam chat id here'
  const url = `https://api.telegram.org/bot${token}/sendmessage?chat_id=${chat_id}&text=${message}`;
  const response = UrlFetchApp.fetch(encodeURI(url));
  console.log(response.getResponseCode());
}
/**
 * 천단위 콤마
 */
function addComma(val) {
  return val.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}

 

 

반응형