File size: 3,304 Bytes
6eb7feb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import pandas as pd
from openpyxl import load_workbook, Workbook
from datetime import datetime
import uuid


def load_or_create_excel(file_path):
    try:
        wb = load_workbook(file_path)
    except FileNotFoundError:
        wb = Workbook() 
        wb.create_sheet("Users")
        wb.create_sheet("GeneratedEmails")
        wb.create_sheet("TemplateEmails")
        
        users_sheet = wb["Users"]
        users_sheet.append(["UserID", "Name", "Email", "Company", "Role"])
        
        generated_emails_sheet = wb["GeneratedEmails"]
        generated_emails_sheet.append(["EmailID", "UserID", "Subject", "Body", "Timestamp"])
        
        template_emails_sheet = wb["TemplateEmails"]
        template_emails_sheet.append(["TemplateID", "Name", "Subject", "Body"])
        
        wb.save(file_path)
    return wb

# Function to add user to Excel
def add_user_to_excel(file_path, user_data):
    wb = load_or_create_excel(file_path)
    sheet = wb["Users"]
    user_id = str(uuid.uuid4())
    sheet.append([user_id, user_data['name'], user_data['email'], user_data['company'], user_data['role']])
    wb.save(file_path)
    return user_id

# Function to save generated email
def save_email_to_excel(file_path, user_id, subject, body):
    wb = load_or_create_excel(file_path)
    sheet = wb["GeneratedEmails"]
    email_id = str(uuid.uuid4())
    sheet.append([email_id, user_id, subject, body, datetime.now().strftime("%Y-%m-%d %H:%M:%S")])
    wb.save(file_path)

# Function to add template email
def add_template_email(file_path, name, subject, body):
    wb = load_or_create_excel(file_path)
    sheet = wb["TemplateEmails"]
    template_id = str(uuid.uuid4())
    sheet.append([template_id, name, subject, body])
    wb.save(file_path)

# Function to get all template emails
def get_template_emails(file_path):
    df = pd.read_excel(file_path, sheet_name="TemplateEmails")
    return df.to_dict('records')

# Function to generate email using Groq LLM (placeholder)
def generate_email(prompt):
    # Implement your Groq LLM email generation logic here
    return f"Subject: Generated Email\n\nBody: {prompt}"

def get_user_by_id(file_path, user_id):
    wb = load_workbook(file_path)
    sheet = wb["Users"]
    for row in sheet.iter_rows(min_row=2, values_only=True):
        if row[0] == user_id:
            return {'name': row[1], 'email': row[2], 'company': row[3], 'role': row[4]}
    return None

def update_user_in_excel(file_path, user_id, user_data):
    wb = load_workbook(file_path)
    sheet = wb["Users"]
    for row in sheet.iter_rows(min_row=2):
        if row[0].value == user_id:
            row[1].value = user_data['name']
            row[2].value = user_data['email']
            row[3].value = user_data['company']
            row[4].value = user_data['role']
            break
    wb.save(file_path)

def store_sent_email(file_path, sender_email, receiver_email, subject, body):
    wb = load_or_create_excel(file_path)
    sheet = wb["GeneratedEmails"]
    email_id = str(uuid.uuid4())
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    # Append the sent email details to the sheet
    sheet.append([email_id, sender_email, receiver_email, subject, body, timestamp])
    
    wb.save(file_path)
    return email_id