Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# TusharKhan Chatbot Package

A framework-agnostic PHP chatbot package that works seamlessly with plain PHP, Laravel, CodeIgniter, or any custom PHP application. Build powerful chatbots with multi-platform support including Web, Telegram, and WhatsApp.
A framework-agnostic PHP chatbot package that works seamlessly with plain PHP, Laravel, CodeIgniter, or any custom PHP application. Build powerful chatbots with multi-platform support including Web, Telegram, WhatsApp, and Slack.

## 🚀 Features

- **Framework Agnostic**: Works with any PHP framework or plain PHP
- **Multi-Platform Support**: Web, Telegram, WhatsApp drivers included
- **Multi-Platform Support**: Web, Telegram, WhatsApp, and Slack drivers included
- **Pattern Matching**: Flexible message routing with parameters, wildcards, and regex
- **Multi-turn Conversations**: Stateful conversations with context management
- **Storage Options**: File-based or in-memory storage (easily extensible)
- **Middleware Support**: Add custom processing logic
- **Fallback Handling**: Graceful handling of unmatched messages
- **Easy Setup**: No complex configuration required
- **Rich Messaging**: Support for buttons, menus, attachments, and interactive components
- **Modern Slack Features**: Events API, Socket Mode, slash commands, and interactive components
- **Fully Tested**: Comprehensive unit test coverage

## 📦 Installation
Expand Down Expand Up @@ -93,6 +95,51 @@ $bot->listen();
?>
```

### Slack Bot

```php
<?php
use TusharKhan\Chatbot\Core\Bot;
use TusharKhan\Chatbot\Drivers\SlackDriver;

$bot = new Bot(new SlackDriver('BOT_TOKEN', 'SIGNING_SECRET'));

$bot->hears('hello', function($bot, $message) {
$bot->reply('Hello from Slack! 👋');
});

// Handle slash commands
$bot->hears('/weather {city}', function($bot, $message, $matches) {
$city = $matches['city'];
$bot->reply("Weather for {$city}: 22°C, Sunny ☀️");
});

// Rich messages with interactive buttons
$bot->hears('menu', function($bot, $message) {
$driver = $bot->getDriver();
$blocks = [
[
'type' => 'section',
'text' => ['type' => 'mrkdwn', 'text' => 'Choose an option:']
],
[
'type' => 'actions',
'elements' => [
[
'type' => 'button',
'text' => ['type' => 'plain_text', 'text' => 'Option 1'],
'action_id' => 'option_1'
]
]
]
];
$driver->sendRichMessage('Menu', $blocks);
});

