Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Sendgrid - activity feed #355

Merged
merged 7 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions SendGrid/SendGrid_Get_all_messages.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "85db721e-2267-40ca-ac5b-00c08ec10ff0",
"metadata": {},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"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
}
Loading