태스크 스케쥴러로 자동으로 R 스크립트 실행하기
2019. 6. 8. 22:48ㆍ카테고리 없음

지난 포스트에 R에서 텔레그램봇으로 메세지를 보내는 방법을 업데이트 했었다.
만약, 일정 시간마다 내가 작업한 R 스크립트를 윈도우에서 자동으로 실행하고 그 결과를 텔레그램으로 보낼 수 있다면 실시간으로 상황을 확인할 수 있을 것이다.
R로 텔레그램봇에 메세지 보내기
텔레그램봇을 활용하면, R에서 수집한 정보를 외부에서 메신저를 통해서 확인할 수 있다. R 스크립트를 일정 시간마다 정기적으로 돌리면서 결과값을 보내거나, 혹은 특정 조건이 발견되었을 때 경고 메세지를 발..
aworklab.tistory.com
이 번에 작업해볼 내용은 아래와 같다.
1. 네이버 야구 메인 페이지에 있는 뉴스 20개의 URL을 수집한다
2. URL을 텔레그램봇으로 자동 전송 한다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <code>library(tidyverse) library(telegram.bot) library(rvest) # scarpping naver basellball news 20 parsed = read_html(url) url = parsed % > % html_node( 'div.home_news' ) % > % html_nodes( 'ul' ) % > % html_nodes( 'a' ) % > % html_attr( 'href' ) % > % as.tibble % > % select( - value) # send 20 news url to telegram bot = Bot(token = 'API 토큰을 여기 입력' ) print (bot$getMe()) updates = bot$getUpdates() updates[[ 1 ]]$message$chat x = url[ 1 , 1 ] % > % toString() chat_id = updates[[ 1 ]]$message$chat$ id bot$sendMessage(chat_id = chat_id, text = 'hello' ) for (i in seq(nrow(url))){ sender = url[i, 1 ] % > % toString() bot$sendMessage(chat_id = chat_id, text = sender) } < / code> |

이 파일을 'r_schedule.R'로 저장한 후 윈도우즈 태스크 스케쥴에 등록해 보자
1 2 3 4 5 6 7 8 9 10 11 12 13 | <code>library(taskscheduleR) setwd( "D:/r_study" ) telegram.schedule = file .path( 'D:/r_study/r_schedule.R' ) ## 지금으로 부터 10초 뒤 실행하고 10분 마다 재 실행 taskscheduler_create(taskname = 'send_telegram' , rscript = telegram.schedule, schedule = 'MINUTE' , starttime = format (Sys.time() + 10 , '%H:%M' ), startdate = format (Sys.time(), '%Y/%m/%d' ), modifier = 10 ) taskscheduler_delete( 'send_telegram' ) # 스케쥴 삭제 < / code> |