$bot->listen();
?>
```

## 🔧 Pattern Matching

The bot supports various pattern matching types:
Expand Down Expand Up @@ -606,6 +653,50 @@ $bot->middleware(function($context) {
- `addMessage($type, $message)` - Add to history
- `getHistory()` - Get message history

## 🌐 Supported Platforms

### Web Driver
- HTTP/AJAX requests
- Form submissions
- Session-based conversations
- JSON/HTML responses

### Telegram Bot API
- Message handling
- Inline keyboards
- File uploads
- Callback queries
- Bot commands

### WhatsApp Business API
- Text messages
- Media messages
- Template messages
- Webhook integration

### Slack API
- **Events API**: Real-time message events
- **Slash Commands**: Custom bot commands
- **Interactive Components**: Buttons, menus, and forms
- **Rich Messaging**: Block Kit for rich layouts
- **Mentions & DMs**: App mentions and direct messages
- **Reactions**: Add/remove emoji reactions
- **File Operations**: Upload, share, and manage files
- **Socket Mode**: WebSocket connections for development
- **User Management**: Get user and channel information

**Slack Features:**
- ✅ Message Events (`message`, `app_mention`)
- ✅ Interactive Components (buttons, selects, modals)
- ✅ Slash Commands (`/command`)
- ✅ Rich Text Formatting (Block Kit)
- ✅ Ephemeral Messages (private responses)
- ✅ Reactions and Emoji
- ✅ File Uploads and Sharing
- ✅ User and Channel Information
- ✅ Message Updates and Deletions
- ✅ Webhook Signature Verification

## 🤝 Contributing

1. Fork the repository
Expand Down
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php",
"telegram",
"whatsapp",
"slack",
"messaging",
"framework-agnostic"
],
Expand All @@ -18,8 +19,11 @@
}
],
"require": {
"php": ">=7.4",
"telegram-bot-sdk/telegram-bot-sdk": "^2.3"
"php": ">=8.0",
Comment thread
tusharkhan marked this conversation as resolved.
Comment thread
tusharkhan marked this conversation as resolved.
"jolicode/slack-php-api": "^4.8",
"symfony/http-client": "^6.0|^7.0",
"nyholm/psr7": "^1.8",
"irazasyed/telegram-bot-sdk": "^3.9"
Comment thread
tusharkhan marked this conversation as resolved.
},
Comment thread
tusharkhan marked this conversation as resolved.
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand All @@ -41,5 +45,10 @@
"cs-fix": "phpcbf"
},
"minimum-stability": "stable",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
209 changes: 209 additions & 0 deletions doc/CROSS_PLATFORM_SSL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Cross-Platform SSL Configuration

The SlackDriver now supports automatic SSL certificate detection and configuration across Windows, macOS, and Linux operating systems. This document explains how the cross-platform SSL configuration works and how to use it.

## Overview

The SSL configuration system automatically:
1. Detects the operating system
2. Searches for SSL certificates in common locations for that OS
3. Provides fallback options for downloading certificates
4. Configures SSL settings appropriately for local development

## Supported Operating Systems

### Windows
- **Laragon**: Automatically detects certificates at `D:\laragon\etc\ssl\cacert.pem` or `C:\laragon\etc\ssl\cacert.pem`
- **XAMPP**: Looks for certificates at `C:\xampp\php\extras\ssl\cacert.pem`
- **WAMP**: Searches in WAMP PHP SSL directories
- **User Directory**: `%USERPROFILE%\AppData\Local\chatbot-certs\`

### macOS
- **Homebrew**: Checks `/usr/local/etc/openssl/cert.pem` and `/opt/homebrew/etc/openssl/cert.pem`
- **System**: Uses `/etc/ssl/cert.pem`
- **MAMP**: Looks in `/Applications/MAMP/conf/apache/ssl.crt/`
- **Valet**: Checks Valet certificate paths
- **User Directory**: `~/Library/Application Support/chatbot-certs/`

### Linux
- **Ubuntu/Debian**: Uses `/etc/ssl/certs/ca-certificates.crt`
- **CentOS/RHEL**: Uses `/etc/pki/tls/certs/ca-bundle.crt`
- **Docker**: Checks container-specific paths
- **User Directory**: `~/.local/share/chatbot-certs/`

## Configuration Methods

### Method 1: Environment Variable (Recommended)
Set the `SLACK_CACERT_PATH` environment variable to point to your certificate file:

#### Windows
```powershell
# PowerShell
$env:SLACK_CACERT_PATH = "C:\path\to\your\cacert.pem"

# Command Prompt
set SLACK_CACERT_PATH=C:\path\to\your\cacert.pem

# Permanently (requires restart)
setx SLACK_CACERT_PATH "C:\path\to\your\cacert.pem"
```

#### macOS/Linux
```bash
# Temporary
export SLACK_CACERT_PATH="/path/to/your/cacert.pem"

