끄적끄적 인턴생활 (2020)/슬랙봇 만들기(2020)

Slack bot READ_ME (2021 ver.)

jellylucy 2022. 1. 17. 02:35

 

<Code Logic>

1. today blob list 들 print

출처 입력

-> 불필요한 list ( 오늘일자가 아닌 list) 미포함 시켜 list print 하기

 

(1) import re 이용

 

re.match 으로 원하는 값만 가져오기 설정

 

(2) import datetime

 

re.match의 인자를 오늘날짜(blob날짜 폴더명 형식)으로 지정하기 위해

datetime.datetime.now().strftime() 이용

print(blobs_filename_list)
now = datetime.datetime.now()
print(now)          # 2018-07-28 12:11:32.669083

nowDate = now.strftime('%Y-%m-%d')
print(nowDate)      # 2018-07-28.
 

(3) bloblist list에 저장하기

 

 

설정한 시간 간격으로 list print 후 비교하여 업데이트 유무 판단

출처 입력

WHILE문 전체코드

new_blob = list()
post_num = 0

while True:


    for blob_filename in blobs_filename_list:
        if re.match(nowDate, blob_filename.name):
            new_blob.append(blob_filename.name)

    # print(new_blob)

    if(post_num != len(new_blob)):

        late_num = int(len(new_blob))

        for i in range(post_num,late_num):
            
            message = f"""
            New file upload :
            """ + new_blob[i]
            
            print("Update File : " + new_blob[i])

            data = {'Content-Type': 'application/x-www-form-urlencoded',
            'token': slack_token,'channel': channel_id, 
            'text': message,'reply_broadcast': 'True', } 
            URL = "https://slack.com/api/chat.postMessage"
            res = requests.post(URL, data=data)

        post_num = len(new_blob)




    time.sleep(100)
 

(1) 오늘일자 blob list 생성

 

re.match으로 오늘일자(nowDate)만의 blobfile 이름들을

생성한 new_blob 리스트에 append()한다 (값들을 저장)

    for blob_filename in blobs_filename_list:
        if re.match(nowDate, blob_filename.name):
            new_blob.append(blob_filename.name)
 

while 문 내의 IF문

(1) If, 그 전에 생성한 list의 원소개수 / 지금생성한 list 원소개수 다르다

 

(2) new_blob의 원소개수를 late_num 이란 변수값에 저장

 

(3) for문 : post_num ~ late_num 까지 돌린다

업데이트 되는 파일들만 출력해주기 위해.

 

-message : New file upload + new_blob[i]

-data : text값에 message 저장

-URL : api 이용하는 postMessage

-res : requests.post인자에 (URL, data) 값을 넣어주어 post, 출력문 요청한다.

 

 

(4)for문이 끝난 후, new_blob의 원소개수를 post_num에 넣는다.

 

 

 

마지막 if문이 끝난 뒤

(5)while문 실행을 TIME.SLEEP(100)으로 조절한다.

time.sleep(100)