- Ensure Node.js and Yarn are installed on your machine.
- You need a Slack workspace where you can set up a bot.
Clone the repository where your code is hosted.
git clone <repository-url>
cd <repository-folder>Run the following command to install the project dependencies using Yarn.
yarn installTo start the development server, use the following command:
yarn run start:devThis will run the application locally. The server should now be available on http://localhost:3000 (or whichever port is specified in your configuration).
Install Ngrok to expose your local server to the internet.
-
Install Ngrok via Homebrew (MacOS) or download directly from Ngrok.
For MacOS:
brew install ngrok
-
Sign up for an Ngrok account (if you haven't already), and get your authentication token from Ngrok's dashboard.
-
Authenticate Ngrok with your account token:
ngrok authtoken <your-auth-token>
-
Start Ngrok to tunnel HTTP traffic on your local server’s port (typically port 3000):
ngrok http 3000
Ngrok will now provide a public URL that you can use for your Slack bot’s interactivity endpoint (e.g.,
https://<your-ngrok-id>.ngrok.io).
Follow these steps to set up a Slack Bot for interactivity:
- Go to Slack API and click on “Create New App”.
- Choose the Slack workspace you want to install the bot in.
- App Permissions:
- Add Scopes under Bot Token Scopes such as
chat:write,commands,users:read, andinteractivity:write. - Under User Token Scopes, you can add additional permissions like
users:readif needed.
- Add Scopes under Bot Token Scopes such as
- Install the App to Workspace: Install the app and grant it necessary permissions.
- Go to OAuth & Permissions in your Slack App settings.
- Copy the Bot User OAuth Token. This is the token you will use to interact with the Slack API.
Example:
xoxb-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX - Set up .env file with
SLACK_BOT_TOKEN=xoxb-XXXXXXXXXXXX-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX.
To grab the Slack User ID of the user you wish to send messages to:
- Use the Slack API to get the user ID by calling the
users.listendpoint, or from Slack’s interface by typing/whois @usernamein a channel. - Example: If you have a user
@john.doe, your user ID might look something likeU08DAUKQ8KZ. - Paste your user ID in .env on SLACK_USER_ID.
Set the Interactivity Endpoint to your Ngrok URL:
- In your Slack App settings, go to Interactivity & Shortcuts.
- Turn on Interactivity and enter your Ngrok URL followed by the route for interactions (e.g.,
https://<your-ngrok-id>.ngrok.io/slack/forms/interaction).
Now Slack will send interaction events (like button clicks, menu selections, etc.) to your server’s endpoint.
To send a POST request to your API, such as sending a form to a user, use the following:
To send a form to a specific user:
-
POST Request to
/slack/forms/sendcurl -X POST \ https://<your-ngrok-id>.ngrok.io/slack/forms/send \ -H "Content-Type: application/json" \ -d '{ "userId": "your-user-id" }'
This will send the form to the user with the given Slack user ID.
-
POST Request to
/slack/forms/interactionAfter the user interacts with your form (e.g., selects an option or submits the form), Slack will make a request to your
/slack/forms/interactionendpoint with the interaction data.Your server should handle the request, and you can access the data in your endpoint handler. An example interaction payload might look like this:
{ "type": "block_actions", "user": { "id": "U08DAUKQ8KZ", "username": "john.doe" }, "actions": [ { "action_id": "task-001", "selected_option": { "value": "in-progress" } } ] }You can process this data to determine which actions the user has taken.
Ensure that your server is properly responding to Slack's requests, including:
-
Sending a confirmation message: After successfully sending the form or receiving an interaction, you should respond with appropriate messages to Slack's API (e.g.,
ok: truein JSON response).Example for sending a response after processing the interaction:
{ "response_action": "update", "view": { "type": "modal", "callback_id": "modal-001", "state": { "values": { "task-001": { "selected_option": { "value": "in-progress" } } } } } }
- /slack/forms/send: Send a form to a specific user using their Slack User ID.
- /slack/forms/interaction: Handle user interactions with the form (such as button clicks, selections, etc.).