olegf13/jivochat-webhooks-api

Jivochat (Jivosite) Webhooks API 集成库。

1.1 2017-04-27 11:45 UTC

This package is not auto-updated.

Last update: 2024-09-20 22:43:18 UTC


README

Latest Stable Version License

Jivochat (Jivosite) Webhooks API 集成库。

此库允许您与 Jivosite Webhooks API 集成,并

  • 基于事件处理 API 调用;
  • 将 API 请求 JSON 数据转换为特定事件对象;
  • 生成 API 响应;
  • 将原始请求(和生成的响应)数据保存到 MySQL 或 MongoDB 服务器,并通过 Monolog 记录日志。

有关俄语文档,请参阅 README-ru.md

要求

该库需要 PHP 7.0 或更高版本进行基本使用。

可选要求

  • PDO 扩展 允许将 Webhooks 请求/响应数据记录到 MySQL 服务器;
  • Monolog 库 允许使用 Monolog 记录 Webhooks 请求/响应;
  • MongoDB 库 允许将 Webhooks 请求/响应数据记录到 MongoDB 服务器。

强烈建议安装上述至少一种日志记录器,以保留通过 Webhooks API 发送的原始请求的“备份”。

安装

安装此库的首选方式是通过 Composer。要安装最新版本,运行

composer require olegf13/jivochat-webhooks-api

基本使用

<?php
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();

文档

许可

此库受 MIT 许可证许可 - 请参阅 LICENSE 文件以获取详细信息。

致谢

感谢 Jivosite Webhook 处理器库。