PHP code example of three_oh_eight / laravel-chaosdesk

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

    

three_oh_eight / laravel-chaosdesk example snippets


use ThreeOhEight\ChaosDesk\Contracts\ProvidesSupportContext;

class User extends Authenticatable implements ProvidesSupportContext
{
    public function supportExternalId(): string
    {
        return (string) $this->id;
    }

    public function supportContext(): array
    {
        return ['plan' => $this->subscription?->plan_name];
    }
}

// routes/api.php
use ThreeOhEight\ChaosDesk\Facades\ChaosDesk;

Route::middleware('auth:sanctum')->group(function () {
    ChaosDesk::routes();
});

use ThreeOhEight\ChaosDesk\Facades\ChaosDesk;

$result = ChaosDesk::createTicket([
    'email' => '[email protected]',
    'subject' => 'Cannot save my settings',
    'message' => 'It reverts every time.',
]);

$ulid = $result['ticket']['ulid'];
$accessToken = $result['access_token'];

ChaosDesk::ticket($ulid, $accessToken);
ChaosDesk::reply($ulid, $accessToken, 'Any news?');

ChaosDesk::reply($ulid, $accessToken, $body, idempotencyKey: "support-reply:{$message->id}");

ChaosDesk::createTicket([
    'subject' => 'Call dropped halfway',
    'message' => 'The audio cut out after ten minutes.',
    'tags' => ['in-session', 'complaint'],
], clientContext: [
    'extra' => ['chat_id' => 12, 'channel' => 'call', 'duration_seconds' => 612],
], user: $request->user());

$this->app->bind(TicketStore::class, YourTicketStore::class);

// config/chaosdesk.php
'sites' => [
    'customers' => [
        'token' => env('CHAOSDESK_SITE_TOKEN_CUSTOMERS'),
        'agent_token' => env('CHAOSDESK_AGENT_TOKEN_CUSTOMERS'),
        'site_id' => 2,
    ],
    'gurus' => [
        'token' => env('CHAOSDESK_SITE_TOKEN_GURUS'),
        'agent_token' => env('CHAOSDESK_AGENT_TOKEN_GURUS'),
        'site_id' => 3,
    ],
],

$result = ChaosDesk::forSite('gurus')->createTicket([...], user: $user);

ChaosDesk::forSite('gurus')->site(); // "gurus"

$agent = ChaosDesk::agent('customers');   // or ChaosDesk::agent() for the default site

$page = $agent->tickets(['status' => ['open', 'pending'], 'tag' => 'complaint'], page: 2);
$ticket = $agent->ticket('01JABCDEFGHIJKLMNOPQRSTUVW');
$agent->update($ticket['ulid'], ['status' => 'pending', 'assigned_to' => 3, 'tags' => ['complaint']]);
$agent->reply($ticket['ulid'], 'We are looking into it.', agentName: $request->user()->name);
$agent->note($ticket['ulid'], 'Checked the billing log.', agentName: $request->user()->name);
$agent->anonymiseAuthor('user:7', siteId: 2);
$agents = $agent->agents(2);
$categories = $agent->categories(2);
$priorities = $agent->priorities(2);
$sites = $agent->sites();

use ThreeOhEight\ChaosDesk\Facades\ChaosDesk;

$boards = ChaosDesk::community()->boards();              // the site's active boards, list<Board>

$community = ChaosDesk::forSite('gurus')->community()->as($request->user());

use ThreeOhEight\ChaosDesk\Community\Member;

$community = ChaosDesk::community()->as(new Member(
    externalId: (string) $user->id,
    name: $user->display_name,
    email: $user->email,
    locale: 'nl',
));

use ThreeOhEight\ChaosDesk\Community\ThreadKind;

$board = $community->board('gurus');

if ($board->needsCharterAcceptance()) {
    // show $board->charter->markdown, then:
    $community->acceptCharter('gurus', $board->charter->version);
}

