From 8122268cfba1d3cbf0248a5d7c60bd5086b0547e Mon Sep 17 00:00:00 2001 From: SanjuEpic Date: Wed, 23 Feb 2022 19:00:06 +0100 Subject: [PATCH 1/7] error code --- Sendgrid/Sendgrid_Get_activity_feed.ipynb | 282 ++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 Sendgrid/Sendgrid_Get_activity_feed.ipynb diff --git a/Sendgrid/Sendgrid_Get_activity_feed.ipynb b/Sendgrid/Sendgrid_Get_activity_feed.ipynb new file mode 100644 index 0000000000..78c9947723 --- /dev/null +++ b/Sendgrid/Sendgrid_Get_activity_feed.ipynb @@ -0,0 +1,282 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", + "metadata": {}, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "2ba2f665-d276-4881-81f8-c9524f75796b", + "metadata": {}, + "source": [ + "# Sendgrid - Get activity feed" + ] + }, + { + "cell_type": "markdown", + "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", + "metadata": {}, + "source": [ + "This notebook enables you to get a dataframe of all the activities performed\n", + "\n", + "**Tags:** #sendgrid #activity" + ] + }, + { + "cell_type": "markdown", + "id": "b025116f-fd57-40ae-81db-99d9dbc30445", + "metadata": {}, + "source": [ + "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" + ] + }, + { + "cell_type": "markdown", + "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T17:37:39.893184Z", + "iopub.status.busy": "2022-02-23T17:37:39.892914Z", + "iopub.status.idle": "2022-02-23T17:37:40.307014Z", + "shell.execute_reply": "2022-02-23T17:37:40.306388Z", + "shell.execute_reply.started": "2022-02-23T17:37:39.893112Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import requests\n", + "from sendgrid import SendGridAPIClient\n", + "from sendgrid.helpers.mail import *" + ] + }, + { + "cell_type": "markdown", + "id": "392e708f-7c32-45d7-8937-382c9651fc0f", + "metadata": {}, + "source": [ + "### Variables" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T17:37:41.759913Z", + "iopub.status.busy": "2022-02-23T17:37:41.759682Z", + "iopub.status.idle": "2022-02-23T17:37:41.762612Z", + "shell.execute_reply": "2022-02-23T17:37:41.761935Z", + "shell.execute_reply.started": "2022-02-23T17:37:41.759890Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "api_url = \"https://api.sendgrid.com/v3/messages\"\n", + "sendgrid_token = \"SG.l6ZmXXXXXXXXXxxxXXX.b0XXXXXXXXXXXXXXXXXxxxxx\"" + ] + }, + { + "cell_type": "markdown", + "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "bd3d2b4a-8946-4a45-882b-caf6c843d15c", + "metadata": {}, + "source": [ + "### Sending mails via sendgrid" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6b6790a3-81ae-454d-b036-20e9f735c4e5", + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T17:37:43.275448Z", + "iopub.status.busy": "2022-02-23T17:37:43.275222Z", + "iopub.status.idle": "2022-02-23T17:37:43.281237Z", + "shell.execute_reply": "2022-02-23T17:37:43.280691Z", + "shell.execute_reply.started": "2022-02-23T17:37:43.275424Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "sg = SendGridAPIClient(api_key=sendgrid_token)\n", + "\n", + "from_email = Email(\"abc@gmail.com\", name = \"abc\")\n", + "to_email = To(\"xyz@iiitnr.edu.in\", name = \"xyz\")\n", + "cc_email = Cc(email = [\"alpha@story.com\", \"beta@story.com\"])\n", + "\n", + "subject = \"Sending with SendGrid is Fun\"\n", + "content = Content(\"text/plain\", \"Hey there this is my second conversation with you via sendgrid!\")\n", + "\n", + "mail = Mail(from_email, to_email, subject, content)\n", + "mail.add_cc(cc_email)" + ] + }, + { + "cell_type": "markdown", + "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", + "metadata": {}, + "source": [ + "### Filtering by mail for required stats (Error block)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T17:38:12.152992Z", + "iopub.status.busy": "2022-02-23T17:38:12.152766Z", + "iopub.status.idle": "2022-02-23T17:38:12.603890Z", + "shell.execute_reply": "2022-02-23T17:38:12.603228Z", + "shell.execute_reply.started": "2022-02-23T17:38:12.152969Z" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'errors': [{'message': 'authorization required'}]}\n" + ] + } + ], + "source": [ + "sg = SendGridAPIClient(api_key = sendgrid_token)\n", + "\n", + "## encoded query as per the format ##\n", + "# Visit this link for more details -> https://docs.sendgrid.com/api-reference/e-mail-activity/filter-all-messages\n", + "\n", + "params = {'query': \"from_email%3D%22attili.sanjeet10%40gmail.com%22\", 'limit': 10}\n", + "\n", + "from python_http_client.exceptions import HTTPError\n", + "\n", + "try:\n", + " response = sg.client.messages.get(\n", + " query_params=params\n", + " )\n", + "except HTTPError as e:\n", + " print(e.to_dict)" + ] + }, + { + "cell_type": "markdown", + "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "b54a2952-c67e-418b-b491-40162fb75477", + "metadata": {}, + "source": [ + "### Send the mail" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "0ecdcb09-5d89-41dc-bf94-dfd4288644ef", + "metadata": { + "execution": { + "iopub.execute_input": "2022-02-23T17:38:34.710180Z", + "iopub.status.busy": "2022-02-23T17:38:34.709933Z", + "iopub.status.idle": "2022-02-23T17:38:35.438771Z", + "shell.execute_reply": "2022-02-23T17:38:35.438191Z", + "shell.execute_reply.started": "2022-02-23T17:38:34.710149Z" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "202\n", + "b''\n", + "Server: nginx\n", + "Date: Wed, 23 Feb 2022 17:38:35 GMT\n", + "Content-Length: 0\n", + "Connection: close\n", + "X-Message-Id: Q4ClLhjaQSmQd29z0SA14Q\n", + "Access-Control-Allow-Origin: https://sendgrid.api-docs.io\n", + "Access-Control-Allow-Methods: POST\n", + "Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl\n", + "Access-Control-Max-Age: 600\n", + "X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html\n", + "Strict-Transport-Security: max-age=600; includeSubDomains\n", + "\n", + "\n" + ] + } + ], + "source": [ + "response = sg.client.mail.send.post(request_body=mail.get())\n", + "\n", + "## if status code is 202, congrats! the mail has been sent successfully ##\n", + "\n", + "print(response.status_code)\n", + "print(response.body)\n", + "print(response.headers)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From f4704faa9650cc591a35a040a866a29b5108d5a4 Mon Sep 17 00:00:00 2001 From: FlorentLvr Date: Tue, 8 Mar 2022 15:30:28 +0100 Subject: [PATCH 2/7] feat: (sendgrid) send email --- SendGrid/SendGrid_Send_message.ipynb | 232 +++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 SendGrid/SendGrid_Send_message.ipynb diff --git a/SendGrid/SendGrid_Send_message.ipynb b/SendGrid/SendGrid_Send_message.ipynb new file mode 100644 index 0000000000..b31f1f4943 --- /dev/null +++ b/SendGrid/SendGrid_Send_message.ipynb @@ -0,0 +1,232 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", + "metadata": {}, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "2ba2f665-d276-4881-81f8-c9524f75796b", + "metadata": {}, + "source": [ + "# Sendgrid - Send message" + ] + }, + { + "cell_type": "markdown", + "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", + "metadata": {}, + "source": [ + "**Tags:** #sendgrid #message #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "b025116f-fd57-40ae-81db-99d9dbc30445", + "metadata": {}, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/ACoAABCNSioBW3YZHc2lBHVG0E_TXYWitQkmwog/)" + ] + }, + { + "cell_type": "markdown", + "id": "c11af9c0-6407-4dec-aba3-ed8cb5ea011d", + "metadata": {}, + "source": [ + "This notebook enables you to get a dataframe of all the activities performed" + ] + }, + { + "cell_type": "markdown", + "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import requests\n", + "from sendgrid import SendGridAPIClient\n", + "from sendgrid.helpers.mail import *\n", + "import naas" + ] + }, + { + "cell_type": "markdown", + "id": "392e708f-7c32-45d7-8937-382c9651fc0f", + "metadata": {}, + "source": [ + "### Setup SendGrid\n", + "👉 Get your [api key](https://app.sendgrid.com/settings/api_keys)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")" + ] + }, + { + "cell_type": "markdown", + "id": "12550bc3-82b5-4b9d-88ac-708f66834c4c", + "metadata": {}, + "source": [ + "### Setup your parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e51067ce-e7f8-4e51-b660-af10a25e01ca", + "metadata": {}, + "outputs": [], + "source": [ + "# Email senders and receivers\n", + "email_from =\"\" \n", + "email_from_name = None #custom email from name, 24 char max\n", + "email_to = \"\"\n", + "email_to_name = None #custom email from name\n", + "email_to_cc = None #emails in copy\n", + "\n", + "# Email content\n", + "subject=\"Test SendGrid\"\n", + "content=\"Hi there, Best regards!\"" + ] + }, + { + "cell_type": "markdown", + "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "bd3d2b4a-8946-4a45-882b-caf6c843d15c", + "metadata": {}, + "source": [ + "### Sending mails via sendgrid" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6b6790a3-81ae-454d-b036-20e9f735c4e5", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def send_email(email_from,\n", + " email_to,\n", + " subject,\n", + " content,\n", + " email_to_cc=None,\n", + " email_from_name=None,\n", + " email_to_name=None,\n", + " content_type=\"text/plain\"):\n", + " \n", + " # Connect to SendGrid\n", + " SG = SendGridAPIClient(api_key=SENDGRID_API_KEY)\n", + "\n", + " from_email = Email(email_from, name=email_from_name)\n", + " to_email = To(email_to, name=email_to_name)\n", + " cc_email = Cc(email=email_to_cc)\n", + " content = Content(content_type, content)\n", + "\n", + " mail = Mail(from_email, to_email, subject, content)\n", + " if email_to_cc is not None:\n", + " mail.add_cc(cc_email)\n", + " try:\n", + " res = SG.client.mail.send.post(request_body=mail.get())\n", + " if res.status_code == 202:\n", + " print(f\"📧 Email successfully sent to {email_to}\")\n", + " except requests.HTTPError as e:\n", + " raise(e)" + ] + }, + { + "cell_type": "markdown", + "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "b54a2952-c67e-418b-b491-40162fb75477", + "metadata": {}, + "source": [ + "### Send the mail" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ecdcb09-5d89-41dc-bf94-dfd4288644ef", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "send_email(email_from=email_from,\n", + " email_to=email_to,\n", + " subject=subject,\n", + " content=content,\n", + " email_to_cc=email_to_cc,\n", + " email_from_name=email_from_name,\n", + " email_to_name=email_to_name)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From d94dc6736edac8fda0a45f05435edfed83ddb7b6 Mon Sep 17 00:00:00 2001 From: FlorentLvr Date: Tue, 8 Mar 2022 16:07:32 +0100 Subject: [PATCH 3/7] fix: send message + get activity feed --- SendGrid/SendGrid_Get_activity_feed.ipynb | 220 +++++++++++++++++ SendGrid/SendGrid_Send_message.ipynb | 10 +- Sendgrid/Sendgrid_Get_activity_feed.ipynb | 282 ---------------------- 3 files changed, 224 insertions(+), 288 deletions(-) create mode 100644 SendGrid/SendGrid_Get_activity_feed.ipynb delete mode 100644 Sendgrid/Sendgrid_Get_activity_feed.ipynb diff --git a/SendGrid/SendGrid_Get_activity_feed.ipynb b/SendGrid/SendGrid_Get_activity_feed.ipynb new file mode 100644 index 0000000000..b7fdaa7fd6 --- /dev/null +++ b/SendGrid/SendGrid_Get_activity_feed.ipynb @@ -0,0 +1,220 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", + "metadata": {}, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "2ba2f665-d276-4881-81f8-c9524f75796b", + "metadata": {}, + "source": [ + "# Sendgrid - Get activity feed" + ] + }, + { + "cell_type": "markdown", + "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", + "metadata": {}, + "source": [ + "**Tags:** #sendgrid #activity #snippet" + ] + }, + { + "cell_type": "markdown", + "id": "b025116f-fd57-40ae-81db-99d9dbc30445", + "metadata": {}, + "source": [ + "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" + ] + }, + { + "cell_type": "markdown", + "id": "c11af9c0-6407-4dec-aba3-ed8cb5ea011d", + "metadata": {}, + "source": [ + "This notebook enables you to get a dataframe of all the activities performed" + ] + }, + { + "cell_type": "markdown", + "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", + "metadata": { + "execution": { + "iopub.execute_input": "2022-03-08T15:03:58.650167Z", + "iopub.status.busy": "2022-03-08T15:03:58.649931Z", + "iopub.status.idle": "2022-03-08T15:03:59.971701Z", + "shell.execute_reply": "2022-03-08T15:03:59.970972Z", + "shell.execute_reply.started": "2022-03-08T15:03:58.650084Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import requests\n", + "from sendgrid import SendGridAPIClient\n", + "from sendgrid.helpers.mail import *\n", + "import naas" + ] + }, + { + "cell_type": "markdown", + "id": "392e708f-7c32-45d7-8937-382c9651fc0f", + "metadata": {}, + "source": [ + "### Setup SendGrid\n", + "👉 Get your [api key](https://app.sendgrid.com/settings/api_keys)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", + "metadata": { + "execution": { + "iopub.execute_input": "2022-03-08T15:03:59.974468Z", + "iopub.status.busy": "2022-03-08T15:03:59.974291Z", + "iopub.status.idle": "2022-03-08T15:04:00.094009Z", + "shell.execute_reply": "2022-03-08T15:04:00.093433Z", + "shell.execute_reply.started": "2022-03-08T15:03:59.974448Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")" + ] + }, + { + "cell_type": "markdown", + "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", + "metadata": {}, + "source": [ + "### Get activity" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", + "metadata": { + "execution": { + "iopub.execute_input": "2022-03-08T15:04:35.597978Z", + "iopub.status.busy": "2022-03-08T15:04:35.597757Z", + "iopub.status.idle": "2022-03-08T15:04:35.991990Z", + "shell.execute_reply": "2022-03-08T15:04:35.990601Z", + "shell.execute_reply.started": "2022-03-08T15:04:35.597954Z" + }, + "tags": [] + }, + "outputs": [ + { + "ename": "BadRequestsError", + "evalue": "HTTP Error 400: Bad Request", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mBadRequestsError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'status%3D%22processed%22'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m response = SG.client.messages.get(\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mquery_params\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m )\n", + "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36mhttp_request\u001b[0;34m(request_body, query_params, request_headers, timeout, **_)\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 276\u001b[0m response = Response(\n\u001b[0;32m--> 277\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 278\u001b[0m )\n\u001b[1;32m 279\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, opener, request, timeout)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m body=exc.body))\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mBadRequestsError\u001b[0m: HTTP Error 400: Bad Request" + ] + } + ], + "source": [ + "import os\n", + "from sendgrid import SendGridAPIClient\n", + "\n", + "\n", + "SG = SendGridAPIClient(SENDGRID_API_KEY)\n", + "\n", + "params = {'query': 'status%3D%22processed%22',\n", + " 'limit': 10}\n", + "\n", + "response = SG.client.messages.get(\n", + " query_params=params\n", + ")\n", + "\n", + "print(response.status_code)\n", + "print(response.body)\n", + "print(response.headers)" + ] + }, + { + "cell_type": "markdown", + "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "b54a2952-c67e-418b-b491-40162fb75477", + "metadata": {}, + "source": [ + "### Send the mail" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f9d8bcd0-0502-42b2-8379-eed7b37a7216", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/SendGrid/SendGrid_Send_message.ipynb b/SendGrid/SendGrid_Send_message.ipynb index b31f1f4943..dbff1e268e 100644 --- a/SendGrid/SendGrid_Send_message.ipynb +++ b/SendGrid/SendGrid_Send_message.ipynb @@ -29,7 +29,7 @@ "id": "b025116f-fd57-40ae-81db-99d9dbc30445", "metadata": {}, "source": [ - "**Author:** [Florent Ravenel](https://www.linkedin.com/in/ACoAABCNSioBW3YZHc2lBHVG0E_TXYWitQkmwog/)" + "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" ] }, { @@ -37,7 +37,7 @@ "id": "c11af9c0-6407-4dec-aba3-ed8cb5ea011d", "metadata": {}, "source": [ - "This notebook enables you to get a dataframe of all the activities performed" + "This notebook enables you to send email with SendGrid" ] }, { @@ -65,11 +65,9 @@ }, "outputs": [], "source": [ - "import pandas as pd\n", "import requests\n", "from sendgrid import SendGridAPIClient\n", - "from sendgrid.helpers.mail import *\n", - "import naas" + "from sendgrid.helpers.mail import *" ] }, { @@ -90,7 +88,7 @@ }, "outputs": [], "source": [ - "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")" + "SENDGRID_API_KEY = \"\"" ] }, { diff --git a/Sendgrid/Sendgrid_Get_activity_feed.ipynb b/Sendgrid/Sendgrid_Get_activity_feed.ipynb deleted file mode 100644 index 78c9947723..0000000000 --- a/Sendgrid/Sendgrid_Get_activity_feed.ipynb +++ /dev/null @@ -1,282 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", - "metadata": {}, - "source": [ - "\"Naas\"" - ] - }, - { - "cell_type": "markdown", - "id": "2ba2f665-d276-4881-81f8-c9524f75796b", - "metadata": {}, - "source": [ - "# Sendgrid - Get activity feed" - ] - }, - { - "cell_type": "markdown", - "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", - "metadata": {}, - "source": [ - "This notebook enables you to get a dataframe of all the activities performed\n", - "\n", - "**Tags:** #sendgrid #activity" - ] - }, - { - "cell_type": "markdown", - "id": "b025116f-fd57-40ae-81db-99d9dbc30445", - "metadata": {}, - "source": [ - "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" - ] - }, - { - "cell_type": "markdown", - "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", - "metadata": {}, - "source": [ - "## Input" - ] - }, - { - "cell_type": "markdown", - "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", - "metadata": {}, - "source": [ - "### Imports" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T17:37:39.893184Z", - "iopub.status.busy": "2022-02-23T17:37:39.892914Z", - "iopub.status.idle": "2022-02-23T17:37:40.307014Z", - "shell.execute_reply": "2022-02-23T17:37:40.306388Z", - "shell.execute_reply.started": "2022-02-23T17:37:39.893112Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "import pandas as pd\n", - "import requests\n", - "from sendgrid import SendGridAPIClient\n", - "from sendgrid.helpers.mail import *" - ] - }, - { - "cell_type": "markdown", - "id": "392e708f-7c32-45d7-8937-382c9651fc0f", - "metadata": {}, - "source": [ - "### Variables" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T17:37:41.759913Z", - "iopub.status.busy": "2022-02-23T17:37:41.759682Z", - "iopub.status.idle": "2022-02-23T17:37:41.762612Z", - "shell.execute_reply": "2022-02-23T17:37:41.761935Z", - "shell.execute_reply.started": "2022-02-23T17:37:41.759890Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "api_url = \"https://api.sendgrid.com/v3/messages\"\n", - "sendgrid_token = \"SG.l6ZmXXXXXXXXXxxxXXX.b0XXXXXXXXXXXXXXXXXxxxxx\"" - ] - }, - { - "cell_type": "markdown", - "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", - "metadata": {}, - "source": [ - "## Model" - ] - }, - { - "cell_type": "markdown", - "id": "bd3d2b4a-8946-4a45-882b-caf6c843d15c", - "metadata": {}, - "source": [ - "### Sending mails via sendgrid" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "6b6790a3-81ae-454d-b036-20e9f735c4e5", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T17:37:43.275448Z", - "iopub.status.busy": "2022-02-23T17:37:43.275222Z", - "iopub.status.idle": "2022-02-23T17:37:43.281237Z", - "shell.execute_reply": "2022-02-23T17:37:43.280691Z", - "shell.execute_reply.started": "2022-02-23T17:37:43.275424Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "sg = SendGridAPIClient(api_key=sendgrid_token)\n", - "\n", - "from_email = Email(\"abc@gmail.com\", name = \"abc\")\n", - "to_email = To(\"xyz@iiitnr.edu.in\", name = \"xyz\")\n", - "cc_email = Cc(email = [\"alpha@story.com\", \"beta@story.com\"])\n", - "\n", - "subject = \"Sending with SendGrid is Fun\"\n", - "content = Content(\"text/plain\", \"Hey there this is my second conversation with you via sendgrid!\")\n", - "\n", - "mail = Mail(from_email, to_email, subject, content)\n", - "mail.add_cc(cc_email)" - ] - }, - { - "cell_type": "markdown", - "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", - "metadata": {}, - "source": [ - "### Filtering by mail for required stats (Error block)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T17:38:12.152992Z", - "iopub.status.busy": "2022-02-23T17:38:12.152766Z", - "iopub.status.idle": "2022-02-23T17:38:12.603890Z", - "shell.execute_reply": "2022-02-23T17:38:12.603228Z", - "shell.execute_reply.started": "2022-02-23T17:38:12.152969Z" - }, - "tags": [] - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'errors': [{'message': 'authorization required'}]}\n" - ] - } - ], - "source": [ - "sg = SendGridAPIClient(api_key = sendgrid_token)\n", - "\n", - "## encoded query as per the format ##\n", - "# Visit this link for more details -> https://docs.sendgrid.com/api-reference/e-mail-activity/filter-all-messages\n", - "\n", - "params = {'query': \"from_email%3D%22attili.sanjeet10%40gmail.com%22\", 'limit': 10}\n", - "\n", - "from python_http_client.exceptions import HTTPError\n", - "\n", - "try:\n", - " response = sg.client.messages.get(\n", - " query_params=params\n", - " )\n", - "except HTTPError as e:\n", - " print(e.to_dict)" - ] - }, - { - "cell_type": "markdown", - "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", - "metadata": {}, - "source": [ - "## Output" - ] - }, - { - "cell_type": "markdown", - "id": "b54a2952-c67e-418b-b491-40162fb75477", - "metadata": {}, - "source": [ - "### Send the mail" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "0ecdcb09-5d89-41dc-bf94-dfd4288644ef", - "metadata": { - "execution": { - "iopub.execute_input": "2022-02-23T17:38:34.710180Z", - "iopub.status.busy": "2022-02-23T17:38:34.709933Z", - "iopub.status.idle": "2022-02-23T17:38:35.438771Z", - "shell.execute_reply": "2022-02-23T17:38:35.438191Z", - "shell.execute_reply.started": "2022-02-23T17:38:34.710149Z" - }, - "tags": [] - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "202\n", - "b''\n", - "Server: nginx\n", - "Date: Wed, 23 Feb 2022 17:38:35 GMT\n", - "Content-Length: 0\n", - "Connection: close\n", - "X-Message-Id: Q4ClLhjaQSmQd29z0SA14Q\n", - "Access-Control-Allow-Origin: https://sendgrid.api-docs.io\n", - "Access-Control-Allow-Methods: POST\n", - "Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl\n", - "Access-Control-Max-Age: 600\n", - "X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html\n", - "Strict-Transport-Security: max-age=600; includeSubDomains\n", - "\n", - "\n" - ] - } - ], - "source": [ - "response = sg.client.mail.send.post(request_body=mail.get())\n", - "\n", - "## if status code is 202, congrats! the mail has been sent successfully ##\n", - "\n", - "print(response.status_code)\n", - "print(response.body)\n", - "print(response.headers)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.8.8" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 16f146594c6ad9ac4e1d6195b8e4a9cf61b25434 Mon Sep 17 00:00:00 2001 From: FlorentLvr Date: Tue, 8 Mar 2022 16:14:27 +0100 Subject: [PATCH 4/7] fix: add links to doc --- SendGrid/SendGrid_Get_activity_feed.ipynb | 77 +++++++++++++---------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/SendGrid/SendGrid_Get_activity_feed.ipynb b/SendGrid/SendGrid_Get_activity_feed.ipynb index b7fdaa7fd6..ec6cdada6f 100644 --- a/SendGrid/SendGrid_Get_activity_feed.ipynb +++ b/SendGrid/SendGrid_Get_activity_feed.ipynb @@ -58,24 +58,22 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:03:58.650167Z", - "iopub.status.busy": "2022-03-08T15:03:58.649931Z", - "iopub.status.idle": "2022-03-08T15:03:59.971701Z", - "shell.execute_reply": "2022-03-08T15:03:59.970972Z", - "shell.execute_reply.started": "2022-03-08T15:03:58.650084Z" + "iopub.execute_input": "2022-03-08T15:10:35.859216Z", + "iopub.status.busy": "2022-03-08T15:10:35.858973Z", + "iopub.status.idle": "2022-03-08T15:10:35.862634Z", + "shell.execute_reply": "2022-03-08T15:10:35.862010Z", + "shell.execute_reply.started": "2022-03-08T15:10:35.859181Z" }, "tags": [] }, "outputs": [], "source": [ - "import pandas as pd\n", - "import requests\n", + "import os\n", "from sendgrid import SendGridAPIClient\n", - "from sendgrid.helpers.mail import *\n", "import naas" ] }, @@ -90,15 +88,15 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 9, "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:03:59.974468Z", - "iopub.status.busy": "2022-03-08T15:03:59.974291Z", - "iopub.status.idle": "2022-03-08T15:04:00.094009Z", - "shell.execute_reply": "2022-03-08T15:04:00.093433Z", - "shell.execute_reply.started": "2022-03-08T15:03:59.974448Z" + "iopub.execute_input": "2022-03-08T15:10:36.365249Z", + "iopub.status.busy": "2022-03-08T15:10:36.364821Z", + "iopub.status.idle": "2022-03-08T15:10:36.489828Z", + "shell.execute_reply": "2022-03-08T15:10:36.489117Z", + "shell.execute_reply.started": "2022-03-08T15:10:36.365207Z" }, "tags": [] }, @@ -107,6 +105,25 @@ "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")" ] }, + { + "cell_type": "code", + "execution_count": 14, + "id": "8b52af7b-8eb1-4d85-a46c-364855736be3", + "metadata": { + "execution": { + "iopub.execute_input": "2022-03-08T15:11:10.155795Z", + "iopub.status.busy": "2022-03-08T15:11:10.155561Z", + "iopub.status.idle": "2022-03-08T15:11:10.158670Z", + "shell.execute_reply": "2022-03-08T15:11:10.158014Z", + "shell.execute_reply.started": "2022-03-08T15:11:10.155770Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "sg = SendGridAPIClient(SENDGRID_API_KEY)" + ] + }, { "cell_type": "markdown", "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", @@ -120,20 +137,21 @@ "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", "metadata": {}, "source": [ - "### Get activity" + "### Get activity\n", + "https://docs.sendgrid.com/api-reference/e-mail-activity/filter-all-messages?code-sample=code-filter-all-messages&code-language=Python&code-sdk-version=6.x" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:04:35.597978Z", - "iopub.status.busy": "2022-03-08T15:04:35.597757Z", - "iopub.status.idle": "2022-03-08T15:04:35.991990Z", - "shell.execute_reply": "2022-03-08T15:04:35.990601Z", - "shell.execute_reply.started": "2022-03-08T15:04:35.597954Z" + "iopub.execute_input": "2022-03-08T15:13:51.654719Z", + "iopub.status.busy": "2022-03-08T15:13:51.654491Z", + "iopub.status.idle": "2022-03-08T15:13:52.160383Z", + "shell.execute_reply": "2022-03-08T15:13:52.159001Z", + "shell.execute_reply.started": "2022-03-08T15:13:51.654695Z" }, "tags": [] }, @@ -145,7 +163,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mBadRequestsError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'status%3D%22processed%22'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m response = SG.client.messages.get(\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mquery_params\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m )\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'ZGkrHSypTsudrGkmdpJJ'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mquery_params\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m )\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36mhttp_request\u001b[0;34m(request_body, query_params, request_headers, timeout, **_)\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 276\u001b[0m response = Response(\n\u001b[0;32m--> 277\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 278\u001b[0m )\n\u001b[1;32m 279\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, opener, request, timeout)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m body=exc.body))\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mBadRequestsError\u001b[0m: HTTP Error 400: Bad Request" @@ -153,16 +171,9 @@ } ], "source": [ - "import os\n", - "from sendgrid import SendGridAPIClient\n", - "\n", - "\n", - "SG = SendGridAPIClient(SENDGRID_API_KEY)\n", - "\n", - "params = {'query': 'status%3D%22processed%22',\n", - " 'limit': 10}\n", + "params = {'query': 'ZGkrHSypTsudrGkmdpJJ', 'limit': 10}\n", "\n", - "response = SG.client.messages.get(\n", + "response = sg.client.messages.get(\n", " query_params=params\n", ")\n", "\n", @@ -184,7 +195,7 @@ "id": "b54a2952-c67e-418b-b491-40162fb75477", "metadata": {}, "source": [ - "### Send the mail" + "### Display result" ] }, { From f855685add063ec03ef203139dad60138ec1d0ce Mon Sep 17 00:00:00 2001 From: SanjuEpic Date: Sun, 27 Mar 2022 11:51:22 +0200 Subject: [PATCH 5/7] fix: work on error --- SendGrid/SendGrid_Get_activity_feed.ipynb | 61 ++++++++++++----------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/SendGrid/SendGrid_Get_activity_feed.ipynb b/SendGrid/SendGrid_Get_activity_feed.ipynb index ec6cdada6f..0d82dc09c2 100644 --- a/SendGrid/SendGrid_Get_activity_feed.ipynb +++ b/SendGrid/SendGrid_Get_activity_feed.ipynb @@ -58,15 +58,15 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:10:35.859216Z", - "iopub.status.busy": "2022-03-08T15:10:35.858973Z", - "iopub.status.idle": "2022-03-08T15:10:35.862634Z", - "shell.execute_reply": "2022-03-08T15:10:35.862010Z", - "shell.execute_reply.started": "2022-03-08T15:10:35.859181Z" + "iopub.execute_input": "2022-03-26T09:36:39.354406Z", + "iopub.status.busy": "2022-03-26T09:36:39.354081Z", + "iopub.status.idle": "2022-03-26T09:36:40.934945Z", + "shell.execute_reply": "2022-03-26T09:36:40.933959Z", + "shell.execute_reply.started": "2022-03-26T09:36:39.354320Z" }, "tags": [] }, @@ -88,34 +88,35 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 2, "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:10:36.365249Z", - "iopub.status.busy": "2022-03-08T15:10:36.364821Z", - "iopub.status.idle": "2022-03-08T15:10:36.489828Z", - "shell.execute_reply": "2022-03-08T15:10:36.489117Z", - "shell.execute_reply.started": "2022-03-08T15:10:36.365207Z" + "iopub.execute_input": "2022-03-26T09:36:56.489374Z", + "iopub.status.busy": "2022-03-26T09:36:56.489140Z", + "iopub.status.idle": "2022-03-26T09:36:56.612237Z", + "shell.execute_reply": "2022-03-26T09:36:56.611593Z", + "shell.execute_reply.started": "2022-03-26T09:36:56.489351Z" }, "tags": [] }, "outputs": [], "source": [ - "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")" + "# SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")\n", + "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API\")" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 3, "id": "8b52af7b-8eb1-4d85-a46c-364855736be3", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:11:10.155795Z", - "iopub.status.busy": "2022-03-08T15:11:10.155561Z", - "iopub.status.idle": "2022-03-08T15:11:10.158670Z", - "shell.execute_reply": "2022-03-08T15:11:10.158014Z", - "shell.execute_reply.started": "2022-03-08T15:11:10.155770Z" + "iopub.execute_input": "2022-03-26T09:37:22.989734Z", + "iopub.status.busy": "2022-03-26T09:37:22.989468Z", + "iopub.status.idle": "2022-03-26T09:37:22.993225Z", + "shell.execute_reply": "2022-03-26T09:37:22.992372Z", + "shell.execute_reply.started": "2022-03-26T09:37:22.989693Z" }, "tags": [] }, @@ -143,15 +144,15 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 9, "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", "metadata": { "execution": { - "iopub.execute_input": "2022-03-08T15:13:51.654719Z", - "iopub.status.busy": "2022-03-08T15:13:51.654491Z", - "iopub.status.idle": "2022-03-08T15:13:52.160383Z", - "shell.execute_reply": "2022-03-08T15:13:52.159001Z", - "shell.execute_reply.started": "2022-03-08T15:13:51.654695Z" + "iopub.execute_input": "2022-03-26T09:58:42.345535Z", + "iopub.status.busy": "2022-03-26T09:58:42.345232Z", + "iopub.status.idle": "2022-03-26T09:58:42.844320Z", + "shell.execute_reply": "2022-03-26T09:58:42.843288Z", + "shell.execute_reply.started": "2022-03-26T09:58:42.345499Z" }, "tags": [] }, @@ -163,7 +164,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mBadRequestsError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m'ZGkrHSypTsudrGkmdpJJ'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mquery_params\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m )\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"from_email%3D%22administration%40cashstory.com%22\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mquery_params\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m )\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36mhttp_request\u001b[0;34m(request_body, query_params, request_headers, timeout, **_)\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 276\u001b[0m response = Response(\n\u001b[0;32m--> 277\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 278\u001b[0m )\n\u001b[1;32m 279\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, opener, request, timeout)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m body=exc.body))\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mBadRequestsError\u001b[0m: HTTP Error 400: Bad Request" @@ -171,12 +172,16 @@ } ], "source": [ - "params = {'query': 'ZGkrHSypTsudrGkmdpJJ', 'limit': 10}\n", + "# params = {'query': 'ZGkrHSypTsudrGkmdpJJ', 'limit': 10}\n", + "\n", + "# notifications@naas.ai\n", + "params = {'query':\"from_email%3D%22administration%40cashstory.com%22\", 'limit':10}\n", "\n", "response = sg.client.messages.get(\n", - " query_params=params\n", + " query_params = params\n", ")\n", "\n", + "# administration@cashstory.com\n", "print(response.status_code)\n", "print(response.body)\n", "print(response.headers)" From 181dfcbf845fe822dcb693bc07bf235741044f94 Mon Sep 17 00:00:00 2001 From: SanjuEpic Date: Sun, 3 Apr 2022 06:42:48 +0200 Subject: [PATCH 6/7] new code error --- SendGrid/SendGrid_Get_activity_feed.ipynb | 80 ++++++++++++----------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/SendGrid/SendGrid_Get_activity_feed.ipynb b/SendGrid/SendGrid_Get_activity_feed.ipynb index 0d82dc09c2..e408d6539a 100644 --- a/SendGrid/SendGrid_Get_activity_feed.ipynb +++ b/SendGrid/SendGrid_Get_activity_feed.ipynb @@ -58,15 +58,15 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", "metadata": { "execution": { - "iopub.execute_input": "2022-03-26T09:36:39.354406Z", - "iopub.status.busy": "2022-03-26T09:36:39.354081Z", - "iopub.status.idle": "2022-03-26T09:36:40.934945Z", - "shell.execute_reply": "2022-03-26T09:36:40.933959Z", - "shell.execute_reply.started": "2022-03-26T09:36:39.354320Z" + "iopub.execute_input": "2022-03-28T08:02:05.332357Z", + "iopub.status.busy": "2022-03-28T08:02:05.332072Z", + "iopub.status.idle": "2022-03-28T08:02:07.016300Z", + "shell.execute_reply": "2022-03-28T08:02:07.015628Z", + "shell.execute_reply.started": "2022-03-28T08:02:05.332328Z" }, "tags": [] }, @@ -74,7 +74,8 @@ "source": [ "import os\n", "from sendgrid import SendGridAPIClient\n", - "import naas" + "import naas\n", + "from python_http_client import exceptions" ] }, { @@ -88,15 +89,15 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", "metadata": { "execution": { - "iopub.execute_input": "2022-03-26T09:36:56.489374Z", - "iopub.status.busy": "2022-03-26T09:36:56.489140Z", - "iopub.status.idle": "2022-03-26T09:36:56.612237Z", - "shell.execute_reply": "2022-03-26T09:36:56.611593Z", - "shell.execute_reply.started": "2022-03-26T09:36:56.489351Z" + "iopub.execute_input": "2022-03-28T08:02:07.017756Z", + "iopub.status.busy": "2022-03-28T08:02:07.017478Z", + "iopub.status.idle": "2022-03-28T08:02:07.130156Z", + "shell.execute_reply": "2022-03-28T08:02:07.129513Z", + "shell.execute_reply.started": "2022-03-28T08:02:07.017725Z" }, "tags": [] }, @@ -108,15 +109,15 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "8b52af7b-8eb1-4d85-a46c-364855736be3", "metadata": { "execution": { - "iopub.execute_input": "2022-03-26T09:37:22.989734Z", - "iopub.status.busy": "2022-03-26T09:37:22.989468Z", - "iopub.status.idle": "2022-03-26T09:37:22.993225Z", - "shell.execute_reply": "2022-03-26T09:37:22.992372Z", - "shell.execute_reply.started": "2022-03-26T09:37:22.989693Z" + "iopub.execute_input": "2022-03-28T08:02:07.132951Z", + "iopub.status.busy": "2022-03-28T08:02:07.132742Z", + "iopub.status.idle": "2022-03-28T08:02:07.137043Z", + "shell.execute_reply": "2022-03-28T08:02:07.136513Z", + "shell.execute_reply.started": "2022-03-28T08:02:07.132931Z" }, "tags": [] }, @@ -144,30 +145,30 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", "metadata": { "execution": { - "iopub.execute_input": "2022-03-26T09:58:42.345535Z", - "iopub.status.busy": "2022-03-26T09:58:42.345232Z", - "iopub.status.idle": "2022-03-26T09:58:42.844320Z", - "shell.execute_reply": "2022-03-26T09:58:42.843288Z", - "shell.execute_reply.started": "2022-03-26T09:58:42.345499Z" + "iopub.execute_input": "2022-03-28T08:02:07.653313Z", + "iopub.status.busy": "2022-03-28T08:02:07.653085Z", + "iopub.status.idle": "2022-03-28T08:02:58.273592Z", + "shell.execute_reply": "2022-03-28T08:02:58.272220Z", + "shell.execute_reply.started": "2022-03-28T08:02:07.653292Z" }, "tags": [] }, "outputs": [ { - "ename": "BadRequestsError", - "evalue": "HTTP Error 400: Bad Request", + "ename": "InternalServerError", + "evalue": "HTTP Error 500: Internal Server Error", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mBadRequestsError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'query'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"from_email%3D%22administration%40cashstory.com%22\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'limit'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mquery_params\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m )\n", + "\u001b[0;31mInternalServerError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;31m# print(response.headers)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 16\u001b[0m query_params = params)\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36mhttp_request\u001b[0;34m(request_body, query_params, request_headers, timeout, **_)\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 276\u001b[0m response = Response(\n\u001b[0;32m--> 277\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 278\u001b[0m )\n\u001b[1;32m 279\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, opener, request, timeout)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m body=exc.body))\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mBadRequestsError\u001b[0m: HTTP Error 400: Bad Request" + "\u001b[0;31mInternalServerError\u001b[0m: HTTP Error 500: Internal Server Error" ] } ], @@ -175,16 +176,19 @@ "# params = {'query': 'ZGkrHSypTsudrGkmdpJJ', 'limit': 10}\n", "\n", "# notifications@naas.ai\n", - "params = {'query':\"from_email%3D%22administration%40cashstory.com%22\", 'limit':10}\n", "\n", - "response = sg.client.messages.get(\n", - " query_params = params\n", - ")\n", + "# params = {'query':\"from_email%3D%22administration%40cashstory.com%22\", 'limit':10}\n", + "params = {'query':'from_email=\"administration@cashstory.com\"', \"limit\":10}\n", "\n", - "# administration@cashstory.com\n", - "print(response.status_code)\n", - "print(response.body)\n", - "print(response.headers)" + "try:\n", + " response = sg.client.messages.get(\n", + " query_params = params)\n", + " print(response.status_code)\n", + " print(response.body)\n", + " print(response.headers)\n", + "except exceptions.BadRequestsError as e:\n", + " print(e.body)\n", + " exit()" ] }, { From fff606b122a84b669101fa1bdf732de4ffddca84 Mon Sep 17 00:00:00 2001 From: FlorentLvr Date: Mon, 4 Apr 2022 13:15:36 +0200 Subject: [PATCH 7/7] feat: get messages from sendgrid --- SendGrid/SendGrid_Get_activity_feed.ipynb | 240 ---------------------- SendGrid/SendGrid_Get_all_messages.ipynb | 206 +++++++++++++++++++ 2 files changed, 206 insertions(+), 240 deletions(-) delete mode 100644 SendGrid/SendGrid_Get_activity_feed.ipynb create mode 100644 SendGrid/SendGrid_Get_all_messages.ipynb diff --git a/SendGrid/SendGrid_Get_activity_feed.ipynb b/SendGrid/SendGrid_Get_activity_feed.ipynb deleted file mode 100644 index e408d6539a..0000000000 --- a/SendGrid/SendGrid_Get_activity_feed.ipynb +++ /dev/null @@ -1,240 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", - "metadata": {}, - "source": [ - "\"Naas\"" - ] - }, - { - "cell_type": "markdown", - "id": "2ba2f665-d276-4881-81f8-c9524f75796b", - "metadata": {}, - "source": [ - "# Sendgrid - Get activity feed" - ] - }, - { - "cell_type": "markdown", - "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", - "metadata": {}, - "source": [ - "**Tags:** #sendgrid #activity #snippet" - ] - }, - { - "cell_type": "markdown", - "id": "b025116f-fd57-40ae-81db-99d9dbc30445", - "metadata": {}, - "source": [ - "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" - ] - }, - { - "cell_type": "markdown", - "id": "c11af9c0-6407-4dec-aba3-ed8cb5ea011d", - "metadata": {}, - "source": [ - "This notebook enables you to get a dataframe of all the activities performed" - ] - }, - { - "cell_type": "markdown", - "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", - "metadata": {}, - "source": [ - "## Input" - ] - }, - { - "cell_type": "markdown", - "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", - "metadata": {}, - "source": [ - "### Imports" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", - "metadata": { - "execution": { - "iopub.execute_input": "2022-03-28T08:02:05.332357Z", - "iopub.status.busy": "2022-03-28T08:02:05.332072Z", - "iopub.status.idle": "2022-03-28T08:02:07.016300Z", - "shell.execute_reply": "2022-03-28T08:02:07.015628Z", - "shell.execute_reply.started": "2022-03-28T08:02:05.332328Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "import os\n", - "from sendgrid import SendGridAPIClient\n", - "import naas\n", - "from python_http_client import exceptions" - ] - }, - { - "cell_type": "markdown", - "id": "392e708f-7c32-45d7-8937-382c9651fc0f", - "metadata": {}, - "source": [ - "### Setup SendGrid\n", - "👉 Get your [api key](https://app.sendgrid.com/settings/api_keys)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", - "metadata": { - "execution": { - "iopub.execute_input": "2022-03-28T08:02:07.017756Z", - "iopub.status.busy": "2022-03-28T08:02:07.017478Z", - "iopub.status.idle": "2022-03-28T08:02:07.130156Z", - "shell.execute_reply": "2022-03-28T08:02:07.129513Z", - "shell.execute_reply.started": "2022-03-28T08:02:07.017725Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "# SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API_KEY\")\n", - "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API\")" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "8b52af7b-8eb1-4d85-a46c-364855736be3", - "metadata": { - "execution": { - "iopub.execute_input": "2022-03-28T08:02:07.132951Z", - "iopub.status.busy": "2022-03-28T08:02:07.132742Z", - "iopub.status.idle": "2022-03-28T08:02:07.137043Z", - "shell.execute_reply": "2022-03-28T08:02:07.136513Z", - "shell.execute_reply.started": "2022-03-28T08:02:07.132931Z" - }, - "tags": [] - }, - "outputs": [], - "source": [ - "sg = SendGridAPIClient(SENDGRID_API_KEY)" - ] - }, - { - "cell_type": "markdown", - "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", - "metadata": {}, - "source": [ - "## Model" - ] - }, - { - "cell_type": "markdown", - "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", - "metadata": {}, - "source": [ - "### Get activity\n", - "https://docs.sendgrid.com/api-reference/e-mail-activity/filter-all-messages?code-sample=code-filter-all-messages&code-language=Python&code-sdk-version=6.x" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "37b21763-5432-4dda-bbe9-ffe8c960401a", - "metadata": { - "execution": { - "iopub.execute_input": "2022-03-28T08:02:07.653313Z", - "iopub.status.busy": "2022-03-28T08:02:07.653085Z", - "iopub.status.idle": "2022-03-28T08:02:58.273592Z", - "shell.execute_reply": "2022-03-28T08:02:58.272220Z", - "shell.execute_reply.started": "2022-03-28T08:02:07.653292Z" - }, - "tags": [] - }, - "outputs": [ - { - "ename": "InternalServerError", - "evalue": "HTTP Error 500: Internal Server Error", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mInternalServerError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;31m# print(response.headers)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m response = sg.client.messages.get(\n\u001b[0m\u001b[1;32m 16\u001b[0m query_params = params)\n\u001b[1;32m 17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36mhttp_request\u001b[0;34m(request_body, query_params, request_headers, timeout, **_)\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 276\u001b[0m response = Response(\n\u001b[0;32m--> 277\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_make_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mopener\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 278\u001b[0m )\n\u001b[1;32m 279\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/opt/conda/lib/python3.9/site-packages/python_http_client/client.py\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, opener, request, timeout)\u001b[0m\n\u001b[1;32m 182\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mexc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 183\u001b[0m body=exc.body))\n\u001b[0;32m--> 184\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mexc\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 185\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 186\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mInternalServerError\u001b[0m: HTTP Error 500: Internal Server Error" - ] - } - ], - "source": [ - "# params = {'query': 'ZGkrHSypTsudrGkmdpJJ', 'limit': 10}\n", - "\n", - "# notifications@naas.ai\n", - "\n", - "# params = {'query':\"from_email%3D%22administration%40cashstory.com%22\", 'limit':10}\n", - "params = {'query':'from_email=\"administration@cashstory.com\"', \"limit\":10}\n", - "\n", - "try:\n", - " response = sg.client.messages.get(\n", - " query_params = params)\n", - " print(response.status_code)\n", - " print(response.body)\n", - " print(response.headers)\n", - "except exceptions.BadRequestsError as e:\n", - " print(e.body)\n", - " exit()" - ] - }, - { - "cell_type": "markdown", - "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", - "metadata": {}, - "source": [ - "## Output" - ] - }, - { - "cell_type": "markdown", - "id": "b54a2952-c67e-418b-b491-40162fb75477", - "metadata": {}, - "source": [ - "### Display result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f9d8bcd0-0502-42b2-8379-eed7b37a7216", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.6" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/SendGrid/SendGrid_Get_all_messages.ipynb b/SendGrid/SendGrid_Get_all_messages.ipynb new file mode 100644 index 0000000000..a679cbdab2 --- /dev/null +++ b/SendGrid/SendGrid_Get_all_messages.ipynb @@ -0,0 +1,206 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "85db721e-2267-40ca-ac5b-00c08ec10ff0", + "metadata": {}, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "2ba2f665-d276-4881-81f8-c9524f75796b", + "metadata": {}, + "source": [ + "# Sendgrid - Get all messages" + ] + }, + { + "cell_type": "markdown", + "id": "b55448ec-bd35-4884-b72f-a06a3f12c914", + "metadata": {}, + "source": [ + "**Tags:** #sendgrid #activity #snippet #operations #dataframe" + ] + }, + { + "cell_type": "markdown", + "id": "b025116f-fd57-40ae-81db-99d9dbc30445", + "metadata": {}, + "source": [ + "**Author:** [Sanjeet Attili](https://linkedin.com/in/sanjeet-attili-760bab190/)" + ] + }, + { + "cell_type": "markdown", + "id": "c11af9c0-6407-4dec-aba3-ed8cb5ea011d", + "metadata": {}, + "source": [ + "This notebook enables you to get a dataframe of all the activities performed" + ] + }, + { + "cell_type": "markdown", + "id": "ec2132f6-bf89-4236-8ac7-0c207f8f03b1", + "metadata": {}, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "81f26c9b-0c3c-4cf8-ad50-dc67e6329ba1", + "metadata": {}, + "source": [ + "### Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26c428b4-66f4-48a4-82db-ff24cdec1782", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import naas\n", + "import requests\n", + "import urllib\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "id": "392e708f-7c32-45d7-8937-382c9651fc0f", + "metadata": {}, + "source": [ + "### Setup SendGrid\n", + "👉 Get your [api key](https://app.sendgrid.com/settings/api_keys)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2de7b1bc-5ec9-4fa7-b89f-3f72f1b9a5e0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "SENDGRID_API_KEY = naas.secret.get(\"SENDGRID_API\")" + ] + }, + { + "cell_type": "markdown", + "id": "a180347f-a2e1-44b2-bce0-775c7ac63b04", + "metadata": {}, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "61983024-d97c-46d5-b27c-b7d5c7af5869", + "metadata": {}, + "source": [ + "### Get activity\n", + "https://docs.sendgrid.com/api-reference/e-mail-activity/filter-all-messages?code-sample=code-filter-all-messages&code-language=Python&code-sdk-version=6.x" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b5d42846-8db8-4bba-b6c6-d37af7b1c138", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def get_messages(msg_id=None,\n", + " from_email=None,\n", + " subject=None,\n", + " to_email=None,\n", + " status=None,\n", + " clicks=None,\n", + " limit=1000):\n", + " \n", + " kargs = locals()\n", + " params = {}\n", + " for k in kargs:\n", + " v = kargs.get(k)\n", + " if v is not None:\n", + " params[k] = v\n", + " req_url = f\"https://api.sendgrid.com/v3/messages?{urllib.parse.urlencode(params)}\"\n", + " headers = {\n", + " \"Authorization\": f\"Bearer {SENDGRID_API_KEY}\",\n", + " \"Content-Type\": \"application/json\"\n", + " }\n", + " res = requests.get(req_url,\n", + " headers=headers)\n", + " try:\n", + " res.raise_for_status()\n", + " except requests.HTTPError as e:\n", + " raise(e)\n", + " res_json = res.json()\n", + " messages = res_json.get(\"messages\")\n", + " \n", + " # Formatting\n", + " df = pd.DataFrame(messages)\n", + " df[\"last_event_time\"] = df[\"last_event_time\"].astype(str).str.replace(\"T\", \" \").str.replace(\"Z\", \"\")\n", + " df.columns = df.columns.str.upper()\n", + " return df\n", + "\n", + "df_messages = get_messages(limit=1000)" + ] + }, + { + "cell_type": "markdown", + "id": "6109e162-ea7e-4db3-bb71-41c5e1403ea1", + "metadata": {}, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "b54a2952-c67e-418b-b491-40162fb75477", + "metadata": {}, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7a3247b7-a627-41b0-89e9-2b7c39b0be47", + "metadata": {}, + "outputs": [], + "source": [ + "df_messages" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}