PHP code example of olegf13 / jivochat-webhooks-api

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

    

olegf13 / jivochat-webhooks-api example snippets



use Olegf13\Jivochat\Webhooks\Log\MySQLLog;
use Olegf13\Jivochat\Webhooks\Event\Event;

// 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 Olegf13\Jivochat\Webhooks\EventListener([$dbLogger]);

// bind listener for `chat_accepted` event
$listener->on(Event::EVENT_CHAT_ACCEPTED, function (Olegf13\Jivochat\Webhooks\Event\ChatAccepted $event) {
    // here you do your stuff - find user in your database, etc
    $user = User::getByEmail($event->visitor->email);
    
    // generate response on Callback API
    $response = new Olegf13\Jivochat\Webhooks\Response();
    $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 (Olegf13\Jivochat\Webhooks\Event\ChatFinished $event) {
    /** @var int Timestamp of the chat's first message. */
    $chatBeginAt = $event->chat->messages[0]->timestamp;
    // ...
    
    return new Olegf13\Jivochat\Webhooks\Response();
});

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