This adapter provides a pluggable implementation for integrating Slack as a messaging platform. It is designed to work with RelaySMS Publisher, enabling users to connect to Slack using OAuth2 authentication.
- Python: Version >= 3.8.10
- Python Virtual Environments: Documentation
Install the necessary system packages:
sudo apt install build-essential python3-dev-
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
. venv/bin/activate -
Install the required Python packages:
pip install -r requirements.txt
-
Create a new Slack app:
- Go to the Slack API Apps page
- Click "Create New App"
- Choose "From scratch"
- Enter your app name and select the workspace where you want to develop the app
- Click "Create App"
-
Configure OAuth & Permissions:
- In your app's settings, navigate to "OAuth & Permissions" in the left sidebar
- Scroll down to "Redirect URLs" section
- Click "Add New Redirect URL"
- Add your redirect URI (e.g.,
https://example.com/callback/) - Click "Save URLs"
-
Enable Token Rotation:
- In the same "OAuth & Permissions" page, scroll down to "Token Rotation" section
- Click "Opt into Token Rotation"
- This ensures that a refresh token is returned every time a user grants access to your app
- Click "Save Changes"
-
Set up User Token Scopes:
- In the same "OAuth & Permissions" page, scroll down to "Scopes"
- Under "User Token Scopes", click "Add an OAuth Scope"
- Add the following required scopes:
chat:writeprofileusers:readusers:read.email
- Click "Save Changes"
-
Enable public distribution (optional):
- To allow your app to work with different workspaces, go to "Manage Distribution" in the left sidebar
- Under "Share Your App with Other Workspaces", review the checklist requirements
- Once all requirements are met, click "Activate Public Distribution"
- This makes your app available for installation in any workspace
-
Gather your app credentials:
- Go to "Basic Information" in the left sidebar
- Under "App Credentials", you'll find:
- Client ID - Copy this value
- Client Secret - Click "Show" and copy this value
Create a credentials.json file with your Slack app information obtained from Step 1 point 5:
Sample credentials.json
{
"client_id": "your_client_id_here",
"client_secret": "your_client_secret_here",
"redirect_uris": ["https://example.com/callback/"]
}Field descriptions:
client_id: Your app's Client ID from the Basic Information pageclient_secret: Your app's Client Secret from the Basic Information pageredirect_uris: Array of redirect URLs you configured in OAuth & Permissions (must match exactly)
Tip
Local Development with HTTPS:
OAuth2 authorization servers require HTTPS protocol for redirect URIs. For local development, you can use these tools to create secure tunnels:
- ngrok:
ngrok http 3000(expose localhost:3000 via HTTPS) - localtunnel:
lt --port 3000(npm package) - VS Code Tunnels: Built into VS Code for port forwarding
- Cloudflare Tunnel: Free secure tunnels to localhost
Create or edit your config.ini file to specify the path to your credentials:
[credentials]
path = ./credentials.jsonNote
Use the --help flag with any command to see the available parameters and their descriptions.
Use the auth-url command to generate the OAuth2 authorization URL.
python3 slack_cli.py auth-url -o session.json-o: Save the output tosession.json.
Use the exchange command to exchange the authorization code for tokens and user info.
python3 slack_cli.py exchange -c auth_code -o session.json -f session.json-c: Authorization code.-o: Save the output tosession.json.-f: Read parameters fromsession.json.
Use the send-message command to send a message using the adapter.
python3 slack_cli.py send-message -f session.json -m "Hello, Slack!" -r "social" -o session.json-f: Read parameters fromsession.json.-m: Message to send.-r: Recipient channel or user ID.-o: Save the output tosession.json.
Direct Messages (DMs):
@username- Send DM to user by username@user@example.com- Send DM to user by email address@John Doe- Send DM to user by display nameU1234567890- Send DM using direct user ID
Channel Messages:
#channel-name- Send message to channel by namechannel-name- Send message to channel by name (without #)C1234567890- Send message using direct channel ID
Examples:
# Send DM to user by username
python3 slack_cli.py send-message -f session.json -m "Hi there!" -r "@john.smith" -o session.json
# Send DM to user by email
python3 slack_cli.py send-message -f session.json -m "Hello!" -r "@john@company.com" -o session.json
# Send message to channel
python3 slack_cli.py send-message -f session.json -m "Team update" -r "#general" -o session.json
# Send message to channel (without #)
python3 slack_cli.py send-message -f session.json -m "Team update" -r "general" -o session.jsonNote
When using @ prefix for direct messages, the adapter automatically resolves usernames, email addresses, and display names to their corresponding user IDs. If the user cannot be found, the adapter will attempt to resolve the value as channel.
Use the revoke command to revoke the OAuth2 token and invalidate the user's session.
python3 slack_cli.py revoke -f session.json -o session.json-f: Read token fromsession.json.-o: Update the file by removing the revoked token.
Warning
After revoking a token, the user will need to re-authenticate to use the adapter again. The revoked token will be removed from the output file if specified.
- Implement message formatting options (markdown, attachments, etc.)