Using Slack to stay updated.

Not too long ago, I was writing about translating the receive of Slack messages onto the waving behavior of a Maneki-Neko.

In this story, I want to talk about Slack and how to use it to stay updated. But a bit of context first. Near my place of work is a little café which offers new dishes daily. Every working day at about noon, we are all facing the same question: What are we going to eat for lunch? Most of my coworkers are interested in daily changing food and instantly start browsing the little cafés’ website to check the dish of the day.

Dishes of the day for one week.

I asked myself why we are all starting to browse instead of getting this information without doing anything? Since we are online anyway, I thought it would be nice to get a notification including the information we are looking for.

So, let`s create a Slack channel with a custom Slack app installation and make sure to get the right information at the right time.

Therefore I built a little application based on Node.js including, the Slack Web-API and puppeteer.js.

I initially started with creating a Slack app for my workspace. To send messages to any channel, the right permissions and scopes need to be set. After that, we get access to the Bot Users OAuth Access Token.

Permissions and scopes need to be set.

Tipp: Make sure you`re going to integrate your app into a public channel, so everyone in the team can join.

Starting from scratch! First, I had to import all modules and declare some variables. As you can see, I also imported the dotenv module and assigned it to a variable called dotenv. After that, I called the config() function. This function loads anything that is in a file called .env into an environment variable. When working with API keys or sensitive information, this is always the best way to go!

Before posting to a Slack channel, we need to make sure to get the right piece of information. In my case, it’s a simple image URL. Therefore I wrote a function including puppeteer.js.

Tipp: Puppeteer is a headless browser and very useful. You can do a lot of great stuff with it. It can even take screenshots from a website, which is very neat.

Since I am writing an async function and expecting to get a promise as its return, we need to use the await keyword here. Simply because JavaScript waits until that promise settles and returns its result. In addition to that, we’ve to wait for several things to get finished first.

It’s also important to deal with error handling too. For this, I used the try/catch syntax construct. If everything works out, the function returns the image URL as a string. Otherwise, there would be an error.

Before we can go on, we need to edit the .env file first. We have to set our authentication token and define our Slack channel.

Tipp: You can find your authentication token in your custom Slack app settings. Open up Slack in the browser to find your Slack channel ID. It should appear in the browser bar.

Now it`s time to take a closer look at the actual posting process. For this, we`re going to use the “chat.postMessage” method to send the message to the channel. After a successful post, it returns the timestamp. This one is going to help us later.

I want to keep this channel as clean as possible. This means, after the message is sent to the channel, my sleep function gets executed. As soon as the time runs out, basically, until we’ve decided on what we’re going to eat for lunch, the previously sent message gets deleted.

To delete a specific message in a Slack channel, we need the corresponding timestamp. As I said before, the previously returned timestamp is going to help us later. And the time is now! So, the only thing we need to pass to the “chat.delete” method is the exact timestamp of the message we want to delete.

Send and delete process.

It has turned out that the small café always publishes its image of the daily dish at around 11:30 am. So, I`ve set up a cronjob that executes the main script every working day. Besides, I can highly recommend you to take a closer look at PM2. It`s a handy daemon process manager that will help you manage your applications.

pm2 start index.js — cron “30 11 * * MON-FRI”

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store