水鸟/chatwork-sdk

仅是Chatwork的非官方SDK

0.4.0 2021-06-25 08:30 UTC

This package is auto-updated.

Last update: 2024-09-25 16:01:20 UTC


README

API文档
请注意,此SDK为非官方SDK。当Chatwork更新其API时,它可能无法正常工作。
(然而,我会尽力覆盖Chatwork的所有更改。如果出现问题,请告知我)

StyleCI Build Status Latest Stable Version Total Downloads Latest Unstable Version License

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

需求

  • PHP >= 5.4
  • PHP cURL

安装

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

composer require wataridori/chatwork-sdk

或者将wataridori/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消息、REPLY或QUOTE消息。

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();
}
有关更详细的信息和使用方法,请在此处查看ChatworkSDK函数列表这里

运行测试

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

贡献

在此查看贡献指南