juniordevstrugglebus.com
Visit the Junior Dev Struggle Bus website for more information on the organzation, and opportunities to contribute to the community!
Useful steps and tools for starting your own Slack workspace to test things like webhooks and slash commands.
This is a guide to setting up your own Slack workspace for testing, representing the first part of the LocalTestStruggleApp repository. It’s purpose is for those that wish to contribute to the JDSB Struggle Buddy Slack project, but also contains basic information that would carry over to using these tools in general.
The above repository will eventually expand to include using serverless Netlify functions in conjunction with Slack, just as Struggle Buddy itself functions. Those additions will most likely appear as a separate a blog post when they are ready.
While this post can be treated as a standalone guide, it may assume you have cloned a local copy of the LocalTestStruggleApp in order to access demonstrative assets, such as the server.js
.
You should already have a Slack account and be signed in at https://slack.com.
Navigate to https://slack.com/create and enter the email address that you wish to use for this workspace. You’ll be asked for a confirmation number that you should receieve via email.
After entering the confirmation number, you will be asked to name your team. This will be the initial name of both your Slack workspace and the URL, e.g. “My Team” and myteam.slack.com
. The next step requests a project that your team is working on. This will be designated as a default channel in your workspace and can be whatever you like. If you enter “My StruggleApp function”, #my-struggleapp-function
channel will be created.
If you’re working solo or not sure who you’re going to invite yet, you can skip the step for inviting team mates.
You should now be looking at your very own workspace in the Slack browser app. If you use the Slack desktop app and wish to add the workspace there:
Window
drop down in the menu bar.A quick note on adding the workspace to your Slack mobile app: You should be able to access your workspaces on your mobile app by tapping the icon that looks 4 squares in a 2x2 formation. The onscreen instructions should make it fairly obvious how to add the workspace from there.
Slack will prompt you to finish signing up. Here are some quick steps to handle that:
Your workspace is all set! Slackbot will introduce itself and you can poke around and play with whatever you like.
Navigate to https://api.slack.com. This is your first stop for familiarizing yourself with the Slack api and how to work with apps and bots. Peruse the wealth of information at your leisure, but for now, let’s create an app.
Click “Your Apps” in the upper right corner, right next to “Documentation” and “Tutorials”. Alternatively, navigate directly to https://api.slack.com/apps.
Click “Create new app” and a dialog will appear asking for a name and a workspace. Try naming your app “LocalTestStruggleApp” and selecting the workspace you created earlier. Keep in mind that while you can rename an app, it is forever tied to the workspace you choose here. Click “Create app” to continue.
You should now be looking at your app’s Basic Information page. Note that while your app exists, it is not yet installed to your workspace and has no functionality. Also take note of the App Credentials section. You’ll return to these later.
For now, click on “Add features and functionality” and then “Incoming Webhooks.” Webhooks are one way for an outside application or request to send a message to your Slack workspace. Click the switch to activate incoming webhooks from “off” to “on” and then click “Add New Webhook to Workspace.”
You will then be prompted to choose a location for the webhook to post to. Since Slack creates #general in your workspace by default, choose that from the drop down menu for the sake of simplicity and click “Install.” This will generate a new webhook URL and provide you with a sample curl request. Copy and paste the curl request into your terminal and your app will send the message “Hello, World!” to your #general channel.
Before doing anything else with your new Slack app, move on to the next section where you’ll install a helpful local testing tool.
/sdt
command.Ngrok is a useful tool for exposing your local webserver to the internet.
Navigate to https://ngrok.com/ and sign up for free. Using your GitHub account eases this process along.
After being redirected to https://dashboard.ngrok.com/get-started, follow the installation and setup steps. On Mac, you could take this approach to installation:
Download the appropriate version of ngrok. From your terminal, enter this command: unzip path/to/downloaded/nkrok.zip -d /usr/local/bin
. This uses the -d
switch to unzip the contents of your ngrok download to the specified folder. In this case, to /usr/local/bin
. This will allow the ngrok
command to be used in anywhere on your system.
Read the top answer on this stackexchange question for why you would want to put it here.
You could also just keep it in any folder of your choice or install it through a package manager like Homebrew.
The third step on the getting started page provides the authtoken command for you to paste in terminal, but you can retrieve your token at any time by clicking on “Auth” in the sidebar.
You should now be able to type ngrok http 3000
to open up a tunnel to your localhost on port 3000. Ngrok will expose your local server with a randomly generated URL in the form of [randomstuff].ngrok.io
.
If you try to navigate to this URL when not running a local server, you will redirect to an error page. You can observe the bad requests from the terminal window that is running ngrok or from http://localhost:4040/inspect/http.
To actually see ngrok in action, run one of your local projects and initiate an ngrok http [port number your run your project on]
and navigate to the URL ngrok provides. If you don’t have a project handy but you’ve cloned this repository, this repository provides a simple Express/Node server that runs on port 3000. Run node server.js
from this repository’s directory in one terminal window, and ngrok http 3000
in another. You should see the same result on localhost:3000
as you do when you access the ngrok fowarding URL.
Note: you can stop your node and ngrok servers from running with ctrl+C
.
To really appreciate the magic that is happening here, grab your mobile device or another computer and navigate to the ngrok URL. You will see your local project running there. This demonstrates that your local server is open to anyone your share the ngrok URL with.
With a free account, ngrok will generate a new random URL everytime you start it up. Keep in mind these aren’t permanant and that any hooks or other outside references to the URL will have to be updated to the new URL if you restart ngrok.
However, the URL will persist as long as you keep ngrok running. Feel free to start and stop your local server as many times as you like. Using the earlier example, run ngrok and navigate to the URL to be redirected to the expected error page. Next, run the server.js from this repository and reload the ngrok URL to see that it is now forwarding your local project. If you stop the Node server, the ngrok URL will return the error page on reload, but the URL will persist for the next time you start up your local server.
Begin by launching ngrok with ngrok http 3000
in its own terminal window and node server.js
in another. Return to https://api.slack.com/apps and select the app you created earlier. This time, you will be selecting “Slash Commands” from either the features sidebar or the “Add features and functionality” menu. Click the Create slash command button to get started.
The well designed UI for creating a new command makes most of the options easy to understand:
/localteststruggle
.Request URL: This is the endpoint on your server that Slack will send a POST request to when the command is used.
/lts
: http://randomstuff.ngrok.io/lts
— this route is already setup in the server.js.The option to escape channels and usernames will send along their respective IDs instead of their actual names. The intended purpose of your command will dictate whether you want this checked or not. It can be useful to access the IDs directly to query Slack API endpoints, but leave it unchecked for now.
After clicking “Save,” you will return to the Slash Commands feature page. You will also be prompted to reinstall your app. This is required anytime your app needs to access a new permission scope. Follow the link and click “Reinstall App.” This will present you with a prompt similar to when you created a webhook. Choose #general once again and click “Install.”
Open your Slack app and type /localteststruggle
. Your app should respond with an ephemeral message (a temporary message, visible only to the person that used the command) assuring you that the slash command is working. The terminal that is running the node server should also print out the object that Slack sends to your app so you can get an idea of how Slack passes data in it’s request. Now type /localteststruggle This will be echoed
to observe the attachment behavior, passing back the arguments given to the slash command along with the original message..
View the server.js file included in this repository to see the code and read the comments to understand what is happening here, starting on line 10.