Send Message to Slack with a Node.js Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there I'm Sean C Davis and actually it's really cold in here okay much better now recently I had written a custom script and I wanted the output of that script to be sent to a specific slack Channel and what I found is that there are so many capabilities in working with the slack API that doing something very simple like sending a message from a script to a channel in slack takes quite a bit of navigating of everything that's going on to understand how to just send this one message and in the end the process is actually quite simple and really only has three main components first we have to set up and configure our application second we have to learn how to work with what slack calls the block kit and then third we can actually write the script and interact with their SDK to send the message all right so let's get into it the first thing we're going to do is create a new slack app you can start by going to api.slack.com apps and then click this create new app button and we can choose to either start from scratch or from an app manifest we're going to start from scratch for the example and we can give the thing a name I'm just going to call it test app for now and then pick the appropriate workspace to put it in I have one that's just for me and so I'm going to say create app and now we have to configure this application the first We'll add incoming web hooks let's turn these on and then come down here and we will add a new web hook to our workspace we're going to choose the channel I'm going to choose the channel called test which is just a private Channel I use to test slack applications and the thing is you can actually send your message to different channels along the way so so just pick something to start with and you can always change that later on okay next come over to oauth and permissions there's a token here that we're going to come back to but for now we want to give it some permission so we have to add a few Scopes by default we have incoming web hook scope but we have to actually add a few more and we're going to add them in this bot token Scopes section so first we're going to add chat right and then we are going to add chat right public and then we have incoming webhook already so we are all set okay and as we're doing this you probably see this big scary Banner pop up here it's basically saying when we created the application we had already installed it and so we now need to reinstall the application so go ahead and click this link and it's basically just going to ask you to pick this channel again and again it doesn't really matter you can always change this later okay and next let's go back to basic information and make sure we've got everything filled out so if we scroll all the way down here you see this display information this is where if you want to add an icon an app name a description this is a really good place to do it this will control how it shows up for example if I go to my slack instance here you can see this is a bot and I've got a logo and then we also get if I click on this I get an about section and I see a description here as well so this will this will really help with especially if you're going to share it or distribute it it's good to have that information the other thing I'll point out while we're here is that you should see a message that says you've added an integration to the channel That You selected when you installed the app and if you're following along you should actually see two of them because if you recall we installed it and then we reinstalled it after we changed the permissions with that let's jump over to our code so in my code base over here all I really have is this singular index.njs file and I'm using mjf so I can use import over require and you can see there's not much else going on in this project okay now let's install our dependencies so we're going to install the slack SDK and that is at slack slash Bolt and then let's also install dot EnV so that we can save our environment variables somewhere and while that's happening let's go ahead and create a file in the root of our project to store these environment variables so dot EnV and if you haven't already probably want to ignore this file in git because this is going to have sensitive data so we here we're going to add slack signing Secret and slack bot token and slack Channel now I know already I can set slack channel to test so I'm going to go ahead and do that so the first one select signing secret you can find down here in app credentials which is under basic information and the slack bot token you can find in oauth and permissions and that is this value right here so go ahead and copy both of those paste them in your dot EnV file and then save this file I'm going to do this off camera for a second by the way before we get back into writing the script something I failed to mention before is that the reason I use an environment variable to specify the channel is because it tends to make it really easy to switch which channel we are posting that message to and that's why when I went back to saying we installed it to a particular Channel if we have the right permission level we can still change that channel that we send it to within the script itself and so you often want a different channel to test in than you're going to use in production and so that's why I use an environment variable because I can quickly change it locally without actually changing any of the code that might be running in production all right now the second component that I mentioned at the beginning of the video is that we have to understand how blocks work blocks are basically the building blocks of the content that we're going to use to craft a visually Rich message in slack and the easiest way to do this and to see what's going on is to go to app.slack.com block kit Builder and you don't need any of this other stuff here if you just go to this URL which I'll put in here just like that and I will link it in the description below you're going to get redirected somewhere and you're going to see that you've got this preview automatically you've you have all of these options for blocks on the left side and as you click them you can add and remove things over here so you get this interactive preview and then on the right side of the screen you see this Json payload and that's what you're going to send to slack to craft that visually Rich message so now this is obviously a very elaborate set of blocks and we're not going for anything that's super interactive here we really just want to post a simple message we don't need any feedback or anything like that so what I'm going to do is I'm going to steal some code that I've already have written paste it over here and just show you how we can we can do something super simple like notify that I have a new post published link to that post and show the author of that post and it's I make I'll make this a little bit bigger here to show you that the Syntax for these markdown sections is a little bit different first of all you see that blocks is an array here it's an array of objects each object is going to have a type these types can be something like section divider context Etc that'll change the style a little bit and when you get down to a markdown section notice that the markdown looks a little bit different than it might in other applications if you're used to writing markdown so emojis come through with a surrounded by colons and then links don't look like typical markdown links and the thing I would suggest is to simply just use this Builder and the items on the left build what you want see the Json output that it gives you and then we can move that over to the script okay and with that we're ready to write some code so let's rearrange the windows I'm actually going to pull up my slack instance here so we can see this happening in real time and let's get our script going so first we are going to import Slack from slack slash bolt and then we're also going to import dot mfrom.env here and the first thing we're going to do is we're going to actually load our environment variables into our process.env object so let's do that by saying Dot env.config crate and now we can create a new slack API client so we're going to say constant and we'll just call it app we're going to say slack dot app and then we have to give it our credentials in here so first is signing secret and we're going to get that from our environment slack signing Secret and then we have our token and that's again process.env because we set it as an environment variable and that's slat slack bot token all right and then usually I want to make sure that this looks okay and I don't get an error so let's log the app let's run the script again and notice that if I get output something that looks like it is a class instance then I am good to go I should have what I need here in my app object so what we can really do now is we can say await app.client this is going to be way simpler than it seems like especially if you've dug through the documentation before it seems like you have to go through all this interactivity and you've got to set up your own server to be able to send a message and you don't you just have to do app.client.chat.post message and then we've got to give it this object so it knows what to expect so the first thing is the token which is process.env.slack bot token we're going to give it our Channel which remember we set as slack Channel and then we're going to give it some text you can say this is a test and for now let's stop there and let's see if we can actually do anything okay so I just tested this and it didn't work and the reason it didn't work was because I'm using a private channel for my test and I hadn't added the application to the channel and it seems a little bit goofy because we had these two messages that said added an integration to this channel but I didn't add the actual app user to the channel and so what I did was I clicked on the name of the app and then I said add app to a channel and then I selected my channel in here and then I saw a test app was added to this Channel and now I should be able to run this so if I run my node index.mjs and we see immediately we get this message posted in here so that's great that's already a big success you can see that look this is not very much code and it's really simple to just be able to send that message right to the channel now just for a test let's go ahead and Define some blocks so that we can send a little bit of a richer message so maybe we make a new constant up here called blocks and we'll make that an array and then let's come back over here to our example copy the just what's inside the array since I've already added the square brackets here I'm going to paste that in and then we will send this block right here at this box as the property and if you're not familiar with this syntax here this is the same as saying this and so this is just a shortcut here so blocks let's go ahead and run it again and then while this happens look at this we actually get a we get the the message we thought we should get so now you can take this and you can play around with it however you want you can build some rich blocks over here for example let's do something that's a little bit more complicated okay there's there's some columns in here as well so let's take that maybe we replace our blocks here and we're gonna have to get rid of the because we want blocks to just be the array inside there save that go ahead post it again let's come back to slack and we see it looks exactly the same so now you have the foundation of what you need to be able to do to just post simple messages to slack you can have an app you don't need to host that app on a server it doesn't need to be reactive in any way you can just have it run inside scripts wherever you can run node.js you can post messages to slack it's as simple as that hopefully you found this tutorial helpful use the comments section below if you have questions or want me to dig deeper into interactions with the slack API subscribe for more videos like this and I'll see you next time
Info
Channel: Sean C Davis
Views: 6,226
Rating: undefined out of 5
Keywords:
Id: SbUv1nCS7a0
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Wed Feb 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.