$page = $community->threads('gurus', ['kind' => ThreadKind::Proposal, 'sort' => 'votes']);

foreach ($page as $thread) {
    echo $thread->title.' ('.$thread->votesCount.')';
}

$thread = $community->createThread('gurus', ThreadKind::Proposal, 'Export to CSV', 'It would save me an hour a month.');
$community->vote('gurus', $thread->ulid);
$community->reply('gurus', $thread->ulid, 'Same here.');

$poll = $community->polls('gurus')->items[0];
$community->respond('gurus', $poll->ulid, [$poll->options[0]]);

use ThreeOhEight\ChaosDesk\Exceptions\CommunityException;

try {
    $community->reply('gurus', $ulid, $body);
} catch (CommunityException $e) {
    if ($e->

'components' => [
    // ...
    'community_board' => 'chaosdesk-community-board',
    'community_thread' => 'chaosdesk-community-thread',
    'community_new_thread' => 'chaosdesk-community-new-thread',
    'community_polls' => 'chaosdesk-community-polls',
    'community_charter' => 'chaosdesk-community-charter',
],

'community' => [
    'routes' => [
        'board' => 'guru.community',
        'thread' => 'guru.community.thread',
        'new_thread' => null,       // inline
        'polls' => null,            // inline
    ],

    'per_page' => 20,               // threads per page, at most 50
],

use ThreeOhEight\ChaosDesk\Community\CommunityUrls;

CommunityUrls::resolveUsing(fn (string $page, string $board, ?string $thread, ?string $site): ?string => match ($page) {
    CommunityUrls::BOARD => url("/community/{$board}"),
    CommunityUrls::THREAD => url("/community/{$board}/threads/{$thread}"),
    default => null,
});

// routes/web.php
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;

Gate::define('community.gurus', fn ($user): bool => $user->isGuru());
Gate::define('community.clients', fn ($user): bool => $user->isClient());

Route::middleware(['auth', 'can:community.gurus'])->group(function (): void {
    Route::view('/guru/community', 'community.board', ['board' => 'gurus'])->name('guru.community');
    Route::get('/guru/community/{thread}', fn (string $thread) => view('community.thread', ['board' => 'gurus', 'thread' => $thread]))
        ->name('guru.community.thread');
});

Route::middleware(['auth', 'can:community.clients'])->group(function (): void {
    Route::view('/account/community', 'community.board', ['board' => 'clients'])->name('account.community');
});

use ThreeOhEight\ChaosDesk\Webhooks\Signature;
use ThreeOhEight\ChaosDesk\Webhooks\WebhookEvent;

class ChaosDeskWebhookController
{
    public function __invoke(Request $request): Response
    {
        $secret = (string) config('services.chaosdesk.webhook_secret');

        abort_unless(Signature::verifyRequest($request, $secret), 401);

        $delivery = (string) $request->header('X-ChaosDesk-Delivery');

        if (Cache::add("chaosdesk-delivery:{$delivery}", true, now()->addDay()) === false) {
            return response()->noContent(); // already handled
        }

        match ($request->header(WebhookEvent::HEADER)) {
            WebhookEvent::TICKET_REPLIED => ...,
            WebhookEvent::TICKET_STATUS_CHANGED => ...,
            WebhookEvent::COMMUNITY_POST_CREATED => ..., // metadata.board_slug names the board
            default => null,
        };

        return response()->noContent();
    }
}
bash
composer migrate
bash
php artisan vendor:publish --tag=chaosdesk-views
php artisan vendor:publish --tag=chaosdesk-lang
blade
{{-- resources/views/community/board.blade.php --}}
<x-layouts.app>
    <livewire:chaosdesk-community-board :board="$board" />
</x-layouts.app>

{{-- resources/views/community/thread.blade.php --}}
<x-layouts.app>
    <livewire:chaosdesk-community-thread :board="$board" :thread="$thread" />
</x-layouts.app>