codebar-ag/laravel-zendesk

Zendesk integration with Laravel

v2.0 2024-02-07 13:10 UTC

This package is auto-updated.

Last update: 2024-05-15 15:51:00 UTC


README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532305a656e6465736b2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d636f64656261722d61672532466c61726176656c2d7a656e6465736b267061747465726e3d63697263756974426f617264267374796c653d7374796c655f32266465736372697074696f6e3d412b4c61726176656c2b5a656e6465736b2b696e746567726174696f6e2e266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313530707826696d616765733d686f6d65267769647468733d35303026686569676874733d353030

Latest Version on Packagist Total Downloads run-tests PHPStan

This package was developed to give you a quick start to creating tickets via the Zendesk API.

💡 What is Zendesk?

Zendesk is a cloud-based help desk management solution offering customizable tools to build customer service portals, knowledge base and online communities.

🛠 Requirements

Package PHP Laravel Zendesk
>v1.0 >8.2 > Laravel 10.0

Authentication

The currently supported authentication methods are:

Method Supported
Basic Auth
API token
OAuth access token

⚙️ Installation

You can install the package via composer:

composer require codebar-ag/laravel-zendesk

Optionally, you can publish the config file with:

php artisan vendor:publish --provider="CodebarAg\Zendesk\ZendeskServiceProvider" --tag="config"

You can add the following env variables to your .env file:

ZENDESK_SUBDOMAIN=your-subdomain #required
ZENDESK_AUTHENTICATION_METHOD=token #default ['basic', 'token']
ZENDESK_EMAIL_ADDRESS=test@example.com #required
ZENDESK_API_TOKEN=your-api-token #required only for token authentication
ZENDESK_API_PASSWORD=your-password #required only for basic authentication

Note: We handle base64 encoding for you so you don't have to encode your credentials.

You can retrieve your API token from your Zendesk Dashboard

Usage

To use the package, you need to create a ZendeskConnector instance.

use CodebarAg\Zendesk\ZendeskConnector;
...

$connector = new ZendeskConnector();

Requests

The following requests are currently supported:

Request Supported
List Tickets
Count Tickets
Show Ticket
Create Ticket
Create Attachment

Responses

The following responses are currently supported for retrieving the response body:

Response Methods Description Supported
body Returns the HTTP body as a string
json Retrieves a JSON response body and json_decodes it into an array.
object Retrieves a JSON response body and json_decodes it into an object.
collect Retrieves a JSON response body and json_decodes it into a Laravel collection. Requires illuminate/collections to be installed.
dto Converts the response into a data-transfer object. You must define your DTO first

See https://docs.saloon.dev/the-basics/responses for more information.

Enums

We provide enums for the following values:

Enum Values
TicketPriority 'urgent', 'high', 'normal', 'low'
TicketType 'incident', 'problem', 'question', 'task'
MalwareScanResult 'malware_found', 'malware_not_found', 'failed_to_scan', 'not_scanned'

Note: When using the dto method on a response, the enum values will be converted to their respective enum class.

DTOs

We provide DTOs for the following:

DTO
AttachmentDTO
ThumbnailDTO
UploadDTO
CommentDTO
AllTicketsDTO
CountTicketsDTO
SingleTicketDTO

Note: This is the prefered method of interfacing with Requests and Responses however you can still use the json, object and collect methods. and pass arrays to the requests.

Examples

Create a ticket

use CodebarAg\Zendesk\Requests\CreateSingleTicketRequest;
use CodebarAg\Zendesk\DTOs\SingleTicketDTO;
use CodebarAg\Zendesk\DTOs\CommentDTO;
use CodebarAg\Zendesk\Enums\TicketPriority;
...

$ticketResponse = $connector->send(
    new CreateSingleTicketRequest(
        SingleTicketDTO::fromArray([
            'comment' => CommentDTO::fromArray([
                'body' => 'The smoke is very colorful.',
            ]),
            'priority' => TicketPriority::URGENT,
            "subject" => "My printer is on fire!",
            "custom_fields" => [
                [
                    "id" => 12345678910111,
                    "value" => "Your custom field value"
                ],
                [
                    "id" => 12345678910112,
                    "value" => "Your custom field value 2"
                ],
            ],
        ])
    )
);

$ticket = $ticketResponse->dto();

List all tickets

use CodebarAg\Zendesk\Requests\AllTicketsRequest;
...

$listTicketResponse = $connector->send(new AllTicketsRequest());
$listTicketResponse->dto();

Count all tickets

use CodebarAg\Zendesk\Requests\CountTicketsRequest;
...

$countTicketResponse = $connector->send(new CountTicketsRequest());
$countTicketResponse->dto();

Show a ticket

use CodebarAg\Zendesk\Requests\ShowTicketRequest;
...

$ticketID = 1;

$showTicketResponse = $connector->send(new ShowTicketRequest($ticketID));
$showTicketResponse->dto();

Upload an attachment

use CodebarAg\Zendesk\Requests\CreateAttachmentRequest;
use CodebarAg\Zendesk\Requests\CreateSingleTicketRequest;
use Illuminate\Support\Facades\Storage;

$uploadResponse = $connector->send(
    new CreateAttachmentRequest(
        fileName: 'someimage.png',
        mimeType: Storage::disk('local')->mimeType('public/someimage.png'),
        stream: Storage::disk('local')->readStream('public/someimage.png')
    )
);

$token = $uploadResponse->dto()->token;

$ticketResponse = $connector->send(
    new CreateSingleTicketRequest(
        SingleTicketDTO::fromArray([
            'comment' => CommentDTO::fromArray([
                ...
                'uploads' => [
                    $token,
                ],
            ]),
        ])
    )
);

$ticket = $ticketResponse->dto();

🚧 Testing

Copy your own phpunit.xml-file.

cp phpunit.xml.dist phpunit.xml

Run the tests:

./vendor/bin/pest

📝 Changelog

Please see CHANGELOG for recent changes.

✏️ Contributing

Please see CONTRIBUTING for details.

composer test

Code Style

./vendor/bin/pint

🧑‍💻 Security Vulnerabilities

Please review our security policy on reporting security vulnerabilities.

🙏 Credits

🎭 License

The MIT License (MIT). Please have a look at License File for more information.