Sent Gmail On New Item
Tags: #notion #gsheet #productivity #naas_drivers
Author: Arun K C

Input

Import librairies

Let's import all the necessary libraries required
1
import naas
2
from naas_drivers import notion, gsheet
3
from naas_drivers import html
4
import pandas as pd
Copied!

Variables

Replace all the variables below with appropriate values.
1
# Notion
2
token = "NOTION_TOKEN"
3
database_id = "NOTION_DATABASE_ID"
4
5
# Gsheet
6
spreadsheet_id = "SPREADSHEET_ID"
7
mail_list_sheet_name = "Sheet1"
8
item_list_sheet_name = "Sheet2"
9
your_email = "YOUR_EMAIL_ID"
Copied!

Read the gsheet

1
email_list_data = gsheet.connect(spreadsheet_id).get(sheet_name = mail_list_sheet_name)
2
try:
3
item_list_history = gsheet.connect(spreadsheet_id).get(sheet_name = item_list_sheet_name)
4
except:
5
item_list_history = []
Copied!

Setting up email

1
firstname_list = email_list_data['FIRSTNAME']
2
email_list = email_list_data['EMAIL']
Copied!

Get database from notion

1
def create_notion_connection():
2
database = notion.connect(token).database.get(database_id)
3
df_db = database.df()
4
print(df_db)
5
return df_db
Copied!

Model

Send data to Gsheet

1
#Send data to Gsheet
2
def send_data_to_gsheet(data):
3
gsheet.connect(spreadsheet_id)
4
gsheet.send(
5
sheet_name = item_list_sheet_name,
6
data = data
7
)
Copied!

Get new items from Notion

Let's fetch out the new items from Notion
Here our unique key is Id
1
#Get new notion items list
2
def get_new_items_list(df_db):
3
4
if not list(item_list_history):
5
new_items = df_db
6
else:
7
item_list_history['Id'] = item_list_history['Id'].astype(int)
8
df_db['Id'] = df_db['Id'].astype(int)
9
10
common = df_db.merge(item_list_history, on=["Id"])
11
new_items = df_db[~df_db.Id.isin(common.Id)]
12
13
data = []
14
15
for i in range(len(new_items.index)):
16
dictionary = {}
17
for col in new_items.columns:
18
dictionary[col] = str(new_items.iloc[i][col])
19
data.append(dictionary)
20
21
send_data_to_gsheet(data)
22
23
return data
24
Copied!

Create email content

1
#Get email contents
2
def get_mail_content():
3
email_content = html.generate(
4
display = 'iframe',
5
title = 'Updates here!!',
6
heading = 'Hi {first_name}, you have some new items in you notion list',
7
text_1 = 'Following are the new list of items seperated by comma : ',
8
text_2 = '{new_items_list}',
9
text_3 = 'Have a great day!!'
10
)
11
#print(email_content)
12
return email_content
Copied!

Sending Emails

1
#Send mail to recipients
2
def send_mail(new_items_list):
3
email_content = get_mail_content()
4
for i in range(len(email_list_data)):
5
subject = "Update on Notion items"
6
content = email_content.replace("{first_name}",firstname_list[i]).replace("{new_items_list}",new_items_list)
7
naas.notifications.send(email_to=email_list[i], subject=subject, html=content, email_from=your_email)
Copied!

Output

1
df = create_notion_connection()
Copied!
1
new_items_list = get_new_items_list(df)
2
new_items_list = ', '.join([data['Books'] for data in new_items_list])
Copied!
1
if new_items_list:
2
send_mail(new_items_list)
3
else:
4
print('No new items!!')
Copied!

Setting up the scheduler

Let's schedule the notebook for every 15mins ⏰
Ps: to remove the "Scheduler", just replace .add by .delete
1
#Schedule the notebook to run every 15 minutes
2
naas.scheduler.add(cron="*/15 * * * *")
Copied!
Copy link
Edit on GitHub