Send Slack Messages For New Notion Database Items
Tags: #notion #slack #operations #automation
This notebook sends a Slack message every-time it sees a new notion page added to a database.
References :
- For this use case we need to create & use user token, rather than bot token with the following permissions/scopes -> [channels: history, channels: read, chat: write, users: read]
from naas_drivers import notion, slack
import naas
- Share integration with your database
# Enter Token API
NOTION_TOKEN = "secret_J9JIQksrylGmJpErmw49A7U9ON1lIdGLjbVk6tDFh2y"
# Enter Database id
DATABASE_ID = "https://www.notion.so/naas-official/72c87516d6e1419fb3a69763892898c7?v=2e71afc61e7644409dd874957c98e78e"
# Token
SLACK_TOKEN = "xoxb-xxx-xxx-xxx"
# Channel name
SLACK_CHANNEL = "channel-name"
# Schedule your notebook every 15min
naas.scheduler.add(cron="*/15 * * * *")
# -> Uncomment the line below to remove your scheduler
# naas.scheduler.delete()
pages = notion.connect(NOTION_TOKEN).database.query(DATABASE_ID, query={})
def send_message():
for page in pages:
# For the first time ever if there is no property/column named 'slack notification sent' in database
if "Slack notification sent" not in page.properties.keys():
page_name = page.properties["Name"]
page_url = page.url
slack.connect(SLACK_TOKEN).send(
SLACK_CHANNEL, f'New notion page created "{page_name}" here: {page_url}'
)
page.select("Slack notification sent", "True")
page.update()
print(f"✅ Notification sent for {page_name}: {page_url}")
# If there is a column present then checks if it is False or None and updates
else:
if str(page.properties["Slack notification sent"]) != "True":
page_name = page.properties["Name"]
page_url = page.url
slack.connect(SLACK_TOKEN).send(
SLACK_CHANNEL,
f'New notion page created "{page_name}" here: {page_url}',
)
page.select("Slack notification sent", "True")
page.update()
print(f"✅ Notification sent for {page_name}: {page_url}")
send_message()
Last modified 1mo ago