From 562d801d078e44a57de21d5ce4c7c5bca61558c8 Mon Sep 17 00:00:00 2001 From: "Carlos D. Escobar-Valbuena" Date: Fri, 26 Jan 2024 02:51:34 -0500 Subject: [PATCH] added sendgrid email notification --- poetry.lock | 43 ++++++++++++++++++++++++-- pyproject.toml | 1 + vortex/dagster/schema.sql | 1 + vortex/dagster/vortex/assets/vortex.py | 38 +++++++++++++++++------ 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/poetry.lock b/poetry.lock index 4cdac83..ac27d82 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4307,6 +4307,18 @@ websockets = ">=5.0" [package.extras] dev = ["black", "flake8", "flake8-black", "flake8-docstrings", "flake8-isort", "gitchangelog", "pre-commit", "pystache"] +[[package]] +name = "python-http-client" +version = "3.3.7" +description = "HTTP REST client, simplified for Python" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +files = [ + {file = "python_http_client-3.3.7-py3-none-any.whl", hash = "sha256:ad371d2bbedc6ea15c26179c6222a78bc9308d272435ddf1d5c84f068f249a36"}, + {file = "python_http_client-3.3.7.tar.gz", hash = "sha256:bf841ee45262747e00dec7ee9971dfb8c7d83083f5713596488d67739170cea0"}, +] + [[package]] name = "python-multipart" version = "0.0.6" @@ -4785,6 +4797,22 @@ trio-websocket = ">=0.9,<1.0" typing_extensions = ">=4.9.0" urllib3 = {version = ">=1.26,<3", extras = ["socks"]} +[[package]] +name = "sendgrid" +version = "6.11.0" +description = "Twilio SendGrid library for Python" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +files = [ + {file = "sendgrid-6.11.0-py3-none-any.whl", hash = "sha256:43ecf5bb742ea5850c7cfe68f5e7d9948772352306d4e83e119899959538b884"}, + {file = "sendgrid-6.11.0.tar.gz", hash = "sha256:71424b2a97f5a034121ea3b2666c653ba0ed315982f0d57b7851c0c9503dc5ab"}, +] + +[package.dependencies] +python-http-client = ">=3.2.1" +starkbank-ecdsa = ">=2.0.1" + [[package]] name = "setuptools" version = "69.0.3" @@ -4985,6 +5013,17 @@ dev = ["build", "flake8"] doc = ["sphinx"] test = ["pytest", "pytest-cov"] +[[package]] +name = "starkbank-ecdsa" +version = "2.2.0" +description = "A lightweight and fast pure python ECDSA library" +category = "main" +optional = false +python-versions = "*" +files = [ + {file = "starkbank-ecdsa-2.2.0.tar.gz", hash = "sha256:9399c3371b899d4a235b68a1ed7919d202fbf024bd2c863ae8ebdad343c2a63a"}, +] + [[package]] name = "starlette" version = "0.32.0.post1" @@ -6020,5 +6059,5 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" -python-versions = "^3.11.7" -content-hash = "04c12b617cd54910a376da0a0c06c1b07a83754708d5d69dbc16d896160efbec" +python-versions = "^3.11" +content-hash = "af7d902544682816d7b12926ddd9f4cf7a7209fef2eb5b8f4af2aa9494101fc9" diff --git a/pyproject.toml b/pyproject.toml index 5804f5a..6aadd34 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ tiktoken = "^0.5.2" pymupdf = "^1.23.19" duckduckgo-search = "^4.2" wikipedia = "^1.4.0" +sendgrid = "^6.11.0" [tool.poetry.group.dev.dependencies] pytest = "^7.4.0" diff --git a/vortex/dagster/schema.sql b/vortex/dagster/schema.sql index 26eb4fd..4c892d1 100644 --- a/vortex/dagster/schema.sql +++ b/vortex/dagster/schema.sql @@ -16,6 +16,7 @@ create server airtable_server create foreign table airtable_articles ( article_id int4, url text, + email text, created_at timestamp, last_update timestamp ) diff --git a/vortex/dagster/vortex/assets/vortex.py b/vortex/dagster/vortex/assets/vortex.py index 01aceac..e9405ff 100644 --- a/vortex/dagster/vortex/assets/vortex.py +++ b/vortex/dagster/vortex/assets/vortex.py @@ -1,22 +1,17 @@ # %% import json +import os from typing import Tuple -from dagster import ( - AssetExecutionContext, - AssetOut, - MetadataValue, - Out, - Output, - asset, - multi_asset, - op, -) +from dagster import (AssetExecutionContext, AssetOut, MetadataValue, Out, + Output, asset, multi_asset, op) from langchain import hub from langchain.agents import AgentExecutor, create_openai_tools_agent from langchain_openai import ChatOpenAI from openai import OpenAI +from sendgrid import SendGridAPIClient +from sendgrid.helpers.mail import Mail from ..resources import PostgresResource from ..tools import scrape_website, tools @@ -204,3 +199,26 @@ def write_consolidated_summary(context, consolidated_summary, get_articles_summa # %% + +@asset( + deps=[update_articles_table_with_summary], + group_name="gather_articles", +) +def send_email_with_sendgrid(context, get_url, summarize_article): + email = get_url[2] + message = Mail( + from_email='carlos@broomva.tech', + to_emails=email, + subject='Here is your URL summary! 🎉', + plain_text_content=summarize_article) + + context.log.info(f"Sending email to {email}") + context.log.info(f"Sending email with {summarize_article}") + + try: + sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY')) + response = sg.send(message) + context.log.info(f"Email sent. Status code: {response.status_code}") + except Exception as e: + context.log.error(f"Failed to send email: {e}") + raise e