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

The "Saving Items to 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 save items to the Notion database. The saveItem method will get called any time a new food fight message is submitted from the modal in Slack.

Preview
Close

Transcript from the "Saving Items to Notion" Lesson

[00:00:00]
>> That'll let us get items, and we'll look at that in a minute. But what we're gonna do now is we're going to save items. So we're gonna create another helper called saveItem, and that is going to also be a NewItem. And this one is also gonna access the notionApi.

[00:00:21]
So we await notionApi. And this time, we're posting to pages. So we wanna set the parent. That parent is going to be the database_id because we want this to show up in our database. So process.env.NOTION_DATABASE_ID. And then what we're gonna pass in is properties. Properties, Will have the opinion.

[00:00:51]
That opinion is going to include a title. That title is an array that has an object in it, that has text. And then it has another object that is content, and that's the item.opinion. Then we need, The spiceLevel. Like I said, these objects get a little big, but that's okay.

[00:01:22]
Let me do a select, and then we're gonna set the name, and that is item.spiceLevel. And finally, we want the submitter. And that submitter is gonna be rich_text. And that is very similar to the way the title works. You pass in an array, and then you pass in objects that have content.

[00:01:46]
And this one, we're gonna do a little bit of formatting. So we're gonna do an @ and item.submitter, On Slack just to kinda link back to who sent this in so that the people triaging can go and set it up. And with that set, we're gonna say, if anything didn't work with that call, then we're going to console.log it.

[00:02:17]
But otherwise, there's not really anything for us to do, we just wanna save that. To actually use this, we can head into our top level function, and we're going to edit the foodfight-modal. And we've already got our details here, and these match that NewItem type that we created.

[00:02:38]
So all we have to do to make this work is await savItem and pass in our fields. In here, see how I wrapped this inside of opinion instead of making it its own thing. So let me go back up here and just fix that typo. So now we have top-level properties.

[00:03:04]
We've got properties, opinion, spiceLevel, and submitter instead of nesting them in that weird way that I did. So we can save that. And we'll come back out here and try this one more time. And we'll say, Pop Rocks make any dessert better. Okay, medium. There we go, and it showed up somewhere here.

[00:03:40]
Here it is. So now we've got a communication directly from Slack into Notion, because whenever we run this command, we're able to gather the details out of the modal. Package them up in a way that Notion understands, and then send it through to Notion so that the team can come and look and see that there are things that need discussion, right?

[00:04:01]
This is great, this is exactly what we want. And if we need more details, we can also see that my user is the user that submitted this particular opinion. So if there's any follow-up that's necessary, like if somebody wants to clarify what flavor of Pop Rocks, they know to find me, and it's not just kind of a mystery as to where this came from.

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