jobcheck.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import requests
  2. import datetime
  3. from dateutil import parser
  4. import gspread
  5. from oauth2client.service_account import ServiceAccountCredentials
  6. import time
  7. import smtplib
  8. from userdata import *
  9. from email.message import EmailMessage
  10. # drop this at PUBLIC2
  11. # TASK LIST HERE
  12. # https://docs.google.com/spreadsheets/d/153w046qHhpnZTYU7FJ-S2xyGIH_BXbycLYfLI2fFsZc/edit#gid=0
  13. scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  14. creds = ServiceAccountCredentials.from_json_keyfile_name('choozmointernal-2e314f3d4e12.json', scope)
  15. client = gspread.authorize(creds)
  16. message = "提醒近期預定執行的作業如下:\n\n"
  17. message2 = "提醒近期需完成的作業如下:\n\n"
  18. message3 = "提醒以下已取消的作業需填寫取消原因:\n\n"
  19. def notify_group(msg):
  20. gid='WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD'
  21. headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
  22. r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
  23. def send_line(messages):
  24. for message in messages:
  25. print(len(message))
  26. if(len(message)>55):
  27. msg = [message[i:i+1000] for i in range(0, len(message), 1000)]
  28. for m in msg:
  29. notify_group(m)
  30. #print(m)
  31. def send_email(emails, unstarted, incomplete, canceled, has_unstarted, has_incomplete, has_canceled):
  32. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  33. server.ehlo()
  34. server.login(gmail_username, gmail_password)
  35. for name in emails:
  36. msg = EmailMessage()
  37. msg['Subject'] = '執行表單過期作業通知' + str(datetime.date.today())
  38. msg['From'] = "service@choozmo.com"
  39. msg['To'] = emails[name]
  40. message = name + "您好,\n\n以下通知您目前作業現況,更新執行表單請至 https://cmm.ai/pm ,謝謝您。\n\n"
  41. if has_unstarted[name] == 0 and has_overdue[name] == 0:
  42. message += "您目前沒有過期的作業。\n\n"
  43. else:
  44. if has_unstarted[name] == 1:
  45. message += unstarted[name]
  46. if has_incomplete[name] == 1:
  47. message += incomplete[name]
  48. if has_canceled[name] == 1:
  49. message += canceled[name]
  50. msg.set_content(message)
  51. print ('Sending email to ' + msg['To'])
  52. server.send_message(msg)
  53. print ('Email sent to ' + msg['To'])
  54. fname = name+'.txt'
  55. with open(fname, 'w', encoding="UTF+8") as f:
  56. f.write(message)
  57. server.close()
  58. sheet = client.open('ChoozMo 專案執行表單_20220720')
  59. sheet_instance = sheet.get_worksheet(0)
  60. records_data = sheet_instance.get_all_records()
  61. #print(records_data[0])
  62. emails = {"Jared": "jared@choozmo.com", "Morrison": "morrison@choozmo.com", "Doris": "doris@choozmo.com", "Nina": "nina.huang@choozmo.com", "Yukyo": "yukyo@choozmo.com", "Andy": "andy@choozmo.com", "Oak": "oak@choozmo.com", "Zooey": "zooey@choozmo.com", "Yuri": "syuanyuri@choozmo.com", "Wizer": "wizer@choozmo.com", "Jason": "jason@choozmo.com", "Tomoya": "tomoya@choozmo.com", "詠心": "noodlesloves@gmail.com", "献竤": "fxp87257@gmail.com"}
  63. overdue_notstarted_content = {}
  64. overdue_incomplete_content = {}
  65. canceled_content = {}
  66. has_unstarted = {}
  67. has_overdue = {}
  68. has_canceled = {}
  69. status = [0,0,0]
  70. for e in emails.keys():
  71. overdue_notstarted_content[e] = "您有已過期而尚未執行的作業,請儘快執行並完成這些作業:\n\n"
  72. overdue_incomplete_content[e] = "您有已過期而未完成的作業,請儘快完成這些作業:\n\n"
  73. canceled_content[e] = "此外,以下已取消執行的作業請在備註欄位填寫取消原因:\n\n"
  74. has_unstarted[e] = 0
  75. has_overdue[e] = 0
  76. has_canceled[e] = 0
  77. for data in records_data:
  78. try:
  79. if(data["完成度"] == "取消"):
  80. status[2] = 1
  81. info += "專案:" + data["專案"] + " / " + data["專案項目"] + "\n"
  82. info += "作業:" + data["執行項目"] + "\n"
  83. info += "執行人員:" + data["指派對象"] + "\n"
  84. info += "PM:" + data["PM"] + "\n\n"
  85. canceled_content[data["PM"]] += info
  86. has_canceled[data["PM"]] = 1
  87. if [data["PM"]]!=[data["指派對象"]]: #sometimes you have to do the task and you are the PM
  88. canceled_content[data["指派對象"]] += info
  89. has_canceled[data["指派對象"]] = 1
  90. if data["PM"] != "Jared": #sometimes Jared is the PM
  91. canceled_content["Jared"] += info
  92. has_canceled["Jared"] = 1
  93. message3 += info
  94. else:
  95. print(parser.parse(data["起始日"]), parser.parse(data["完成日"]))
  96. deltastart = (parser.parse(data["起始日"]).date()-datetime.date.today()).days
  97. deltaend = (parser.parse(data["完成日"]).date()-datetime.date.today()).days
  98. print(deltastart, deltaend)
  99. info = ""
  100. print("Project: " + data["專案"] + "Note: " + data["備註"])
  101. if(deltastart <= 7 and data["完成度"] == "預定執行"):
  102. status[0] = 1
  103. info += "專案:" + data["專案"] + " / " + data["專案項目"] + "\n"
  104. info += "作業:" + data["執行項目"] + "\n"
  105. info += "執行人員:" + data["指派對象"] + "\n"
  106. info += "PM:" + data["PM"] + "\n"
  107. info += "預定執行期間:" + data["起始日"] + "-" + data["完成日"]
  108. if(deltastart>0):
  109. info += " (尚有"+ str(deltastart) + "天)\n\n"
  110. elif(deltastart==0):
  111. info += " (當天開始執行)\n\n"
  112. else:
  113. info += " (過期"+ str(deltastart).replace('-','') + "天)\n\n"
  114. overdue_notstarted_content[data["PM"]] += info
  115. has_unstarted[data["PM"]] = 1
  116. if [data["PM"]]!=[data["指派對象"]]: #sometimes you have to do the task and you are the PM
  117. overdue_notstarted_content[data["指派對象"]] += info
  118. has_unstarted[data["指派對象"]] = 1
  119. if data["PM"] != "Jared": #sometimes Jared is the PM
  120. overdue_notstarted_content["Jared"] += info
  121. has_unstarted["Jared"] = 1
  122. message += info
  123. elif(deltaend <= 7 and data["完成度"] == "執行中"):
  124. status[1] = 1
  125. info += "專案:" + data["專案"] + " / " + data["專案項目"] + "\n"
  126. info += "作業:" + data["執行項目"] + "\n"
  127. info += "執行人員:" + data["指派對象"] + "\n"
  128. info += "PM:" + data["PM"] + "\n"
  129. info += "完成日:" + data["完成日"]
  130. if(deltaend>0):
  131. info += " (尚有"+ str(deltaend) + "天)\n\n"
  132. elif(deltaend==0):
  133. info += " (當天完成)\n\n"
  134. else:
  135. info += " (過期"+ str(deltaend).replace('-','') + "天)\n\n"
  136. overdue_incomplete_content[data["PM"]] += info
  137. has_overdue[data["PM"]] = 1
  138. if [data["PM"]]!=[data["指派對象"]]: #sometimes you have to do the task and you are the PM
  139. overdue_incomplete_content[data["指派對象"]] += info
  140. has_overdue[data["指派對象"]] = 1
  141. if data["PM"] != "Jared": #sometimes Jared is the PM
  142. overdue_incomplete_content["Jared"] += info
  143. has_overdue["Jared"] = 1
  144. message2 += info
  145. except:
  146. print("PASS")
  147. message += "更新執行表單請至 https://cmm.ai/pm"
  148. message2 += "更新執行表單請至 https://cmm.ai/pm"
  149. message3 += "更新執行表單請至 https://cmm.ai/pm"
  150. messages = []
  151. messages.append(message)
  152. messages.append(message2)
  153. messages.append(message3)
  154. print(has_unstarted)
  155. print(has_overdue)
  156. print(has_canceled)
  157. print(status)
  158. fname="msg1.txt"
  159. with open(fname, 'w', encoding="UTF+8") as f:
  160. f.write(message)
  161. fname="msg2.txt"
  162. with open(fname, 'w', encoding="UTF+8") as f:
  163. f.write(message2)
  164. fname="msg3.txt"
  165. with open(fname, 'w', encoding="UTF+8") as f:
  166. f.write(message3)
  167. send_line(messages)
  168. send_email(emails, overdue_notstarted_content, overdue_incomplete_content, canceled_content, has_unstarted, has_overdue, has_canceled)
  169. '''notify_group(message)
  170. time.sleep(5)
  171. notify_group(message2)'''