neleid/chatwork-sdk

仅适用于Chatwork的非官方SDK

0.4.1 2018-12-15 07:52 UTC

This package is auto-updated.

Last update: 2024-09-19 19:44:50 UTC


README

请记住,此SDK是非官方的。当Chatwork更新其API时,它可能无法正常工作。

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

该项目是从wataridori/chatwork-sdk派生的,并添加了一些功能。如果您想使用原始存储库,请检查wataridori/chatwork-sdk

此存储库已在Packagist上注册。因此,您可以使用composer通过'neleid/chatwork-sdk'安装此存储库。

Chatwork SDK支持Chatwork API版本2
在此查看Chatwork API文档
英文API文档在此

与原始存储库的差异

  • 添加了对Chatwork API使用限制的支持。您可以在成功调用API后获取API使用限制信息。
  • 添加了对Chatwork 'self_unread'功能的支持。这将使您发布的消息保持未读状态。(这意味着您会收到提示。)
  • 将命名空间从wataridori更改为Neleid。
  • PHP要求从5.4升级到7.0。

要求

  • PHP >= 7.0
  • PHP cURL

安装

您可以使用Composer安装和管理Chatwork SDK for PHP

composer require neleid/chatwork-sdk

或将neleid/chatwork-sdk添加到您的composer.json文件的require部分,然后运行composer update

使用方法

首先,要使用Chatwork API,您必须注册一个API密钥。
将您的密钥传递给ChatworkSDK类。
ChatworkSDK::setApiKey($apiKey);

如果您遇到SSL证书验证问题,可以通过以下设置将其关闭。

// Not recommend. Only do this when you have problems with the request
ChatworkSDK::setSslVerificationMode(false);

现在,您可以使用许多功能轻松访问Chatwork API端点

ChatworkSDK的类

ChatworkAPI:这是一个包含基本API的类。您可以使用它向Chatwork发送请求并接收数组形式的响应。

ChatworkSDK::setApiKey($apiKey);
$api = new ChatworkApi();
// Get user own information
$api->me();

// Get user own statics information
$api->getMyStatus();

// Get user rooms list
$api->getRooms();
ChatworkSDK还提供了许多其他类,帮助您以更面向对象的方式工作。
  • ChatworkRoom:用于存储房间信息,具有许多用于处理房间的功能
  • ChatworkUser:用于存储用户信息。
  • ChatworkMessage:用于存储消息信息。
ChatworkSDK::setApiKey($apiKey);
$room = new ChatworkRoom($roomId);
// The following function will return an array of ChatworkUser
$members = $room->getMembers();
foreach ($members as $member) {
    // Print out User Information
    print_r($member->toArray());
}

// Send Message to All Members in the Room
$room->sendMessageToAll('Test Message');

// Send Message to list of members in the room
$room->sendMessageToList([$member_1, $member_2], 'Another Test Message');

上述3个类都扩展自ChatworkBase类。ChatworkBase提供了一些用于处理消息的有用功能。您可以轻松构建TO消息、回复或引用消息。

ChatworkSDK::setApiKey($apiKey);
$room = new ChatworkRoom($roomId);
$messages = $room->getMessages();
if ($messages & !empty($messages[0])) {
    $lastMessage = $messages[0];
    // Reset Message to null string
    $room->resetMessage();
    // Append the REPLY text to current message
    $room->appendReplyInRoom($lastMessage);
    // Append the QUOTE text to current message
    $room->appendQuote($lastMessage);
    // Append the Information Text to the current message
    $room->appendInfo('Test Quote, Reply, Info text', 'Test from Chatwork-SDK');
    // Send current message into the Room
    $room->sendMessage();
}

运行测试

  • tests/fixtures/文件夹中创建一个名为config.json的文件。
  • config.json文件中输入您的API密钥和一个测试房间。它应该看起来像这样
{
  "apiKey": "YOUR-API-KEY-HERE",
  "roomId": "YOUR-TEST-ROOM-HERE"
}
  • 然后运行phpunit开始测试。

贡献

在此查看贡献指南