It can be hard to keep up with all of the changes in your Slack workspace! This tutorial will walk you through how to build a simple tool in Darklang that posts all new, public Slack channels created in your Slack workspace in the past 24 hours.
A full sample canvas for this tutorial is available here.
- Create a new app in Slack.
- Navigate to Features -> OAuth& Permissions, scroll down to scopes and add
the following bot token scopes:
chat:write.public. This will allow our app to interact with our the channels (also known as conversations) and post messages about them.
- Install your app to the workspace (either via Settings -> Basic Information or Settings -> Install App). Since this app is being built for your workspace specifically, we will not need to set up full OAuth, like we would if we wanted to distribute this more publicly.
- Copy your bot user access token, which will now be available under Settings
-> Install app and save it as a function in your Darklang canvas. Use this
function wherever you see
tokenin the code examples.
- If no new channels have been created in your Slack recently, create a new public channel so we have some data to work with.
- You will need to choose a Slack channel to post your messages to. You can
find your Slack channel's id by navigating to the channel and looking for the
11 character string that begins with a capital C - it will look something
C1234567891. Add that to its own Darklang function as well, and use it wherever you see
channelin the code examples.
Building in Darklang
- Create a new daily Cron named
checkForNewChannelsby clicking the + in the sidebar next to Cron, and call the
getNewConversationsSlack function (it's a little confusing, but channels are referred to as conversations in the Slack API). The
getNewConversationsfunction is built into Darklang's package manager, and it takes your token as well as an amount of time to check in Epoch seconds. Here, we're asking it to check in the past 24 hours, which is equivalent to 86400 seconds.
- Click the play button on the
getNewConversationsfunction and then place your carat in
newChannels. You will see a list of information about any new channels that have been created.
- Create a function called
channelListparameter. This is where we're going to build the format of the messages Slack will print about your new channels, but for now, just return
channelList. You'll notice that an error is shown - this is because
channelListhas not yet been set.
- Return to your
checkForNewChannelsCron, and add a line to format the
newChannelslist you previously generated. Press the play button.
- Return to your
formatChannelListfunction and place your carat over the top dot (trace) on the left. You will see your
channelListtrace - now you can work with that data in order to format your list.
- For our purposes, we're going to do a very simple amount of formatting -
we're going to add the markdown Slack needs to make channel names links, and
add information about the channel's purpose. However, lots of information
about Slack channels is available. Type
val.and scroll through the autocomplete to see all of your options.
formatChannelListto update the value of
Add a check for the length of the list - we're going to print out something different if there have been no new channels.
- The full code is going to look like this - if there are entries in the list,
we'll post an announcement message and then the list of new channels. You'll
notice that we also use the
postMarkDownMessageSlack function to simplify the posting of the message.
- To make our code a little bit cleaner, we've pulled the logic of posting the list of new channels out into a function. This function iterates through the list it is given and posts a markdown message to Slack for each entry.
- Once you've completed all of your code, run the Cron again. A message will appear in the channel you selected.
Congratulations - you now have a Slack app that will post every 24 hours with any new channels created since it last posted.