1. Go to this page and download the library: Download tusharkhan/chatbot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tusharkhan / chatbot example snippets
TusharKhan\Chatbot\Core\Bot;
use TusharKhan\Chatbot\Drivers\WebDriver;
use TusharKhan\Chatbot\Storage\FileStore;
// Initialize bot
$driver = new WebDriver();
$storage = new FileStore(__DIR__ . '/storage');
$bot = new Bot($driver, $storage);
// Add message handlers
$bot->hears('hello', function($context) {
return 'Hello! How can I help you?';
});
$bot->hears('my name is {name}', function($context) {
$name = $context->getParam('name');
$context->getConversation()->set('name', $name);
return "Nice to meet you, $name!";
});
// Set fallback handler
$bot->fallback(function($context) {
return "I don't understand. Try saying 'hello'.";
});
// Listen for messages
$bot->listen();
// Output responses
$driver->outputJson(); // For AJAX
// or $driver->outputHtml(); // For form submissions
use TusharKhan\Chatbot\Core\Bot;
use TusharKhan\Chatbot\Drivers\TelegramDriver;
use TusharKhan\Chatbot\Storage\FileStore;
// Configuration
$botToken = $_ENV['TELEGRAM_BOT_TOKEN'] ?? 'your-telegram-bot-token-here';
// Initialize bot
$driver = new TelegramDriver($botToken);
$storage = new FileStore(__DIR__ . '/storage');
$bot = new Bot($driver, $storage);
// Handle /start command
$bot->hears(['/start', 'start'], function($context) {
$name = $context->getData()['from']['first_name'] ?? 'there';
return "Welcome to our bot, $name! 🤖\n\nType /help to see what I can do.";
});
// Handle /help command
$bot->hears(['/help', 'help'], function($context) {
return "🤖 *Bot Commands:*\n\n• /start - Start the bot\n• /help - Show this help\n• order - Start food ordering";
});
// Start food ordering
$bot->hears(['order', '/order'], function($context) {
$context->getConversation()->setState('ordering_category');
return "🍽️ *Food Ordering*\n\nWhat would you like?\n• Pizza 🍕\n• Burger 🍔\n• Salad 🥗";
});
$bot->listen();
use TusharKhan\Chatbot\Core\Bot;
use TusharKhan\Chatbot\Drivers\SlackDriver;
use TusharKhan\Chatbot\Storage\FileStore;
// Configuration
$botToken = $_ENV['SLACK_BOT_TOKEN'] ?? 'xoxb-your-bot-token-here';
$signingSecret = $_ENV['SLACK_SIGNING_SECRET'] ?? 'your-signing-secret-here';
// Initialize bot
$driver = new SlackDriver($botToken, $signingSecret);
$storage = new FileStore(__DIR__ . '/storage');
$bot = new Bot($driver, $storage);
$bot->hears('hello', function($context) {
return 'Hello! 👋 How can I help you today?';
});
// Handle slash commands
$bot->hears('/weather {city}', function($context) {
$city = $context->getParam('city');
return "Weather for {$city}: 22°C, Sunny ☀️";
});
$bot->listen();
$bot->hears('hello', $handler);
$bot->hears('hello*', $handler); // Matches "hello world", "hello there", etc.
$bot->hears('my name is {name}', function($context) {
$name = $context->getParam('name');
return "Hello, $name!";
});
$bot->hears(['hello', 'hi', 'hey'], $handler);
$bot->hears('/^\d+$/', $handler); // Matches numbers only
use TusharKhan\Chatbot\Storage\FileStore;
$storage = new FileStore('/path/to/storage/directory');
$bot = new Bot($driver, $storage);
use TusharKhan\Chatbot\Storage\ArrayStore;
$storage = new ArrayStore();
$bot = new Bot($driver, $storage);
use TusharKhan\Chatbot\Contracts\StorageInterface;
class DatabaseStore implements StorageInterface
{
public function store(string $key, array $data): bool { /* ... */ }
public function retrieve(string $key): ?array { /* ... */ }
public function exists(string $key): bool { /* ... */ }
public function delete(string $key): bool { /* ... */ }
}
// In a Laravel Controller
use TusharKhan\Chatbot\Core\Bot;
use TusharKhan\Chatbot\Drivers\WebDriver;
class ChatbotController extends Controller
{
public function handle(Request $request)
{
$bot = new Bot(new WebDriver(), new FileStore(storage_path('chatbot')));
$bot->hears('hello', function($context) {
return 'Hello from Laravel!';
});
$bot->listen();
return response()->json([
'responses' => $bot->getDriver()->getResponses()
]);
}
}
// For AJAX requests
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) {
$driver->outputJson();
} else {
$driver->outputHtml();
}
use TusharKhan\Chatbot\Contracts\DriverInterface;
class CustomDriver implements DriverInterface
{
public function getMessage(): ?string { /* ... */ }
public function getSenderId(): ?string { /* ... */ }
public function sendMessage(string $message, ?string $senderId = null): bool { /* ... */ }
public function getData(): array { /* ... */ }
public function hasMessage(): bool { /* ... */ }
}