allcdnboy/textline

此包最新版本(dev-master)没有可用的许可证信息。

Textline API 的 PHP 客户端

dev-master 2023-09-13 21:56 UTC

This package is auto-updated.

Last update: 2024-09-14 00:27:12 UTC


README

设置

使用 composer

composer require allcdnboy/textline

用法

身份验证

在初始化客户端时,您必须提供您的 API 密钥(在此处获取:https://application.textline.com/organization/api_settings)以及您想由您的 API 请求代表的代理的电子邮件和密码。这将向 Textline API 发送请求以检索一个身份验证令牌,然后此令牌将自动附加到所有后续的 API 请求。如果您已经知道您的令牌,您可以将它作为第四个参数传递。

<?php
$apiKey = "ZZZZZZZZ"; // Your account api key
$email = "agent@acme.com"; // Email address of the agent
$password = "XXXXXX"; // Password associated with the email address above
$token = 'YYYYYYYY'; // Can be left null

$client = new Textline\Client($email, $password, $apiKey, $token);

如果 API 密钥和/或令牌不正确,将抛出 Textline\Exceptions\AuthenticationException

对话

列出对话

$client->conversations()
       ->get();

要添加查询参数,请参考 此处 的列表,传递键值数组

$query = [
    'page' => 1,
    'page_size' => 20,
    'query' => 'foo',
];

$client->conversations()
       ->get($query);

给电话号码发消息

$number = '0781234567';

use Textline\Resources\Message;

$message = new Message('foo');

// (optional) add attachment
$message->addAttachment('application/pdf','name-of-file.pdf', '/path/to/file');
                
$client->conversations()
       ->messageByPhone($number, $message);

有关 $body 选项的列表,请参阅 此处。注意,请求中必须指定 whispercomment 之一,但不能同时指定两者。

通过电话号码安排消息

$number = '07812345678';
$timestamp = 1551528788; # unix timestamp
$body = 'foo';
$params = [
    'group_uuid' => '123' # optional extra params
];

$client->conversations()
       ->scheduleByPhone($number, $timestamp, $body, $params);

对话

检索对话

$uuid = 'abc-123';

$client->conversation($uuid)
       ->retrieve();

要添加查询参数,请参考 此处 的列表,将键值数组作为第二个参数传递。

向对话发送消息

将对话 uuid 作为第一个参数传递,将消息属性作为第二个参数传递

$uuid = 'abc-123';
$body = [
    'comment' => [
        'body' => 'foo',
    ],
    'whisper' => [
        'body' => 'bar',
    ]
];

$client->conversation($uuid)
       ->message($body);

向对话安排消息

$uuid = 'abc-123';
$timestamp = 1551528788; # unix timestamp
$body = 'foo';

$client->conversation($uuid)
       ->scheduleMessage($timestamp, $body);

解决对话

$uuid = 'abd-123';

$client->conversation($uuid)
       ->resolve();

转移对话

$uuid = 'abd-123';

$client->conversation($uuid)
       ->transfer();

客户

列出客户

$client->customers()
       ->get();

要添加查询参数,请参考 此处 的列表,传递键值数组

$query = [
    'page' => 1,
    'page_size' => 20,
    'query' => 'foo',
];

$client->customers()
       ->get($query);

创建客户

$number = '07812345678';
$attrs = [
    'email' => 'john@mail.com',
    'name' => 'John Mail',
];

$client->customers()
       ->create($number, $attrs);

有关完整属性列表,请参阅 此处

客户

检索客户

$uuid = 'abc-123';

$client->customer($uuid)
       ->retrieve();

更新客户

$uuid = 'abc-123';
$attrs = [
    'name' => 'John Smith',
    'email' => 'john@smith.com',
];

$client->customer($uuid)
       ->update($attrs);

组织

检索组织详细信息

$query = [
    'include_groups' => false,
];

$client->orgnization()
       ->get($query);

有关完整查询选项列表,请参阅 此处

异常

  • 如果找不到使用 uuid 标识的对话、客户或其他资源,将抛出 Textline\Exceptions\ResourceNotFoundException

  • 如果超出配额限制,将抛出 Textline\Exceptions\RateLimitException

  • 对于所有其他客户端错误,将抛出通用的 Textline\Exceptions\ClientException