PHP code example of danlopez00 / chatbase-api

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

    

danlopez00 / chatbase-api example snippets



// API KEY and default platform must be set
$apiKey = '12345-12345-12345';
$platform = 'messenger';

// Instantiate a new Chatbase client with the dependency of Guzzle
$client = new \Richdynamix\Chatbase\Service\ChatbaseClient(new \GuzzleHttp\Client());

// Instantiate a new FieldManager with the dependency of the API KEY and default platform
$fieldsManager = new \Richdynamix\Chatbase\Entities\FieldsManager($apiKey, $platform);

// Instantiate a new GenericMessage with the dependency of the Chatbase client and the FieldsManager
$chatbase = new \Richdynamix\Chatbase\Service\GenericMessage($client, $fieldsManager);

// send a user message to chatbase
$send = $chatbase->userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send();


// config/app.php
'providers' => [
    ...
    Richdynamix\Chatbase\ChatbaseServiceProvider::class,
    ...
];

// config/app.php
'aliases' => [
    ...
    'Chatbase' => Richdynamix\Chatbase\Facades\Chatbase::class,
    ...
];

return [
    /*
     * The Chatbase API key
     */
    'api_key' => env('CHATBASE_API_KEY'),
    'platform' => env('CHATBASE_DEFAULT_PLATFORM', 'messenger'),
];

// Directly from the IoC
$chatbase = app(Richdynamix\Chatbase\Contracts\GenericMessage::class);

// From a constructor
class FooClass {
    public function __construct(Richdynamix\Chatbase\Contracts\GenericMessage $chatbase) {
       // . . .
    }
}

// From a method
class BarClass {
    public function barMethod(Richdynamix\Chatbase\Contracts\GenericMessage $chatbase) {
       // . . .
    }
}

    Chatbase::userMessage()->with($data)->send();

    Chatbase::notHandledUserMessage()->with($data)->send();

    Chatbase::botMessage()->with($data)->send();

$userId, $message, $intent, $version, $customSessionID

    $send = $chatbase->userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send();
    
    // With Facade
    $send = Chatbase::userMessage()->with(['user_id => '12345', 'message' => 'hello'])->send();
    
    // With helper setters
    $send = Chatbase::userMessage()->withUserId('12345')->withMessage('hello')->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "2981752682"
      +"status": 200
    }

    $send = $chatbase->userMessage()->with([
        'user_id => '12345',
        'message' => 'hello',
        'platform' => 'slack'
    ])->send();
    
    // With Facade
    $send = Chatbase::userMessage()->with([
        'user_id => '12345',
        'message' => 'hello',
        'platform' => 'slack'
    ])->send();
    
    // With helper setters
    $send = Chatbase::userMessage()
        ->setPlatform('slack')
        ->withUserId('12345')
        ->withMessage('hello')
        ->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "2981752682"
      +"status": 200
    }

    $send = $chatbase->userMessage()->with([
            'user_id => '12345',
            'message' => 'hello',
            'version' => '1.2.1'
        ])->send();
        
        
    // With Facade
    $send = Chatbase::userMessage()->with([
        'user_id => '12345',
        'message' => 'hello',
        'version' => '1.2.1'
    ])->send();
    
    
    // With helper setters
    $send = Chatbase::userMessage()
        ->withUserId('12345')
        ->withMessage('hello')
        ->withVersion('1.2.1')
        ->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "2981752682"
      +"status": 200
    }

    $send = $chatbase->userMessage()->with([
        'user_id => '12345',
        'message' => 'hello',
        'intent' => 'hotel-booking'
    ])->send();
        
        
    // With Facade
    $send = Chatbase::userMessage()->with([
        'user_id => '12345',
        'message' => 'hello',
        'intent' => 'hotel-booking'
    ])->send();
    
    
    // With helper setters
    $send = Chatbase::userMessage()
        ->withUserId('12345')
        ->withMessage('hello')
        ->withIntent('hotel-booking')
        ->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "2981752682"
      +"status": 200
    }

    $send = $chatbase->notHandledUserMessage()
        ->with([
            'user_id => '12345',
            'message' => 'hello',
        ])
        ->send();
    
    // With Facade
    $send = Chatbase::notHandledUserMessage()
        ->with([
          'user_id => '12345',
          'message' => 'hello',
        ])
        ->send();
    
    // With helper setters
    $send = Chatbase::userMessage()
        ->withUserId('12345')
        ->withMessage('hello')
        ->withIntent('hotel-booking')
        ->send();
        
    dd($send);

    {#618 ▼
      +"message_id": "29817235682"
      +"status": 200
    }

    $send = $chatbase->botMessage()->with(['user_id => '12345','message' => 'hello'])->send();
    
    // With facade
    $send = Chatbase::botMessage()->with(['user_id => '12345','message' => 'hello'])->send();
    
    // With helper setters
    $send = Chatbase::botMessage()->withUserId('12345')->withMessage('hello')->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "29347235682"
      +"status": 200
    }

    $send = $chatbase->userMessage()->with([
        'api_key' => 'my-chatbase-api-key',
        'user_id => '12345',
        'message' => 'hello'
    ])->send();


    // With facade
    $send = Chatbase::userMessage()->with([
        'api_key' => 'my-chatbase-api-key',
        'user_id => '12345',
        'message' => 'hello'
    ])->send();
    
    
    // With helper setters
    $send = Chatbase::botMessage()->setApiKey('my-chatbase-api-key')->withUserId('12345')->withMessage('hello')->send();
    
    dd($send);

    {#618 ▼
      +"message_id": "2981752682"
      +"status": 200
    }
 bash
php artisan vendor:publish --provider="Richdynamix\Chatbase\ChatbaseServiceProvider"