A Slack bot to move or delete entire threads from one channel to another, preserving the original authors' names and avatars.
- Move Threads: Copy an entire thread from one public or private channel to another.
- Delete Threads: Delete an entire thread from a channel.
- Preserve Authorship: When moving a thread, messages are posted with the original author's name and profile picture.
- Avoid Notification Spam: User and channel mentions are de-linked in the copied thread to prevent re-notifying users.
- Easy to Use: A simple message shortcut opens a modal to select the action and destination channel.
- Permission Tester: Includes a
/test-permissionsslash command to diagnose setup issues.
The project is organized with a clear separation of concerns to make it easier to maintain and extend.
index.js: The main entry point for running the bot locally. It imports the handlers and starts the Bolt app.lambda.js: The entry point for the AWS Lambda deployment. It configures the app for a serverless environment.serverless.yml: Configuration file for the Serverless Framework, defining the deployment settings for AWS Lambda.src/: Contains the core application logic.app.js: Initializes and configures the Slack Bolt app and the userWebClient.handlers/: Contains the handlers for different Slack events.commandHandler.js: Handles the/test-permissionsslash command.shortcutHandler.js: Handles thecopy_thread_shortcutmessage shortcut.viewHandler.js: Handles the submission of thecopy_thread_modal.
services/: Contains services that abstract away external API calls.slackService.js: A dedicated service for all interactions with the Slack API, such as fetching messages, posting messages, and deleting messages.
utils/: Contains utility functions that can be reused across the application.mentionUtils.js: Provides functions to strip mentions from messages to prevent re-notifying users.
The bot is triggered by a message shortcut ("Copy Thread"). It then opens a modal asking the user if they want to:
- Move the thread: Copies all messages to a new channel and then deletes the original thread.
- Delete the thread: Deletes all messages in the original thread without copying them.
To perform these actions, the bot uses two types of Slack tokens:
- A Bot Token for most actions like reading messages, posting messages, and getting user information.
- A User Token for deleting messages, which requires higher privileges.
- Node.js (v14 or later recommended)
- A Slack workspace where you have permission to install and manage apps.
ngrokor another tunneling service for local development to expose your local server to Slack's API.
- Go to the Slack API website and click "Create New App".
- Choose "From scratch", give your app a name (e.g., "Thread Mover"), and select your workspace.
Navigate to the "OAuth & Permissions" page in your app's settings. You need to add scopes for both the bot and the user.
Bot Token Scopes:
Add the following scopes under "Bot Token Scopes":
commands: To add slash commands and shortcuts.chat:write: To post messages as the bot.conversations:history: To read messages in channels where the bot is a member.users:read: To access user profiles for names and avatars.conversations:read: To get channel information.conversations:members: To check channel membership.
User Token Scopes:
Add the following scopes under "User Token Scopes":
chat:write: To delete messages. Important: The user who authorizes this token must have permission to delete messages in the channels where the bot will be used. This usually means they are a Workspace Admin/Owner.
After adding scopes, you'll need to (re)install the app to your workspace.
- Bot Token: On the "OAuth & Permissions" page, you'll find the "Bot User OAuth Token" (starts with
xoxb-). - User Token: You also need to install the app as a user to get a "User OAuth Token" (starts with
xoxp-). Make sure you are installing it as a user with admin/owner privileges. - Signing Secret: On the "Basic Information" page, find your "Signing Secret" under "App Credentials".
- Go to the "Interactivity & Shortcuts" page.
- Make sure "Interactivity" is turned ON.
- Under "Shortcuts", click "Create New Shortcut".
- Select "On a message".
- Fill in the details:
- Name:
Copy Thread - Short description:
Move or delete a thread. - Callback ID:
copy_thread_shortcut
- Name:
- Click "Create".
- Go to the "Slash Commands" page.
- Click "Create New Command".
- Fill in the details:
- Command:
/test-permissions - Short Description:
Check if the bot has the right permissions. - Usage Hint:
Run in a channel to test.
- Command:
- Click "Save".
When you run the bot (either locally with ngrok or on a server), you'll have a public URL. Slack needs this URL to send events.
- Interactivity & Shortcuts Request URL: Set this to
https://your-public-url.com/slack/events. - Slash Commands Request URL: Set this to
https://your-public-url.com/slack/events.
- Clone this repository.
- Install dependencies:
npm install
- Create a
.envfile in the root of the project and add your credentials:SLACK_BOT_TOKEN=xoxb-... SLACK_USER_TOKEN=xoxp-... SLACK_SIGNING_SECRET=...
-
Start the app:
node index.js
The bot will be running on port 3000 by default.
-
Expose your local server to the internet using
ngrok:ngrok http 3000
-
Use the
httpsURL provided byngrokas the base for your Request URLs in the Slack App configuration (e.g.,https://xxxx-xx-xxx-xx-xx.ngrok.io/slack/events).
This project is configured for serverless deployment to AWS Lambda using the Serverless Framework.
- Install the Serverless CLI:
npm install -g serverless - Configure your AWS credentials.
- Deploy the function:
serverless deploy
- The output will give you an API Gateway URL. Use this URL for your Slack App's Request URLs.
- You will also need to configure the environment variables (
SLACK_BOT_TOKEN,SLACK_USER_TOKEN,SLACK_SIGNING_SECRET) in yourserverless.ymlfile or directly in the Lambda function's configuration in the AWS Console.
- Invite the Bot: Invite the bot to any channel you want to manage threads in.
- Move a Thread:
- Hover over any message in a thread.
- Click the "More actions" (three dots) button.
- Select "Copy Thread".
- In the modal that appears, select "Move thread to another channel".
- Choose a destination channel.
- Click "Execute".
- Delete a Thread:
- Follow the same steps as above.
- In the modal, select "Just delete thread (no copy)".
- Click "Execute".
- Test Permissions:
- In any channel the bot is in, type
/test-permissions. - The bot will reply with a summary of its current permissions and token status.
- In any channel the bot is in, type