Getting started
To follow along with this tutorial you’ll need:
- A 46elks account.
- A Notion account.
- A domain and an online server where to upload your script.
For development purposes you could set up a temporary ngrok link to your local machine. However, for a production environment you are highly recommended to use an online server.
For this tutorial you are assumed to have a basic understanding of how to use Notion since we are not going to cover the fundamentals. If you are new to Notion please check out their guides and tutorials.
What we want to achieve
We want to create a page and store a list of users with names and phone numbers. We also want to store a message to send to our users.
To send the message we'll create a link that triggers a script. The script will then get all the information we added in Notion (via the Notion API) and then send an SMS to each user on the list.
Here's an example of how the page could look like in Notion:
Enable the Notion API
First things first. To query the Notion API you must first create a so called integration. You could think of it as a special type of user who has a special set of permissions.
To create an integration you must have admin access to your account. If you use a personal account you’ll have admin access by default. If not, you might have to ask for admin access by the owner of the account.
Go ahead and follow Notion's guide on how to create an integration. Do step 1 in the guide and save your ”Internal Integration Token”, you'll need it later. When you’re done, come back here.
Here's an example of how your integration might look like:
Set up your page in Notion
Now we are going to create our page and add some data to it. Please do the following steps:
- Create a new page and give it a title.
- Create a new text paragraph and write the message that you want to send to your users.
Please note that you must specifically create a paragraph. Not a callout or a quote. This is because the Notion API can't query those elements as of right now (API version 2021-08-16). This might change in the future.
Also note that if you want to use line breaks in your paragraph, please click
SHIFT + ENTER
to keep the text within the same block. Otherwise you will create separate blocks which will cause issues further ahead. - Create a new inline database.
- Add two properties called “Name” and "Phone number" to the database.
The property names are case sensitive. It means you must write it exactly as it says above. If you for instance write it in in lower case like "name" and "phone number" the script we are going to use won't work.
If you really want to name the fields something else, you must also change it in the script later on in this tutorial.
- Add a few entries to the database and you should now have something looking like this:
When you are adding the phone numbers, please ensure they are formatted according to E.164.
- Share your page with your integration as you would any other user. This allows your integration to access data within your page and database. If you omit this step you won’t be able to query the Notion API.
Set up your script
-
Get the script and save it into a file called
send-sms-notion.php
-
Set the following variables inside the file:
$notion_api_key
is your "Internal Integration Token" you got when you created your integration.$elks_api_user
and$elks_api_pass
is your 46elks API credentials.$from
could be either a phone number or a name.
-
Upload the file to your server. It should be available under
your-domain.com/send-sms-notion.php
Test your script
The last step is to add a link to your Notion page which will trigger the script.
-
To make the link work, we will need two ID's; One for the message paragraph and one for the database. To get these ID's you can simply copy/paste the share link for each element and retrieve the ID directly from the URL like so:
The URL structure is as follows:
- Database:
/{database_id}?v={view_id}
- Block:
/{page_name}#{block_id}
You are going to copy the value for
{database_id}
and{block_id}
in your own URLs.Example
If your database URL is
https://notion.so/298f3ju21a60442c967fe4c3f3e52402?v=6wm203e77ab324e38h817b44b84eb1e8
then your database id is298f3ju21a60442c967fe4c3f3e52402
.If your block URL is
https://notion.so/Hackathon-abff640fb8274928abc9110527a07fa6#5829ea358di64b3884adef95lk4wuf28
, then your block id is5829ea358di64b3884adef95lk4wuf28
. - Database:
-
Create a link, name it “Send SMS” and assign it the following URL:
https://your-domain.com/send-sms-notion.php?database_id={database_id}&message_id={block_id}
- Change
https://your-domain.com
to your own domain. - Change
{database_id}
to your database id. - Change
{block_id}
to your block id.
- Change
Now to the moment of truth: Click on your link. If everything was successful you should see a message in the new browser tab saying that an SMS was successfully sent to all users.
Troubleshoot
If you get the 404 error "object_not_found" while running your script, you’ve probably entered the wrong ID or forgot to share your page with your integration.
Here’s a list of all error codes from the Notion API.
Now what?
If you have multiple databases with the same structure, you can trigger the same script by only having to change database_id
and block_id
in your "send SMS" link.
If you want to target specific users from your list you can use filters and this is only the tip of the iceberg.
Feel free to modify the script as you wish and please let us know what you are building. We love talking to our users!
Do you need help?
Please contact our support. We are happy to help!