PHP code example of chezrd / jivochat-webhooks-api

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

    

chezrd / jivochat-webhooks-api example snippets




use ChezRD\Jivochat\Webhooks\Log\MySQLLog;
use ChezRD\Jivochat\Webhooks\Event;
use ChezRD\Jivochat\Webhooks\EventListener;
use ChezRD\Jivochat\Webhooks\Model\EventRequest\ChatAcceptedRequest;
use ChezRD\Jivochat\Webhooks\Model\EventRequest\ChatFinishedRequest;
use ChezRD\Jivochat\Webhooks\Response;
use ChezRD\Jivochat\Webhooks\Response\SuccessResponse;
use ChezRD\Jivochat\Webhooks\Response\UpdateResponse;

// create MySQL logger
$dbLogger = new MySQLLog(new PDO('mysql:dbname=test;host=127.0.0.1', 'root', 'root'));

// create Callback API event listener
$listener = new EventListener([$dbLogger]);

// bind listener for `chat_accepted` event
$listener->on(Event::EVENT_CHAT_ACCEPTED, function (ChatAcceptedRequest $request): Response {
    // here you do your stuff - find user in your database, etc
    $user = User::getByEmail($request->visitor->email);
    
    // generate response on Callback API
    $response = new UpdateResponse();
    $response->setCRMLink(...);
    $response->setContactInfo(...);
    $response->setCustomData(...);
    
    // event handler must return Response object
    return $response;
});

// bind listener for `chat_accepted` event
$listener->on(Event::EVENT_CHAT_FINISHED, function (ChatFinishedRequest $request): Response {
    /** @var int Timestamp of the chat's first message. */
    $chatBeginAt = $request->chat->messages[0]->timestamp;
    // ...
    
    return new SuccessResponse();
});

// execute event listener
$listener->listen();