Building a Slack Chat Bot

Creating a Reminder

Jason Lengstorf

Jason Lengstorf

Learn With Jason
Building a Slack Chat Bot

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

>> 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.

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.

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.

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.

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.

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 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.

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

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.

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.

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.

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.

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 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.

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?

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.

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.

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?

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.

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
Get Unlimited Access Now