import requests import datetime from dateutil import parser import gspread from oauth2client.service_account import ServiceAccountCredentials import time # drop this at PUBLIC2 # TASK LIST HERE # https://docs.google.com/spreadsheets/d/153w046qHhpnZTYU7FJ-S2xyGIH_BXbycLYfLI2fFsZc/edit#gid=0 scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive'] creds = ServiceAccountCredentials.from_json_keyfile_name('choozmointernal-2e314f3d4e12.json', scope) client = gspread.authorize(creds) message = "提醒近期預定執行的作業如下:\n\n" message2 = "提醒近期需完成的作業如下:\n\n" def notify_group(msg): gid='WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD' headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"} r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg}) sheet = client.open('ChoozMo 專案執行表單_20220720') sheet_instance = sheet.get_worksheet(0) records_data = sheet_instance.get_all_records() #print(records_data[0]) for data in records_data: try: print(parser.parse(data["完成日"])) print(datetime.date.today()) deltastart = (parser.parse(data["起始日"]).date()-datetime.date.today()).days deltaend = (parser.parse(data["完成日"]).date()-datetime.date.today()).days print(deltastart, deltaend) if(deltastart <= 7 and data["完成度"] == "預定執行"): message += "專案:" + data["專案"] + "\n" message += "專案項目:" + data["專案項目"] + "\n" message += "作業:" + data["執行項目"] + "\n" message += "執行人員:" + data["指派對象"] + "\n" message += "PM:" + data["PM"] + "\n" message += "執行期間:" + data["起始日"] + "-" + data["完成日"] if(deltastart>0): message += " (尚有"+ str(deltastart) + "天)\n\n" elif(deltastart==0): message += " (當天開始執行)\n\n" else: message += " (過期"+ str(deltastart).replace('-','') + "天)\n\n" elif(deltaend <= 7 and data["完成度"] == "執行中"): message2 += "專案:" + data["專案"] + "\n" message2 += "專案項目:" + data["專案項目"] + "\n" message2 += "作業:" + data["執行項目"] + "\n" message2 += "執行人員:" + data["指派對象"] + "\n" message2 += "PM:" + data["PM"] + "\n" message2 += "完成日:" + data["完成日"] if(deltaend>0): message2 += " (尚有"+ str(deltaend) + "天)\n\n" elif(deltaend==0): message2 += " (當天完成)\n\n" else: message2 += " (過期"+ str(deltaend).replace('-','') + "天)\n\n" except: print("PASS") msg1 = [message[i:i+1000] for i in range(0, len(message), 1000)] for m1 in msg1: #notify_group(m1) #annotated to prevent unintentional trigger print(m1) msg2 = [message2[i:i+1000] for i in range(0, len(message2), 1000)] for m2 in msg2: #notify_group(m2) print(m2) '''notify_group(message) time.sleep(5) notify_group(message2)'''