GCP/Apps Script

Apps Script로 지난 밤 Nasdaq 지수 Telegram으로 아침에 조간 브리핑 받기

whistory 2023. 1. 5. 10:59
반응형

 

 

 

매일아침에 정보를 받는데, 욕심이 생긴다.

지난밤 Nasdaq 지수도 받아보고 싶다.

 

 

function nasdaqYesterday() {
  const nasdaqUrl = "<https://quote.cnbc.com/quote-html-webservice/restQuote/symbolType/symbol?symbols=.IXIC&requestMethod=itv&noform=1&partnerId=2&fund=1&exthrs=1&output=json&events=1>";

  const response = UrlFetchApp.fetch(nasdaqUrl, {
    method : "GET",
    header:{
      "contentType" : "application/json"
    }
  });

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

  const message = `${data.last_time} ${data.shortName}\\r\\n종가\\t: ${addComma(data.last)}\\r\\n오픈가\\t: ${addComma(data.open)}\\r\\n최고가\\t: ${addComma(data.high)}\\r\\n최저가\\t: ${addComma(data.low)}\\r\\n전일대비\\t: ${data.change}\\r\\n전일대비(%)\\t: ${data.change_pct}`;

  sendTelegramMsg(message);
}

/**
 * Telegram send messages
 */
function sendTelegramMsg(message) {
  const token = 'telegram token here';
  const chat_id = '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(?

 

 

 

 

 

 

반응형