Skip to content

Commit

Permalink
Deseperate code
Browse files Browse the repository at this point in the history
  • Loading branch information
seohyunjun committed May 15, 2024
1 parent d1fd22f commit 5d2cbd3
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 88 deletions.
96 changes: 48 additions & 48 deletions .github/workflows/slack-imap.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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/[email protected]
# 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
99 changes: 60 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 = "<blank>"
return s

def get_mail(num, genai):
result, email_data = server.uid("fetch", num, "(RFC822)")
raw_email = email_data[0][1]
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ python-dateutil
google-generativeai
google-auth>2.15.0
html2text
slack-sdk

0 comments on commit 5d2cbd3

Please sign in to comment.