shapin/talkjs

该软件包已被放弃,不再维护。作者建议使用 carandclassic/talkjs 软件包。

talk.js API 的 PHP 客户端

v2.1.0 2020-01-16 12:07 UTC

This package is auto-updated.

Last update: 2021-07-23 12:04:25 UTC


README

Latest version Build status Total downloads

注意:此软件包未维护。有关进一步更新,请参阅 carandclassic/talkjs

安装

通过 Composer

$ composer require shapin/talkjs

用法

创建 TalkJSClient

use Shapin\TalkJS\TalkJSClient;
use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create([
    'base_uri' => 'https://api.talkjs.com/v1/'.self::APP_ID.'/',
    'auth_bearer' => self::SECRET_KEY,
    'headers' => [
        'Content-Type' => 'application/json',
    ],
]);

$talkJSClient = new TalkJSClient($httpClient);

标识符

TalkJS 用户和会话的标识符是自定义的,由您的应用程序管理。

过滤

所有获取多个记录(用户、会话、消息)的端点都有限制和分页选项。下面的 API 用法将尽可能使用 $filters 变量进行演示,其形式如下

$filters = [
    'limit' => 50,
    'startingAfter' => 'latestMessageId'
];

用户

请注意,TalkJS 目前不提供用户删除 API,而是建议您使用更新/编辑端点来匿名化个人可识别信息。了解更多

// Create or update a user
$client->users()->createOrUpdate('my_custom_id', [
    'email' => 'georges@abitbol.com',
]);

// Retrieve a user
$user = $client->users()->get('my_custom_id');

会话

// Create or update a conversation
$client->conversations()->createOrUpdate('my_custom_id', [
    'subject' => 'My new conversation',
    'participants' => ['my_user_id_1', 'my_user_id_2'],
    'welcomeMessages' => ['Welcome!'],
    'custom' => ['test' => 'test'],
    'photoUrl' => null
]);

// Retrieve a conversation
$conversation = $client->conversations()->get('my_custom_id');

// Find conversations
$conversations = $client->conversations()->find($filters);

// Join a conversation
$client->conversation()->join('my_conversation_id', 'my_user_id');

// Leave a conversation
$client->conversation()->leave('my_conversation_id', 'my_user_id');

// Update participation settings
$params = [
  'notify' => true, // Boolean, default true
  'access' => 'ReadWrite' // ReadWrite or Read, default ReadWrite 
];
$client->conversations()->updateParticipation('my_conversation_id', 'my_user_id', $params);

消息

有关自定义数据和过滤器的更多信息,请参阅上面链接的 TalkJS 文档。

请注意

  • 尚未实现发送文件附件。
  • 返回多个消息的端点将按降序返回,即最新消息先显示。
$custom = [
  'foo' => 'bar'
];
// Find Messages
$client->conversations()->findMessages('my_conversation_id', $filters);

// Post a system message
$client->conversations()->postSystemMessage('my_conversation_id', 'message_text', $custom);

// Post a user message
$client->conversations()->postUserMessage('my_conversation_id', 'my_user_id', 'message_text', $custom);

Symfony 集成

创建一个新的 HttpClient

framework:
    http_client:
        scoped_clients:
            talkjs.client:
                auth_bearer: '%env(TALKJS_SECRET_KEY)%'
                base_uri: 'https://api.talkjs.com/v1/%env(TALKJS_APP_ID)%/'
                headers:
                    'Content-Type': 'application/json'

然后创建您的服务

services:
    Shapin\TalkJS\TalkJSClient: ~

完成!

有一天,我可能考虑创建一个捆绑包来启动此 SDK...

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件