Spaces:
Runtime error
Runtime error
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"] | |
} | |
} | |
}, | |
}, | |
} | |