Check out a free preview of the full Building a Slack Chat Bot course

The "Getting Items from Notion" Lesson is part of the full, Building a Slack Chat Bot course featured in this preview video. Here's what you'd learn in this lesson:

Jason creates a helper method to retrieve a list of items from the Notion database. It filters based on the status of "new" so items already in progress or completed are not returned.

Preview
Close

Transcript from the "Getting Items from Notion" Lesson

[00:00:00]
>> So next let's create a function that's gonna let us get the new item. So anything in our database that has it, it's in the the new column the to do items. So we can check how that's gonna work. It's gonna return a promise. And that promise is going to have a new item in it.

[00:00:19]
And that's gonna be an array. And that, just to show what we're actually trying to do here, if we close this one and open up this one, we are looking at this column. We want to run a query that will only give us back these fields. We don't care about stuff that's in progress because that means it's been assigned to somebody or is otherwise in motion and we don't care about stuff is complete.

[00:00:45]
We only care about things that need action. So things in the to do column are the things that need someone to actually triage them, look through them, make sure that they're valid, so on and so forth. So then we can go into our code again and let's actually write out that filter.

[00:01:06]
So for this one, we are going to use notion data that is going to be an await notion Api. And we're going to send databases and then our database ID NOTION_DATABASE-ID and query. So we're running a query against the database. And this looks a little more like the Slack API has those chat.postMessage, which makes it look very fancy.

[00:01:44]
But that is just the URL. Whereas with these notion ones, they look a little more familiar URL endpoints. Then we're gonna pass in our filter. And so this is, one of a few things you can also pass in salt and a few other pieces, but what we care about is specifically the filter, because that's gonna allow us to limit down what we're getting to just the stuff we need.

[00:02:06]
So we're gonna go with property status. And that is aligned to these properties here. So when you're looking, you can see the status spice level submitter. And the reason that these are written like they are, not with spaces and stuff is because knowshon doesn't have a differentiation between what displays on the screen and how you run these filters.

[00:02:30]
So if you want something like spice level in your code then you would also set spice level here otherwise you're gonna have like quoted string keys with spaces in them and stuff it's your call on how you wanna manage that. It doesn't really matter one way or the other.

[00:02:46]
It's just a preference thing. So inside here, we're gonna get the filter property status. I added one too many. We've got the property status. We want the status equals new so we're getting only things that are in the new column. And that finishes our filter and then we want the page size to be 100 and this is unhappy because expected two arguments but got three.

[00:03:22]
Because I double quoted this, there we go. That's better. So now we've got an object, it's got our filter, and then that says that we only want items with the new status, and we'll get up to 100 items. You can specify this for however many you want. Like if you have a lot of incoming issues, maybe you only wanna show the most recent 10, and have a link to view the rest or something like that.

[00:03:46]
Then what we're gonna do is we are going to grab out our open items and format them, By getting the results and we're gonna map over those. That'll be an item and that item is a notion item, which we have a type 4. And we're gonna return the opinion, and that's going to be item.properties.opinion.title.

[00:04:13]
Then you get to destructure that because it's an array. Then you get your text, and then you get the content. The Notion Api is similar to Slack's block kit. It's very verbose and it's done that for a good reason. It allows you to pass a lot of information about the content and it's formatting and a bunch of other details.

[00:04:34]
But it does mean that these accessors here get a little intense. And what you would probably wanna do is in a more mission-critical application. Maybe look into some kind of runtime validation of the input that's coming in like Zod to make sure that you don't, as you're drilling into these items, if something is misplaced, if somebody updates the notion database without telling you there are a lot of things that could get weird here.

[00:05:01]
But in our case, we have control over it. We know how it's working and so we can kinda just dive in and be happy. So I'm gonna get the spice level as well. That spice level is item.properties.spiceLevel.select.name, and then our status Is item.properties.status, capital.status.name. And then we can return our open items.

[00:05:40]
Okay, so that is one helper piece ready to go we're happy about that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now