PHP code example of mb4it / filament-chatbot

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

    

mb4it / filament-chatbot example snippets


use MB\FilamentChatbot\ChatbotPlugin;

public function panel(Panel $panel): Panel
{
    return $panel->plugins([
        ChatbotPlugin::make(),
    ]);
}

use MB\FilamentChatbot\Bot\Fields\Field;
use MB\FilamentChatbot\Bot\Steps\StepContext;
use MB\FilamentChatbot\Bot\Steps\StepResult;
use MB\FilamentChatbot\Bot\Steps\StepType;
use MB\FilamentChatbot\Enums\StepOutputs;
use MB\FilamentChatbot\Models\RunStep;

final class AwardPointsStep extends StepType
{
    public static function key(): string
    {
        return 'shop.award_points';
    }

    public function label(): string
    {
        return 'Award loyalty points';
    }

    public function icon(): string
    {
        return 'heroicon-o-gift';
    }

    public function outputs(): StepOutputs
    {
        return StepOutputs::Pair;
    }

    public function pairLabels(): array
    {
        return ['awarded', 'could not'];
    }

    public function fields(): array
    {
        return [
            Field::number('points')->label('Points')->default(50)->min(1)->max(10000),
            Field::variable('into')->label('Remember the new balance as'),
        ];
    }

    public function run(StepContext $context): StepResult
    {
        $customer = $context->conversation->contact;

        if ($customer === null) {
            return StepResult::second()->traced(RunStep::FAILED, ['reason' => 'nobody to award']);
        }

        $balance = $customer->award((int) $context->setting('points', 0));

        $context->runtime->store((string) $context->setting('into', 'points'), (string) $balance);

        return StepResult::next()->traced(RunStep::PASSED, ['balance' => $balance]);
    }
}

use MB\FilamentChatbot\Facades\Chatbot;

public function boot(): void
{
    Chatbot::registerStep(AwardPointsStep::class);
}

public function run(StepContext $context): StepResult
{
    return StepResult::wait()->traced(RunStep::WAITING);
}

public function resume(StepContext $context, string $answer): StepResult
{
    $context->runtime->store('order_number', $answer);

    return StepResult::next()->traced(RunStep::ANSWERED);
}

use MB\FilamentChatbot\Capabilities\CapabilityAction;
use MB\FilamentChatbot\Enums\SideEffect;
use MB\FilamentChatbot\Facades\Chatbot;

Chatbot::registerCapability(new CapabilityAction(
    key: 'shop.open_ticket',
    version: '1',
    label: 'Open a support ticket',
    description: 'Creates a ticket and returns its reference.',
    effect: SideEffect::Write,
    input: [
        Field::text('subject')->label('Subject')->

use MB\FilamentChatbot\Bot\Slots\Slot;
use MB\FilamentChatbot\Facades\Chatbot;

Chatbot::registerSlot(
    Slot::date('appointment_at')
        ->label('Appointment date')
        ->question('Which day suits you?')
        ->group('booking'),
);

use MB\FilamentChatbot\Bot\Conditions\ConditionField;
use MB\FilamentChatbot\Bot\Conditions\ConditionKind;

final class HasUnpaidInvoice extends ConditionField
{
    public static function key(): string
    {
        return 'shop.has_unpaid_invoice';
    }

    public function label(): string
    {
        return 'Has an unpaid invoice';
    }

    public function kind(): ConditionKind
    {
        return ConditionKind::Flag;
    }

    public function evaluate(Conversation $conversation, string $operator, mixed $value, Runtime $runtime): bool
    {
        return $conversation->contact?->invoices()->unpaid()->exists() ?? false;
    }
}
bash
php artisan vendor:publish --tag=chatbot-config