[Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 - fin
프로젝트/[ed]_슬랙봇_증시알림

[Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 - fin

728x90
반응형

본 블로그 첫번째 프로젝트로 슬랙 알림봇을 활용하여 매일 증시알림을 해주는 시스템을 만들었습니다.

프로젝트의 개요 및 목차; 각 포스팅별 링크와 내용을 아래에 기입해두었습니다. 확인해보시고 궁금하신 내용 확인해보세요.

 

1. 개요

- 목적 : 매일 네이버 증권에서 확인하던걸 편하게 확인하기 위함

- 내용 : 매일 6시 평일 (공휴일은 반영 못함) 아래의 항목을 알려 줌.

  → 코스피 지수 및 전일대비 변동량

  → 투자자별 / 프로그램 매매동향

  → 1일치 코스피 변동 그래프

 

증시 알림 봇 설정 예시.

 

2. 목차

2021.09.22 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (1)

 → 슬랙봇 만들기 위한 슬랙 설정방법 (1)

 

2021.09.22 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (2)

 → 슬랙봇 만들기 위한 슬랙 설정방법 (2)

 → 파이썬 활용한 기본적인 슬랙봇 호출방법 (텍스트 메시지)

 

2021.09.24 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (3) - 1

 → 네이버 증권에서 정보 크롤링 (웹스크래이핑 기능 활용 (Beautiful Soup, requests))

 

2021.09.25 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (3) - 2

 → 크롤링 결과 가공하기

 → 가공한 결과 슬랙봇에 업로드 하기

 

2021.10.02 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (4)

 → 매일 원하는 시간에 메시지 보내도록 하기

 → 자동부팅, 자동로그인, 작업스케줄러 등록

 

2021.10.12 - [프로젝트] - [Python] 슬랙(Slack) 알림봇 설정하여 매일 증시 알림 받기 (5)

 → 문제점 수정. attachment 의 이미지가 전부 동일한 현상.

 → 슬랙봇에 이미지 파일 업로드

 

3. 폴더 구조 및 코드

- 폴더 구조 : 

 - Images : 일간 코스피 차트 이미지 저장

 - src : 관련 소스코드 저장

   - computer_turn_off.py : 컴퓨터 꺼짐을 슬랙봇에서 알려주고 컴퓨터 꺼짐

   - Slacker_notice_functions.py : 웹스크래이핑하여 정보 크롤링, 슬랙봇에 메시지 전달, 파일 업로드 함수

   - Slacker_notices.py : main 문. 함수를 호출해서 필요한 정보 가공 및 슬랙봇에 메시지 내용, 파일 업로드 실시

 

폴더 구조

 

 

- 코드 : 

주석은 상세하게 하지는 않았습니다. 궁금한 점은 이전 포스팅 글 참조하시거나 댓글 부탁드립니다.

 

# computer_turn_off.py

import os
from Slacker_notice_functions import *
from datetime import datetime

date_notice = datetime.today().strftime('%Y-%m-%d %H시 %M분')
Token = 'xoxb-' # 자신의 Token 입력
notice_message = f'{date_notice}, 컴퓨터가 꺼집니다.'

notice_text(Token, '#컴퓨터-알림', notice_message)
os.system('shutdown -s -f')

 

# Slacker_notice_functions.py

from bs4 import BeautifulSoup
import requests
import json

def KospiRead():
    '''
    오늘의 KOSPI 관련 정보를 알려주는 함수. \n
    리턴 값 목록 : 
    1. trend_value : 투자자별, 프로그램 매매동향 값
    2. s : 코스피 차트 이미지 주소 (장시간동안 변화)
    3. a3 : 오늘의 KOSPI 값 / 등락값 / 등락률 값 (문자열)
    '''

    url = 'https://finance.naver.com/sise/sise_index.naver?code=KOSPI'
    # 네이버 금융이 헤더를 요구하는 방식으로 바뀌었다. 그래서 헤더가 필요하다.
    headers = {' '}
    req = requests.get(url, headers = headers)

    soup = BeautifulSoup(req.text, 'lxml')
    daily = soup.find('div', class_ = 'graph')
    s = str(daily.img['src']) # 코스피 이미지 파일 주소.

    posts = soup.find('dl', class_ = 'lst_kos_info')
    post_title_1 = posts.find("dt").get_text() # 투자자별 매매동향
    post_title_2 = posts.find("dt", class_ = "last").get_text() # 프로그램별 매매동향

    trend_value = [] # 개인 , 외국인, 기관, 차익, 비차익, 전체
    characters = ",억" # 억으로 표기된 글자를 지우기 위해서 만듬
    operator = []
    i = 0

    for td in posts.parent.find_all('dd'):
        if td.text.find('+') != -1: # 양수
            operator.append('+')
        else: # -1 이 나온경우. 음수임.
            operator.append('-')
        refined_text = ''.join(x for x in str(td.text) if x not in characters)
        trend_value.append(str(refined_text.split(operator[i])[1]))
        trend_value[i] = operator[i] + trend_value[i]
        i = i + 1

    daily2 = soup.find('div', id = "quotient").get_text()
    a1 = daily2.split('\n')
    a2 = a1[2].split('%')[0]
    
    if a2.find('+') != -1: # + 인 경우
        a4 = '△'
    else:
        a4 = '▽'

    a3 = []
    a3.append(a1[1] + ' ')
    a3.append(a4 + a2 + '%')

    return s, trend_value, a3


def notice_message(token, channel, text, attachments):
    '''
    메시지를 보내는 부분. 함수 안 argument 순서 : \n
    token : Slack Bot의 토큰
    channel : 메시지를 보낼 채널 #stock_notice
    text : Slack Bot 이 보낼 텍스트 메시지. 마크다운 형식이 지원된다.
    attachments : 링크. 텍스트 이외에 URL 이미지등을 첨부할 수 있다.
    '''
    attachments = json.dumps(attachments) # 리스트는 Json 으로 덤핑 시켜야 Slack한테 제대로 간다.
    response = requests.post("https://slack.com/api/chat.postMessage",
        headers={"Authorization": "Bearer "+token},
        data={"channel": channel, "text": text ,"attachments": attachments})

def upload_file(token, channel, files, filename):
    '''
    Slack API 직접호출 파일 업로드 함수. 함수 안 argument 순서 : \n
    token : Slack Bot의 토큰
    channel : 메시지를 보낼 채널 #stock_notice
    files : 첨부파일. 로컬 컴퓨터에서 이미지, pdf 문서등을 업로드 할 수 있다.
    '''

    response2 = requests.post("https://slack.com/api/files.upload",
        headers={"Authorization": "Bearer "+token},
        data={"channels": channel, "filename": filename},
        files = {"file": files})

def notice_text(token, channel, text):
    '''
    Slack Bot 에 텍스트만 전달하는 함수\n
    token : 개인 토큰 코드
    channel : Slack Workspace 채널
    text : 보내고 싶은 메시지
    '''
    response = requests.post("https://slack.com/api/chat.postMessage",
        headers={"Authorization": "Bearer "+token},
        data={"channel": channel, "text": text})

 

# Slacker_notices.py

from Slacker_notice_functions import *
from datetime import datetime
import urllib.request

Token = 'xoxb-' # 자신의 Token 입력
image_url, trend, today_value = KospiRead()
today_date = datetime.today().strftime('%Y-%m-%d')
image_name = 'C:/Projects/Slack_notice/images/' + today_date + '_KOSPI.png' # 파일 경로, 이름 지정 : 날짜_KOSPI.png
urllib.request.urlretrieve(image_url, image_name) # 해당 경로에 이미지 저장
upload_image = (image_name, open(image_name, 'rb'), 'png') # 이미지 형식 지정. 경로, 바이트 타입 파일 파싱, 파일 타입

weekdays_dict = {
    0 : '월',
    1 : '화',
    2 : '수',
    3 : '목',
    4 : '금',
    5 : '토',
    6 : '일'
}

weekdays = datetime.weekday(datetime.today())

if weekdays == 5 or weekdays == 6:
    print(f'weekdays : {weekdays}')
else:
    str1_title = 'KOSPI, ' + today_date + ' (' + weekdays_dict[weekdays] + ')'

    buySell = '개인 : ' + trend[0] + ', 외국인 : ' + trend[1] + ', 기관 : ' + trend[2] + '\n'
    programBuySell = '차익 : ' + trend[3] + ', 비차익 : ' + trend[4] + ', 총합 : ' + trend[5] + '\n'
    kospi_value = today_value[0] + today_value[1] + '\n'
    total_text = kospi_value + buySell + programBuySell

    if today_value[1].find('+') != -1: # + 인 경우,
        today_color = '#ff0000' # 빨강색
    else:
        today_color = '#0000ff' # 파랑색


    attach_dict = {
        'color' : today_color,
        'author_name' : '오늘의 증시 알림',
        'title' : str1_title,
        'title_link' : 'http://finance.naver.com/sise/sise_index.nhn?code=KOSPI',
        'text' : total_text
    }
    
    attach_list=[attach_dict]
    
    notice_message(Token, "#stock_notice", str1_title, attach_list)
    upload_file(Token, "#stock_notice", upload_image, today_date + '_KOSPI.png')

 

아직 보완해야 할 부분이 있긴 하지만,, 여기서 일단락 짓도록 하겠습니다...!!

이제 다음 프로젝트는 가벼운 백테스팅 툴 제작 예정입니다.

728x90
반응형