serafim/gitter-api

Gitter 异步 API

4.0.8 2018-09-01 23:04 UTC

README

Latest Stable Version https://travis-ci.org/SerafimArts/gitter-api Scrutinizer Code Quality License Total Downloads

安装

composer require serafim/gitter-api

创建 Gitter 客户端

use Gitter\Client;

$client = new Client($token); 
// OR
$client = new Client($token, $logger); // $logger are instance of \Psr\Log\LoggerInterface

// ... SOME ACTIONS ...

$client->connect(); // Locks current runtime and starts an EventLoop (for streaming requests)

资源

// $client = new \Gitter\Client('token');

$client->groups;   // Groups resource (Traversable)
$client->messages; // Messages resource
$client->rooms;    // Rooms resource (Traversable)
$client->users;    // Users resource

示例

foreach ($client->rooms as $room) {
    var_dump($room);
}

流式传输

$observer = $client->rooms->messages('roomId'); // Observer

$observer->subscribe(function ($message) {
    var_dump($message);
});

// Connect to stream!
$client->connect();

可用资源

列出当前用户所在的组。

  • $client->groups->all(): array

列出指定组下嵌套的房间。

  • $client->groups->rooms(string $roomId): array

消息

按历史顺序反向排列的房间中的消息列表。

  • $client->messages->all(string $roomId[, string $query]): \Generator

也可以通过其 ID 获取单个消息。

  • $client->messages->find(string $roomId, string $messageId): array

向房间发送消息。

  • $client->messages->create(string $roomId, string $content): array

更新消息。

  • $client->messages->update(string $roomId, string $messageId, string $content): array

删除消息。

  • $client->messages->delete(string $roomId, string $messageId): array

房间

列出当前用户所在的房间。

  • $client->rooms->all([string $query]): array

将用户加入房间。

  • $client->rooms->joinUser(string $roomId, string $userId): array

将当前用户加入房间。

  • $client->rooms->join(string $roomId): array

通过房间名称(URI)将当前用户加入房间。

  • $client->rooms->joinByName(string $name): array

通过房间名称(URI)查找房间。

  • $client->rooms->findByName(string $name): array

从目标房间踢出用户。

  • $client->rooms->kick(string $roomId, string $userId): array

这也可以是自己对自己的操作,以离开房间并从左侧菜单中删除房间。

  • $client->rooms->leave(string $roomId): array

设置目标房间的新的主题。

  • $client->rooms->topic(string $roomId, string $topic): array

设置房间由搜索引擎索引。

  • $client->rooms->searchIndex(string $roomId, bool $enabled): array

设置定义房间的标签。

  • $client->rooms->tags(string $roomId, array $tags): array

如果你讨厌某个房间 - 你可以摧毁它!

  • $client->rooms->delete(string $roomId): array

列出当前房间中的用户列表。

  • $client->rooms->users(string $roomId[, string $query]: \Generator

使用流式 API 监听事件。

  • $client->rooms->events(string $roomId): Observer

使用流式 API 监听消息。

  • $client->rooms->messages(string $roomId): Observer

用户

返回当前登录的用户。

  • $client->users->current(): array
  • $client->users->currentUserId(): string

列出用户所在的房间列表。

  • $client->users->rooms([string $userId]): array

您可以使用以下端点检索未读项和提及。

  • $client->users->unreadItems(string $roomId[, string $userId]): array

在房间下还有一个嵌套的端点,您可以使用它来将聊天消息标记为已读。

  • $client->users->markAsRead(string $roomId, array $messageIds[, string $userId]): array

列出用户的 GitHub 组织及其相应的房间(如果可用)。

  • $client->users->orgs([string $userId]): array

列出用户的 GitHub 仓库及其相应的房间(如果可用)。

  • $client->users->repos([string $userId]): array

列出用户下的 Gitter 频道。

  • $client->users->channels([string $userId]): array

自定义 WebHook 通知

创建一个 "自定义 Webhook"

  • 打开您的聊天
  • 单击 "房间设置" 按钮
  • 点击“集成”
  • 选择“自定义”
  • 记住你的钩子ID,例如在 https://webhooks.gitter.im/e/ URL 内的 2b66cf4653faa342bbe8
$client->notify($hookId)
    // ->error($message) - Send "Error" message
    // ->info($message) - Send "Info" message
    // ->withLevel(...) - Sets up level
    ->send('Your message with markdown'); // Send message with markdown content

自定义路由

$route = Route::get('rooms/{roomId}/chatMessages')
    ->with('roomId', $roomId)
    ->toStream();
    
// Contains "GET https://stream.gitter.im/v1/rooms/.../chatMessages" url

$client->viaStream()->request($route)->subscribe(function($message) {
     var_dump($message);
    // Subscribe on every message in target room (Realtime subscribtion)
});

$client->connect();

可用的路由方法

  • Route::get(string $route) - GET http 方法
  • Route::post(string $route) - POST http 方法
  • Route::put(string $route) - PUT http 方法
  • Route::patch(string $route) - PATCH http 方法
  • Route::delete(string $route) - DELETE http 方法
  • Route::options(string $route) - OPTIONS http 方法
  • Route::head(string $route) - HEAD http 方法
  • Route::connect(string $route) - CONNECT http 方法
  • Route::trace(string $route) - TRACE http 方法

路由参数

  • $route->with(string $key, string $value) - 添加路由或 GET 查询参数
  • $route->withMany(array $parameters) - 添加路由或 GET 查询参数
  • $route->withBody(string $key, string $value) - 添加 POST、PUT、DELETE 等 body 参数

有关API的更多信息,请参阅 文档