# Slack AI-Powered Two-Way Chat CLI

A powerful command-line tool for AI-powered two-way communication with Slack channels. Send and receive messages in real-time with intelligent question-answering capabilities using Perplexity AI.

## Installation

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

## Usage

### Interactive AI-Powered Two-Way Chat Mode (Recommended)

Start an interactive chat session where you can send and receive messages with AI-powered question answering:

```bash
python slack_poster.py --chat
python slack_poster.py -i
```

In chat mode, you can:
- Type messages directly to send them to Slack
- Ask questions and get AI-powered answers automatically
- 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, channel ID, and Perplexity API key automatically saved for future sessions

### Command Line Mode

Send a single message using command line arguments:

```bash
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:

```bash
python slack_poster.py
```

### Examples

```bash
# 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
- `/perplexity <key>` - Set or update your Perplexity API key
- `/status` - Show current configuration (token, channel, and AI settings)
- `/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)
- `/ai on/off` - Enable or disable AI responses
- `<message>` - Send any text as a message to the current channel

## AI-Powered Question Answering Features

The tool now includes intelligent question-answering capabilities using Perplexity AI:

### Automatic Question Detection
- AI automatically detects when you ask questions
- Supports various question formats: "What is...?", "How do...?", "Why...?", etc.
- Responds with intelligent, accurate answers

### AI Response Examples
```
> What is the capital of India?
[14:30:15] You: What is the capital of India?
[OK] Message sent to #C09JKHXS9L3
[AI] Thinking...
[14:30:18] AI Bot: The capital of India is New Delhi.
[OK] AI response sent to #C09JKHXS9L3
```

### AI Configuration
- Use `/perplexity <your-api-key>` to set your Perplexity API key
- Use `/ai on` to enable automatic AI responses
- Use `/ai off` to disable AI responses
- AI responses are sent to the Slack channel with 🤖 AI: prefix

## 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 API Credentials

### 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-`)

### Perplexity AI Credentials

1. Go to https://www.perplexity.ai/
2. Sign up for an account
3. Go to your account settings or API section
4. Generate an API key
5. Copy the API key (starts with `pplx-`)
6. Use `/perplexity <your-api-key>` in the chat to set it

## 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`)
