bytelyst-devops-tools/Slack Message/README.md
reethu2703 a971d9366c Upgrade Slack CLI to two-way interactive chat with real-time messaging
- Add interactive chat mode with persistent configuration storage
- Implement real-time message receiving with polling mechanism
- Add threading support for simultaneous send/receive operations
- Create MessageReceiver class for handling incoming messages
- Add commands: /listen, /stop, /history for two-way communication
- Display sent messages in terminal with timestamps
- Support for environment variables and .env file loading
- Enhanced error handling and user-friendly prompts
- Update README with comprehensive two-way communication documentation
- Maintain backward compatibility with original CLI functionality
2025-10-05 00:08:27 +05:30

4.2 KiB

Slack Two-Way Chat CLI

A powerful command-line tool for two-way communication with Slack channels. Send and receive messages in real-time with both CLI and interactive chat modes.

Installation

  1. Install dependencies:
pip install -r requirements.txt

Usage

Start an interactive chat session where you can send and receive messages:

python slack_poster.py --chat
python slack_poster.py -i

In chat mode, you can:

  • Type messages directly to send them to Slack
  • Use /listen to start receiving messages from others in real-time
  • View message history with /history
  • Use commands to manage your configuration
  • Have your token and channel ID automatically saved for future sessions

Command Line Mode

Send a single message using command line arguments:

python slack_poster.py --token <SLACK_TOKEN> --channel <CHANNEL_ID> --message "<MESSAGE>"

Interactive CLI Mode

If any parameter is missing, the tool will prompt you interactively:

python slack_poster.py

Examples

# Interactive chat mode
python slack_poster.py --chat

# Single message via CLI
python slack_poster.py -t xoxb-1234567890-1234567890-abcdefghijklmnopqrstuvwx -c C04ABC123 -m "Hello World!"

# CLI with prompts for missing parameters
python slack_poster.py

Parameters

  • --token or -t: Slack Bot/User OAuth token (e.g., xoxb-...)
  • --channel or -c: Slack channel ID (e.g., C04ABC123)
  • --message or -m: Message text to post
  • --chat or -i or --interactive: Start interactive chat mode

Interactive Chat Commands

When in chat mode, you can use the following commands:

  • /help or /h - Show help message with all available commands
  • /quit or /q or /exit - Exit the chat
  • /token <token> - Set or update your Slack token
  • /channel <id> - Set or update the channel ID
  • /status - Show current configuration (token and channel)
  • /clear - Clear all saved configuration
  • /test - Send a test message to verify your configuration
  • /listen - Start listening for incoming messages in real-time
  • /stop - Stop listening for messages
  • /history [n] - Show recent message history (default: 10 messages)
  • <message> - Send any text as a message to the current channel

Two-Way Communication Features

The tool now supports full two-way communication with Slack channels:

Real-Time Message Receiving

  • Use /listen to start receiving messages from others in the channel
  • Messages appear automatically with timestamps and sender names
  • Polls for new messages every 2 seconds for near real-time experience
  • Use /stop to stop listening when needed

Message History

  • Use /history to view recent messages in the channel
  • Specify number of messages: /history 20 (default: 10, max: 50)
  • Shows messages with timestamps and sender information

Threading Support

  • Send and receive messages simultaneously
  • Background thread handles incoming messages
  • Clean shutdown when exiting the application

Configuration Storage

The tool automatically saves your Slack token and channel ID to slack_config.json in the current directory. This allows you to:

  • Skip entering credentials on subsequent runs
  • Switch between different tokens/channels using commands
  • Maintain persistent configuration across sessions

Error Handling

The tool handles various error scenarios:

  • Invalid token
  • Channel not found
  • Bot not in channel
  • Rate limiting
  • Network issues
  • Empty messages

Getting Slack Credentials

  1. Go to https://api.slack.com/apps
  2. Create a new app or select existing one
  3. Go to "OAuth & Permissions"
  4. Add the following scopes:
    • chat:write - Send messages
    • channels:history - Read channel messages
    • groups:history - Read private channel messages
    • im:history - Read direct messages
    • mpim:history - Read group direct messages
    • users:read - Get user information
  5. Install the app to your workspace
  6. Copy the Bot User OAuth Token (starts with xoxb-)

Channel ID

To get a channel ID:

  1. Open Slack in your browser
  2. Navigate to the channel
  3. The channel ID is in the URL: https://app.slack.com/client/T1234567890/C04ABC123
  4. Use the part after /C (e.g., C04ABC123)