diff --git a/.github/workflows/slack-imap.yml b/.github/workflows/slack-imap.yml index 9a46883..54dc3f7 100644 --- a/.github/workflows/slack-imap.yml +++ b/.github/workflows/slack-imap.yml @@ -1,55 +1,55 @@ -name: Slack IMAPLIB -on: - push: - branches: - - main +# name: Slack IMAPLIB +# on: +# push: +# branches: +# - main - schedule: - # https://crontab.guru/ - # Past 1 hour day-of-week (Monday through Friday) - - cron: '0 */1 * * 1-5' - workflow_dispatch: +# schedule: +# # https://crontab.guru/ +# # Past 1 hour day-of-week (Monday through Friday) +# - cron: '0 */1 * * 1-5' +# workflow_dispatch: -jobs: - cron: - runs-on: ubuntu-latest +# jobs: +# cron: +# runs-on: ubuntu-latest - steps: - - name: Checkout & Set Environment Variables - uses: actions/checkout@v3 +# steps: +# - name: Checkout & Set Environment Variables +# uses: actions/checkout@v3 - - name: Install dependencies - uses: actions/setup-python@v5 - with: - python-version: '3.11' - cache: 'pip' +# - name: Install dependencies +# uses: actions/setup-python@v5 +# with: +# python-version: '3.11' +# cache: 'pip' - - name: Set up environment variables - run: | - cd $GITHUB_WORKSPACE - echo "IMAP4_SSL=${{ secrets.IMAP4_SSL }}" >> .env - echo "EMAIL=${{ secrets.EMAIL }}" >> .env - echo "PASSWORD=${{ secrets.PASSWORD }}" >> .env - echo "PORT=${{ secrets.PORT }}" >> .env - echo "LABEL=${{ secrets.LABEL }}" >> .env - echo "${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}" | base64 --decode > jq > ${{ secrets.GOOGLE_CREDENTIALS_NAME }} - pip install -r requirements.txt - python3 main.py - env: - GOOGLE_APPLICATION_CREDENTIALS : ${{ secrets.GOOGLE_CREDENTIALS_NAME }} +# - name: Set up environment variables +# run: | +# cd $GITHUB_WORKSPACE +# echo "IMAP4_SSL=${{ secrets.IMAP4_SSL }}" >> .env +# echo "EMAIL=${{ secrets.EMAIL }}" >> .env +# echo "PASSWORD=${{ secrets.PASSWORD }}" >> .env +# echo "PORT=${{ secrets.PORT }}" >> .env +# echo "LABEL=${{ secrets.LABEL }}" >> .env +# echo "${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}" | base64 --decode > jq > ${{ secrets.GOOGLE_CREDENTIALS_NAME }} +# pip install -r requirements.txt +# python3 main.py +# env: +# GOOGLE_APPLICATION_CREDENTIALS : ${{ secrets.GOOGLE_CREDENTIALS_NAME }} - - name: Input email to environment - id: input-email - run: | - echo "email="$(cat send_email.json)"" >> $GITHUB_ENV - - name: Send custom JSON data to Slack workflow - id: slack - uses: slackapi/slack-github-action@v1.24.0 - with: - # For posting a rich message using Block Kit - payload: | - ${{ env.email }} +# - name: Input email to environment +# id: input-email +# run: | +# echo "email="$(cat send_email.json)"" >> $GITHUB_ENV +# - name: Send custom JSON data to Slack workflow +# id: slack +# uses: slackapi/slack-github-action@v1.24.0 +# with: +# # For posting a rich message using Block Kit +# payload: | +# ${{ env.email }} - env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK +# env: +# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} +# SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK diff --git a/README.md b/README.md index 7a95b9b..1c82789 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,10 @@ # slack-imap #### This script sends unread emails to slack in real time. --- -#### (24.2.27) v0.0.1 add workflow +#### (24.5.15) v0.0.3 Deseperate the code (End of free plan) +- Cloud Billing enabled will be billed for Gemini 1.0 Pro requests + +#### (24.2.27) v0.0.2 add workflow - gemini summarize - summarize email diff --git a/main.py b/main.py index 611fa21..ff78a75 100644 --- a/main.py +++ b/main.py @@ -2,20 +2,15 @@ from pytz import timezone import imaplib import os - import html2text - +import time import json import re - from dateutil.parser import parse import datetime - import string - import email from email.header import decode_header, make_header - import google.generativeai as genai load_dotenv() @@ -35,20 +30,21 @@ def clean(text): def genai_transform( text: str, genai: genai, prompt: str = "Summarize 30 words the following text: ", max_lenght: int=210 ): - model = genai.GenerativeModel("gemini-pro") + model = genai.GenerativeModel("gemini-1.5-pro-latest") prompt = prompt + text response = model.generate_content(prompt) text = response.text # slack block message max length is 255. - return text[:max_lenght] + return text def genai_gag(genai: genai)->str: - model = genai.GenerativeModel("gemini-pro") + model = genai.GenerativeModel("gemini-1.5-pro-latest") now = datetime.datetime.now(tz=timezone("Asia/Seoul")) date_format= "%Y-%m-%d" prompt = f"""지금 시간({now:{date_format}})에 맞는 농담이나 재밌는 이야기를 해주세요. 50자 이내로 작성해주세요.""" response = model.generate_content(prompt) + time.sleep(4) text = response.text # slack block message max length is 255. return text @@ -67,6 +63,19 @@ def check_mailbox(server, mailbox): return "Can't connect to the mailbox" +def normalize_text(s:str, sep_token = " \n ")->str: + s = re.sub(r'\s+', ' ', s).strip() + s = re.sub(r". ,","",s) + # remove all instances of multiple spaces + s = s.replace("..",".") + s = s.replace(". .",".") + s = s.replace("\n", "") + s = s.replace("#","") + s = s.strip() + if s =="": + s = "" + return s + def get_mail(num, genai): result, email_data = server.uid("fetch", num, "(RFC822)") raw_email = email_data[0][1] @@ -103,6 +112,38 @@ def get_mail(num, genai): message = genai_transform(f"""{subject.__str__()} {body}""", genai) return f"{date.__str__()} {fr} {message}" +def preprocess_block(message:str, genai: genai) -> list[dict]: + """Preprocess the message to fit the slack block message format.""" + try: + answer = genai_transform( + normalize_text(message), + genai, + prompt="아래 이메일을 요약하고 한국어로 번역 :" + ) + except Exception as e: + answer = f"Error: {e}" + error_block = { + "type": "section", + "text": { + "type": "mrkdwn", + "text": f"{answer}", + } + } + return error_block + + max_legnth = 255 + block_counts = len(answer) // max_legnth + 1 + blocks = [] + for i in range(block_counts): + block = { + "type": "section", + "text": { + "type": "mrkdwn", + "text": f"{answer[i*max_legnth:(i+1)*max_legnth]}", + } + } + blocks.append(block) + return blocks if __name__ == "__main__": genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) @@ -133,35 +174,15 @@ def get_mail(num, genai): mail_text = get_mail(num, genai) fields.append({"type": "mrkdwn", "text": f"{mail_text}"}) summary += f"""{idx}. {mail_text}\n""" + + block=[] + block.append({"type": "section", "fields": fields}) + block.append({"type": "divider"}) + + for info_block in preprocess_block(summary, genai): + block.append(info_block) - if idx == 7: - break - - temp = { - "text": "Unread Email", - "blocks": [ - {"type": "section", "fields": fields}, - {"type": "divider"}, - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": genai_transform( - summary, - genai, - prompt="아래 이메일을 요약하고 한국어로 번역 :", - ), - }, - }, - { - "type": "section", - "text": { - "type": "mrkdwn", - "text": genai_gag(genai), - }, - }, - ], - } - + temp = {"text": "Unread Email","blocks": block} + with open("send_email.json", "w") as outfile: - json.dump(temp, outfile, indent=4) + json.dump(temp, outfile, indent=4) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index c7abb75..f116e79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ python-dateutil google-generativeai google-auth>2.15.0 html2text +slack-sdk \ No newline at end of file