Browse Source

[bin] Some minor fixes to checking work email

Colin Powell 2 months ago
parent
commit
939eec9cd7
1 changed files with 15 additions and 7 deletions
  1. 15 7
      bin/.bin/checkworkmail.py

+ 15 - 7
bin/.bin/checkworkmail.py

@@ -26,6 +26,7 @@ NTFY_TOPIC = os.getenv("NTFY_TOPIC", "dev-notifications")
 NTFY_SERVER = os.getenv("NTFY_SERVER", "https://ntfy.sh")
 CHECK_INTERVAL = int(os.getenv("CHECK_INTERVAL", "60"))
 JIRA_SENDERS = ["jira@yourcompany.com", "jira@atlassian.net"]
+SLACK_SENDER = "notification@slack.com"
 GITHUB_SENDER = "notifications@github.com"
 JIRA_BASE_URL = os.getenv("JIRA_BASE_URL", "https://yourcompany.atlassian.net/browse")
 
@@ -73,7 +74,6 @@ def extract_jira_link(subject):
 
 def format_message(source, subject, body, link=None):
     return body.strip() or ""
-    return "\n".join(lines)
 
 def send_ntfy_notification(title, message):
     url = f"{NTFY_SERVER.rstrip('/')}/{NTFY_TOPIC}"
@@ -85,15 +85,14 @@ def is_github_email(from_email):
 def is_jira_email(from_email, subject):
     return any(s in from_email.lower() for s in JIRA_SENDERS) or re.search(r'[A-Z]+-\d+', subject)
 
+def is_slack_email(from_email, subject):
+    return SLACK_SENDER in from_email.lower()
+
 def archive_message(mail, email_id):
     try:
         if isinstance(email_id, bytes):
             email_id = email_id.decode()
-
-        # Properly remove the \Inbox label (archives the email)
-        # X-GM-LABELS is a Gmail IMAP extension
-        mail.store(email_id, '-X-GM-LABELS', r'(\Inbox)')
-        print(f"Archived message {email_id}")
+        mail.store(email_id, '+X-GM-LABELS', 'processed')
     except Exception as e:
         print(f"Error archiving message {email_id}: {e}")
 
@@ -106,7 +105,12 @@ def check_notifications():
     mail.login(GMAIL_USER, GMAIL_APP_PASSWORD)
     mail.select("inbox")
 
-    result, data = mail.search(None, '(UNSEEN)')
+    #result, data = mail.search(None, '(UNSEEN)')
+    ok, data = mail.search(None, 'UNSEEN X-GM-LABELS', "inbox")
+
+    #search_criteria = r'(UNSEEN X-GM-LABELS "\\Inbox")'
+    #result, data = mail.uid('search', None, search_criteria)
+
     mail_ids = data[0].split()
 
     if not mail_ids:
@@ -129,6 +133,9 @@ def check_notifications():
         elif is_jira_email(from_email, subject):
             source = "Jira"
             link = extract_jira_link(subject)
+        elif is_slack_email(from_email, subject):
+            source = "Slack"
+            link = ""
         else:
             print(f"Skipping non-matching email: {subject}")
             continue
@@ -138,6 +145,7 @@ def check_notifications():
         send_ntfy_notification(subject, message)
         archive_message(mail, num)
 
+    mail.expunge()
     mail.logout()
 
 # ----------------------