This article desribes how to integrate notion with telegram. I needed to send a daily notification with all my overdue contacts from my notion CRM table. The article assumes you understand the basics of how to manage infrastructure with CDK. The article focuses on the telgram / notion specifics.
I have a Notion database with people I stay in touch. Inside the table I have a field
Last Contacted and
Cadence days. The formula in
Status checks if the today is later than
last contect + Cadence days. In cases that today is later it contains "Time to reach out!".
if(dateBetween(now(), prop("Last Contacted"), "days") > prop("Cadence days"), "Time to reach out!", "👍")
Everday at 8 I want to recieve a list of all overdue contacts to reach out in Telegram. The solution be cheap.
The solution is a serverless cron job that triggers a lambda fuction that get the overdue contacts through the notion api and send the notifications to the telegram bot and the bot then sends this to me.
Follow manage infrastructure up to "Implement Code and Use Level 2/3 Contructs (Add an AWS Lambda Function to Your Stack)" to:
- init repo,
- create cdk template,
- authenticate and bootstrap
Implement the Cron Job
Next remove the sample cdk resources and add the cronjob based on AWS Eventbridge:
Add Integration Lambda Function
Next add an Lambda function that contains the integration, including stubs for the function to get the
get_contacts_overdue (from Notion) and
send_message (to Telegram). Trigger this lambda function from the cron job. First install a new new dependency with
pip install aws_cdk.aws_lambda_python_alpha && pip freeze > requirements.txt
Then Get the Overdue Contacts From the Notion Api (registration, Code, query)
Next register and api client ("integration") with notion and write down the api key
Then give the app permission to access the table: Find the table id
pip install pytest notion-client && pip freeze > requirements.txt
Then Send the Overdue Contacts to Telegram (registration, Code to Build requests)
Register a bot, as a result you get a botKey, find out the userId you want to send to and then create an REST request to send a message to the bot that is then send to the user.2 Then you implement this into the integration lambda function. After the function collected the overdue contacts with names and links, create a text to send to the telegrambot. In that request add the botKey and the userId and telegram then sends the message to the user.
Replace Environment Variables with AWS Systems Manager Paramter Store
Replace the environment variables with paramters from AWS SSM. SSM has the benefit you can encrypt. To access SSM you need to boto3:
pip install boto3 && pip freeze > requirements.txt
Then you manually create the paramters in SSM (currently you can create encrypted SSM paramter with CDK): See manage infrastructure (Store Credentials Securly Using SSM).