Check out a free preview of the full Building a Slack Chat Bot course
The "Creating a Reminder" 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 cron job to run at a set interval and posts a list of unaddressed items to the slack channel.
Transcript from the "Creating a Reminder" Lesson
[00:00:00]
>> Let's set up a triage call. We're gonna have this thing send us a reminder once every, we'll say, week to post the open items. And to make that work, we're gonna create a new function. So I'm gonna open up code. And in here under my source, I'm gonna create a new function called reminder.ts.
[00:00:31]
And inside reminder, we're going to set up a new handler that is a type though, so we can make that a type. And then I also want schedule, which is the way that Netlify functions handle cron jobs. I'm also going to import get new items from the notion, and I'm gonna import blocks, and the Slack API helper from the Slack utils.
[00:01:06]
Then we're gonna create a new function called postnewNotionItemsToSlack. Nice descriptive function name there. And that's gonna be an async function that is going to grab the items by await getNewItems(). So now we've got the items from Notion in this items variable and we can send them off to Slack.
[00:01:32]
So we're gonna await Slack API and use the chat.postMessage, send it to our general channel, which I'm going to grab out of here. So click on the channel itself, go down to the bottom, grab that channel. We could have put that into the environment. No real reason that I didn't other than I didn't think about it until just now.
[00:01:58]
Then we're gonna set up blocks. And for these, we're gonna use blocks.section and send in some text. And our text is going to be, so I wanna make this kind of a multi-line piece of text. And this is a trick that I learned to avoid having a lot of line break characters and stuff, is you can make an array and then join it on a new line.
[00:02:20]
So I'm going to say, here are the opinions awaiting judgment. Then we wanna set up just an empty line. And then we're going to items.map to get our list of items for display. So we'll get the item. And for each of these, we're going to do a dash to make a list, and we'll get the item.opinion and then add the spice level.
[00:02:55]
Spice level is going to be item.spicelevel, Okay, so you'll see we spread that here, and that means that each one of these is now a line in this setup. And I'm gonna add another empty line and we're gonna say, see all items and add a link. So we'll say https.notion, or https://notion.com/.
[00:03:24]
Then we're gonna put in that database ID. Because, well, Notion usually shows you the space name in the URL. You can just drop in a database ID and it will work. And then this is another kind of interesting Slack thing. So we're opening up these pointy boys to do a link, but I want it to be text.
[00:03:50]
So I can just add a pipe and then the text I want it to be, and Notion will turn that into link text. Again, that's, I don't know why Notion does things, or sorry, why Slack does things like that, but that was a choice they made, and we must abide by it.
[00:04:10]
So then we can join the whole thing using a newline character. And that way what we get is a multi-paragraph chunk of text that gets sent to the browser or into the the Slack channel. So we can save that. And then underneath it, we're going to return statusCode of 200.
[00:04:29]
Now cron jobs don't need any body, so you don't have to send one back. And to actually set up the cron part of this, we're gonna export a const called handler, and then we imported that schedule function. So the first argument here is gonna be the cron, and then the second piece is gonna be the function that gets called.
[00:04:51]
So I'm gonna use, I could just guess at what one of these is, but instead, I'm gonna use a really cool tool called crontab.guru. And what this does is, it lets you kind of do this a little more human readably. So if you come in here and you say, we'll just set this to be like this, every minute it would run our cron job.
[00:05:17]
So then you can say every 15 minutes or at minute 0, so hourly, right? And then we could also say, run it at noon, which is what we want, but then we also want it to be run on a day. So we want this to be, why don't we run it a day of the week instead, right?
[00:05:39]
There we go. So every Monday at noon we want this to run. So maybe ours is gonna be 9. Now you gotta play with this a little bit because you gotta figure out what the offset is between the deployed server and your location, like your team's location. Because they don't give you a timezone offset, you just kinda have to figure out how this works.
[00:06:00]
But this seems good. So every Monday at 9AM, we're gonna run this, and you'll see how it works. So then when we come in here and set this, It sits just like this. And that'll tell it to run every Monday, but then we wanna test this. So why don't we also set this to run every minute for a little bit and then we'll change that back.
[00:06:30]
So now this is running. And if I go out to my browser, only in testing, you can do this. And it's actually gonna yell at me for trying, but it's okay. So I'm gonna hit API Slack, and this time I'm going to hit Reminder. Am I on the wrong version of this?
[00:07:00]
We're running on This version. So I can replace the hash there and hit it. And it says, you perform an HTTP request for the reminder, which is a scheduled function. You can do it to test locally, but it won't work in production. Now assuming that did what we wanted, hey, here we go.
[00:07:28]
We've got a reminder from our Food Fight app that says, what all of the open items are, and then has a link to follow through and see them all in Notion.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops