PHP code example of rumenx / wp-chatbot

1. Go to this page and download the library: Download rumenx/wp-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/ */

    

rumenx / wp-chatbot example snippets


[wp_chatbot]

[wp_chatbot theme="dark" position="bottom-right" greeting="Hello! How can I help you?"]


if (function_exists('wp_chatbot_display')) {
    wp_chatbot_display([
        'theme' => 'light',
        'position' => 'bottom-left',
        'greeting' => 'Welcome! Ask me anything.'
    ]);
}

// Customize default settings
add_filter('wp_chatbot_default_settings', function($settings) {
    $settings['greeting_message'] = 'Hello! Welcome to our site!';
    $settings['theme'] = 'custom';
    return $settings;
});

// Add custom chat responses
add_filter('wp_chatbot_responses', function($responses, $message) {
    if (strpos(strtolower($message), 'price') !== false) {
        return 'Please contact our sales team for pricing information.';
    }
    return $responses;
}, 10, 2);

// Before chatbot initialization
do_action('wp_chatbot_before_init');

// After chatbot is loaded
do_action('wp_chatbot_loaded');

// When a message is sent
do_action('wp_chatbot_message_sent', $message, $user_id);

// When a response is generated
do_action('wp_chatbot_response_generated', $response, $message);

// Modify chatbot settings
apply_filters('wp_chatbot_settings', $settings);

// Customize chat messages
apply_filters('wp_chatbot_messages', $messages);

// Filter responses before sending
apply_filters('wp_chatbot_response', $response, $message, $context);

// Modify the chat UI
apply_filters('wp_chatbot_ui_config', $ui_config);

// In your theme's functions.php
function my_theme_chatbot_setup() {
    add_filter('wp_chatbot_settings', function($settings) {
        $settings['primary_color'] = get_theme_mod('primary_color', '#007cba');
        $settings['font_family'] = get_theme_mod('body_font', 'Arial, sans-serif');
        return $settings;
    });
}
add_action('after_setup_theme', 'my_theme_chatbot_setup');

// Add a custom command handler
add_filter('wp_chatbot_commands', function($commands) {
    $commands['hours'] = function() {
        return 'Our business hours are Monday-Friday, 9 AM - 6 PM EST.';
    };

    $commands['contact'] = function() {
        return 'You can reach us at [email protected] or call (555) 123-4567.';
    };

    return $commands;
});

// Add product search functionality
add_filter('wp_chatbot_responses', function($response, $message) {
    if (strpos(strtolower($message), 'search product') !== false) {
        $product_name = trim(str_ireplace('search product', '', $message));
        $products = wc_get_products([
            'search' => $product_name,
            'limit' => 1,
        ]);

        if (!empty($products)) {
            $product = $products[0];
            return sprintf(
                'I found this product: %s - %s. View it here: %s',
                $product->get_name(),
                wc_price($product->get_price()),
                $product->get_permalink()
            );
        }

        return 'Sorry, I couldn\'t find any products matching your search.';
    }

    return $response;
}, 10, 2);

     return [
         'provider' => 'openai',
         'model' => 'gpt-4o',
         'openai_api_key' => getenv('OPENAI_API_KEY'),
         'prompt' => 'You are a helpful assistant for a WordPress site.',
         // ...other options...
     ];
     

     $configPath = getenv('PHPCHATBOT_CONFIG_PATH') ?: WP_CONTENT_DIR . '/uploads/wp-chatbot-config.php';
     if (!file_exists($configPath)) {
         $configPath = WP_PLUGIN_DIR . '/wp-chatbot/config/phpchatbot.php';
     }
     if (!file_exists($configPath)) {
         $configPath = WP_PLUGIN_DIR . '/wp-chatbot/vendor/rumenx/php-chatbot/src/Config/phpchatbot.php';
     }
     $config = 

add_action('wp_ajax_wp_chatbot_message', 'my_wp_chatbot_message_handler');
add_action('wp_ajax_nopriv_wp_chatbot_message', 'my_wp_chatbot_message_handler');

function my_wp_chatbot_message_handler() {
    $configPath = getenv('PHPCHATBOT_CONFIG_PATH') ?: WP_CONTENT_DIR . '/uploads/wp-chatbot-config.php';
    if (!file_exists($configPath)) {
        $configPath = WP_PLUGIN_DIR . '/wp-chatbot/config/phpchatbot.php';
    }
    if (!file_exists($configPath)) {
        $configPath = WP_PLUGIN_DIR . '/wp-chatbot/vendor/rumenx/php-chatbot/src/Config/phpchatbot.php';
    }
    $config =