# Permanent (add to ~/.bashrc, ~/.zshrc, or ~/.profile)
echo 'export SLACK_CACERT_PATH="/path/to/your/cacert.pem"' >> ~/.bashrc
```

### Method 2: Laravel .env File
If you're using Laravel, add this to your `.env` file:
```env
SLACK_CACERT_PATH=storage/certs/cacert.pem
```

### Method 3: Automatic Download
The system can automatically download the certificate:

#### Windows (PowerShell)
```powershell
# Create directory
New-Item -ItemType Directory -Force -Path "$env:USERPROFILE\AppData\Local\chatbot-certs"
# Download certificate
Invoke-WebRequest -Uri "https://curl.se/ca/cacert.pem" -OutFile "$env:USERPROFILE\AppData\Local\chatbot-certs\cacert.pem"
# Set environment variable
$env:SLACK_CACERT_PATH = "$env:USERPROFILE\AppData\Local\chatbot-certs\cacert.pem"
```

#### macOS
```bash
# Create directory
mkdir -p "$HOME/Library/Application Support/chatbot-certs"
# Download certificate
curl -o "$HOME/Library/Application Support/chatbot-certs/cacert.pem" https://curl.se/ca/cacert.pem
# Set environment variable
export SLACK_CACERT_PATH="$HOME/Library/Application Support/chatbot-certs/cacert.pem"
```

#### Linux
```bash
# Create directory
mkdir -p "$HOME/.local/share/chatbot-certs"
# Download certificate
curl -o "$HOME/.local/share/chatbot-certs/cacert.pem" https://curl.se/ca/cacert.pem
# Set environment variable
export SLACK_CACERT_PATH="$HOME/.local/share/chatbot-certs/cacert.pem"
```

## Development Environment Detection

The system automatically detects common development environments:

### Windows
- **Laragon**: Looks for `LARAGON_ROOT` environment variable
- **XAMPP**: Checks for `XAMPP_ROOT` environment variable
- **WAMP**: Searches common WAMP installation directories

### macOS
- **Homebrew**: Detects Homebrew installations
- **MAMP**: Looks for MAMP.app installations
- **Valet**: Checks for Laravel Valet configurations

### Linux
- **Docker**: Detects container environments
- **Vagrant**: Looks for Vagrant-specific paths
- **System packages**: Uses system certificate stores

## Troubleshooting

### Certificate Not Found
If you see SSL certificate errors:

1. **Check environment variable**:
```bash
# Windows (PowerShell)
echo $env:SLACK_CACERT_PATH

# macOS/Linux
echo $SLACK_CACERT_PATH
```

2. **Verify certificate file exists**:
```bash
# Check if file exists and is readable
ls -la /path/to/your/cacert.pem
```

3. **Download manually**:
```bash
curl -o cacert.pem https://curl.se/ca/cacert.pem
```

### SSL Verification Issues
For development environments, the system automatically:
- Disables SSL peer verification
- Sets appropriate cURL options
- Configures stream contexts

### Permission Issues
Ensure the certificate file and directory are readable:

#### Windows
```powershell
# Check permissions
Get-Acl "C:\path\to\cacert.pem"
```

#### macOS/Linux
```bash
# Check permissions
ls -la /path/to/cacert.pem

# Fix permissions if needed
chmod 644 /path/to/cacert.pem
```

## Testing Your Configuration

Use the provided test script to verify your configuration:

```bash
php test_cross_platform_ssl.php
```

This will show:
- Detected operating system
- Found certificate paths
- Environment variables
- Platform-specific recommendations

## Production Considerations

1. **Always use proper SSL certificates in production**
2. **Don't disable SSL verification in production**
3. **Use system-provided certificate stores when possible**
4. **Set appropriate file permissions for certificate files**

## Environment Variables Reference

- `SLACK_CACERT_PATH`: Path to your SSL certificate file
- `LARAGON_ROOT`: Laragon installation directory (Windows)
- `XAMPP_ROOT`: XAMPP installation directory (Windows)
- `SSL_CERT_FILE`: System SSL certificate file
- `SSL_CERT_DIR`: System SSL certificate directory
- `CURL_CA_BUNDLE`: cURL certificate bundle path

## Automatic Fallbacks

The system provides these fallbacks in order:

1. User-defined `SLACK_CACERT_PATH` environment variable
2. Laravel storage directory (if available)
3. OS-specific development server paths
4. Current working directory
5. PHP's default certificate settings
6. Automatic download to user directory
7. SSL verification bypass (development only)

This ensures maximum compatibility across different environments while maintaining security best practices.
Loading