File size: 6,517 Bytes
061239f
 
 
 
e6dd180
 
061239f
e6dd180
 
061239f
 
83a5cfb
e6dd180
061239f
e6dd180
061239f
e6dd180
 
 
 
 
 
 
061239f
 
 
e6dd180
061239f
 
 
83a5cfb
061239f
 
83a5cfb
 
 
 
 
061239f
e6dd180
061239f
e6dd180
 
 
 
 
061239f
e6dd180
 
061239f
e6dd180
83a5cfb
e6dd180
83a5cfb
 
061239f
 
 
e6dd180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
061239f
e6dd180
 
061239f
 
 
e6dd180
 
061239f
 
 
e6dd180
 
 
 
 
061239f
e6dd180
061239f
e6dd180
061239f
e6dd180
061239f
e6dd180
061239f
e6dd180
 
 
 
 
061239f
e6dd180
061239f
e6dd180
061239f
 
 
e6dd180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
061239f
e6dd180
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

import gspread
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

from dotenv import load_dotenv
load_dotenv()

GCP_JSON_KEY = os.environ.get("GCP_JSON_KEY")
MY_EMAIL = "[email protected]"
TEMPLATE_COL_NAME = "Template"

sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))


def saveProfileDetailsInGSheet(
    emailPurpose,
    aboutYou,
    recipientIndustry,
    recipientRole,
    specificDetails
):
    client = gspread.service_account_from_dict(json.loads(GCP_JSON_KEY))

    workBook = "Cold Mail Outreach - User Profiles"
    try:
        spreadsheet = client.open(workBook)
    except gspread.SpreadsheetNotFound:
        print(f"Creating new sheet: {workBook}")
        spreadsheet = client.create(workBook)
        print(f"Created new sheet: {spreadsheet.url}")
        spreadsheet.share(
            MY_EMAIL,
            perm_type='user',
            role='writer',
        )

    sheet = spreadsheet.worksheet("Profiles & Templates")
    sheet.append_row([
        emailPurpose,
        aboutYou,
        recipientIndustry,
        recipientRole,
        specificDetails
    ])
    if not (emailPurpose and aboutYou):
        return {}
    return {
        "response": "Saved profile details in Google Sheet.",
        "display": {
            "text": f"`Saved profile details in Google Sheet` [Link]({spreadsheet.url})",
            "icon": "icons/completed-task.png",
        }
    }


def saveTemplateInGSheet(template: str):
    client = gspread.service_account_from_dict(json.loads(GCP_JSON_KEY))

    workBook = "Cold Mail Outreach - User Profiles"
    try:
        spreadsheet = client.open(workBook)
    except gspread.SpreadsheetNotFound:
        print(f"Sheet not found: {workBook}")
        return {
            "response": "Failed to save template. Sheet not found.",
            "display": {
                "text": "`Failed to save template. Sheet not found.`",
                "icon": "icons/error.png",
            }
        }

    sheet = spreadsheet.worksheet("Profiles & Templates")

    headers = sheet.row_values(1)
    templateColIndex = headers.index(TEMPLATE_COL_NAME) + 1
    lastRow = len([row for row in sheet.get_all_values() if any(row)])

    sheet.update_cell(lastRow, templateColIndex, template)

    return {
        "response": "Saved template in Google Sheet.",
        "display": {
            "text": f"`Saved template in Google Sheet` [Link]({spreadsheet.url})",
            "icon": "icons/completed-task.png",
        }
    }


def sendEmail(
    toEmail: str,
    subject: str,
    htmlContent: str
):
    message = Mail(
        from_email=MY_EMAIL,
        to_emails=toEmail,
        subject=subject,
        html_content=htmlContent,
    )
    try:
        sg.send(message)
        return {
            "response": "Email sent",
            "display": {
                "text": f"`Email sent to {toEmail}`",
                "icon": "icons/mail.png",
            }
        }
    except Exception as e:
        print(e.message)
        return {
            "response": f"Failed to send email. Error: {e.message}",
            "display": {
                "text": f"`Failed to send email to {toEmail}`",
                "icon": "icons/error.png",
            }
        }


toolsInfo = {
    "saveProfileDetailsInGSheet": {
        "func": saveProfileDetailsInGSheet,
        "schema": {
            "type": "function",
            "function": {
                "name": "saveProfileDetailsInGSheet",
                "description": "Saves the email profile details in Google Sheet",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "emailPurpose": {
                            "type": "string",
                            "description": "Purpose of the email"
                        },
                        "aboutYou": {
                            "type": "string",
                            "description": "A bit about you that's required for email content"
                        },
                        "recipientIndustry": {
                            "type": "string",
                            "description": "Industry of the recipient"
                        },
                        "recipientRole": {
                            "type": "string",
                            "description": "Recipient's role in the company"
                        },
                        "specificDetails": {
                            "type": "string",
                            "description": "Any specific details about the email"
                        }

                    },
                    "required": ["emailPurpose", "aboutYou", "recipientIndustry"]
                }
            }
        },
    },

    "saveTemplateInGSheet": {
        "func": saveTemplateInGSheet,
        "schema": {
            "type": "function",
            "function": {
                "name": "saveTemplateInGSheet",
                "description": "Saves the email template in Google Sheet",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "template": {
                            "type": "string",
                            "description": "Email template"
                        }
                    },
                    "required": ["template"]
                }
            }
        },
    },

    "sendEmail": {
        "func": sendEmail,
        "schema": {
            "type": "function",
            "function": {
                "name": "sendEmail",
                "description": "Sends an email to the user",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "toEmail": {
                            "type": "string",
                            "description": "Email address of the recipient"
                        },
                        "subject": {
                            "type": "string",
                            "description": "Subject of the email"
                        },
                        "htmlContent": {
                            "type": "string",
                            "description": "HTML content of the email"
                        }
                    },
                    "required": ["toEmail", "subject", "htmlContent"]
                }
            }
        },
    },

}