반응형
예전에 UPbit api를 이용해 코인 시세를 매일 아침에 telegram으로 받는 걸 만들어봤다.
이제 이걸 telegram이 아니라 카카오톡 메세지로 받아보려 한다.
/**
* nasdaq, upbit 시세 받기
*/
function goodMorningFinance() {
const coinArr = ["BTC","ETH", "XRP"];
var message = "";
var kakaoMessage = "";
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 ) {
kakaoMessage += `${data.trade_date_kst} ${data.trade_time_kst} 코인 시세\\\\n\\\\n`;
message += `${data.trade_date_kst} ${data.trade_time_kst} 코인 시세\\r\\n\\r\\n`;
}
kakaoMessage += `종목\\\\t\\\\t: ${data.market}\\\\n현재가\\\\t: ${addComma(data.trade_price)}\\\\n오픈가\\\\t: ${addComma(data.opening_price)}\\\\n최고고가\\\\t: ${addComma(data.high_price)}\\\\n최저가\\\\t: ${addComma(data.low_price)}\\\\n추세\\\\t\\\\t: ${data.change}\\\\n\\\\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`;
}
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 responseNasdaq = UrlFetchApp.fetch(nasdaqUrl, {
method : "GET",
header:{
"contentType" : "application/json"
}
});
const jsonNasdaq = responseNasdaq.getContentText();
const returnNasdaqData = JSON.parse(jsonNasdaq);
const dataNasdaq = returnNasdaqData.FormattedQuoteResult.FormattedQuote[0];
message += `${dataNasdaq.shortName} (${dataNasdaq.last_time}) \\r\\n종가\\t: ${addComma(dataNasdaq.last)}\\r\\n오픈가\\t: ${addComma(dataNasdaq.open)}\\r\\n최고가\\t: ${addComma(dataNasdaq.high)}\\r\\n최저가\\t: ${addComma(dataNasdaq.low)}\\r\\n전일대비\\t: ${dataNasdaq.change}\\r\\n전일대비(%): ${dataNasdaq.change_pct}`;
kakaoMessage += `${dataNasdaq.shortName} (${dataNasdaq.last_time}) \\\\n종가\\\\t\\\\t: ${addComma(dataNasdaq.last)}\\\\n오픈가\\\\t: ${addComma(dataNasdaq.open)}\\\\n최고가\\\\t: ${addComma(dataNasdaq.high)}\\\\n최저가\\\\t: ${addComma(dataNasdaq.low)}\\\\n전일대비\\\\t: ${dataNasdaq.change}`;
sendTelegramMsg(message);
kakao.sendKakaoMessageToMe(kakaoMessage);
}
function sendKakaoMessageToMe(message) {
const NEW_TOKEN = getAuthRefresh();
const url = "https://kapi.kakao.com/v2/api/talk/memo/default/send";
var dataString = `template_object={
"object_type": "text",
"text":"${message}",
"link":{
"web_url": "https://developers.kakao.com",
"mobile_web_url": "https://developers.kakao.com"
},
"button_title": "바로 확인"
}`;
const options = {
method: "POST",
headers: {
Authorization: "Bearer " + NEW_TOKEN
, contentType: "application/x-www-form-urlencoded;charset=utf-8"
},
payload: dataString
};
const res = UrlFetchApp.fetch(url, options).getContentText();
console.log(res);
}
function getAuthRefresh() {
const url = "https://kauth.kakao.com/oauth/token";
data = {
"grant_type" : "refresh_token",
"client_id" : CLIENT_ID,
"redirect_uri" : "https://whiseung.tistory.com",
"refresh_token": REFRESH_TOKEN
}
const options = {
contentType: "application/x-www-form-urlencoded;charset=utf-8",
headers: {
Authorization: "Bearer " + CLIENT_ID
},
payload: data
};
const res = UrlFetchApp.fetch(url, options).getContentText();
const returndData = JSON.parse(res);
return returndData.access_token;
}
반응형
'GCP > Apps Script' 카테고리의 다른 글
Apps Script로 Google Forms 항목(item) 추가하기 (0) | 2023.06.07 |
---|---|
Apps Script로 친구에게 카카오톡 메세지 보내기 (0) | 2023.06.05 |
Apps Script로 카카오톡 메세지 보내기 위해 만료된 토큰 REFRESH 하기 (3) | 2023.06.02 |
Apps Script로 카카오톡 메세지 보내기 (나한테) (5) | 2023.06.01 |
Apps Script와 Google Sites를 이용한 홈페이지 제작 및 배포 (0) | 2023.05.25 |