jc5/slack-client
Composer 安装命令:
composer require jc5/slack-client
包简介
A better Slack client, with RTM API support and updated packages
README 文档
README
This is an API client for Slack for PHP clients, with support for the Real Time Messaging API (RTM API) using web sockets.
Overview
This library was created primarily for Slackyboy, but was branched off into its own codebase so it could be used in other projects as well. I created this client because existing clients were either too complicated to use, or buggy, or incomplete. This is also the first PHP client I am aware of to support Slack's RTM API.
This project is updated to be compatible with PHP 8.1+ and has updated packages.
Installation
Install with Composer, obviously:
$ composer require jc5/slack-client
Please note that the current version has unstable dependencies.
In order to install those dependencies, you can set "minimum-stability" in your composer.json, and recommend that you set "prefer-stable":
{
"minimum-stability": "dev",
"prefer-stable": true
}
Usage
First, you need to create a client object to connect to the Slack servers. You will need to acquire an API token for your app first from Slack, then pass the token to the client object for logging in. Since this library uses React, you must also pass in an event loop object:
$loop = \React\EventLoop\Factory::create(); $client = new \Slack\ApiClient($loop); $client->setToken('YOUR-TOKEN-HERE'); // ... $loop->run();
Assuming your token is valid, you are good to go! You can now use wrapper methods for accessing most of the Slack API. Below is an example of posting a message to a channel as the logged in user:
$client->getChannelById('C025YTX9D')->then(function (\Slack\Channel $channel) use ($client) { $client->send('Hello from PHP!', $channel); });
Advanced messages
Slack supports messages much more rich than plain text through attachments. The easiest way to create a custom message is with a MessageBuilder:
use Slack\Message\{Attachment, AttachmentField}; $message = $client->getMessageBuilder() ->setText('Hello, all!') ->setChannel($someChannelObject) ->addAttachment(new Attachment('My Attachment', 'attachment text')) ->addAttachment(new Attachment('Build Status', 'Build failed! :/', 'build failed', 'danger'))) ->addAttachment(new Attachment('Some Fields', 'fields', null, '#BADA55', [ new AttachmentField('Title1', 'Text', false), new AttachmentField('Title2', 'Some other text', true) ])) ->create(); $client->postMessage($message);
Check the API documentation for a list of all methods and properties that messages, attachments, and fields support.
Asynchronous requests and promises
All client requests are made asynchronous using React promises. As a result, most of the client methods return promises. This lets you easily compose request orders and handle them as you need them. Since it uses React, be sure to call $loop->run() or none of the requests will be sent.
React allows the client to perform well and prevent blocking the entire thread while making requests. This is especially useful when writing real-time apps, like Slack chat bots.
Real Time Messaging API
You can also connect to Slack using the Real Time Messaging API. This is often useful for creating Slack bots or message clients. The real-time client is like the regular client, but it enables real-time incoming events. First, you need to create the client:
$client = new \Slack\RealTimeClient(); $client->setToken('YOUR-TOKEN-HERE'); $client->connect();
Then you can use the client as normal; RealTimeClient extends ApiClient, and has the same API for sending requests. You can attach a callback to handle incoming Slack events using RealTimeClient::on():
$client->on('file_created', function($data) { echo 'A file was created called ' . $data['file']['name'] . '!\n'; });
Below is a very simple, complete example:
$loop = React\EventLoop\Factory::create(); $client = new Slack\RealTimeClient($loop); $client->setToken('YOUR-TOKEN-HERE'); // disconnect after first message $client->on('message', function ($data) use ($client) { echo "Someone typed a message: ".$data['text']."\n"; $client->disconnect(); }); $client->connect()->then(function () { echo "Connected!\n"; }); $loop->run();
See the Slack API documentation for a list of possible events.
Documentation
You can view the complete API documentation here.
Running tests
You can run automated unit tests using PHPUnit after installing dependencies:
$ vendor/bin/phpunit
Where to get help
Need help? Just send me an email with your questions. Be sure to add "Slack client" to the message subject line so I know how I can help you out.
License
This library is licensed under the MIT license. See the LICENSE file for details.
jc5/slack-client 适用场景与选型建议
jc5/slack-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.6k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「realtime」 「slack」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jc5/slack-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jc5/slack-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jc5/slack-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Talk is a Laravel based realtime messaging, chatting and conversation system. It helps to develop users messaging, chatting and conversations in easy way.
A Highly Opinionated Symfony3/4 Deployer Recipe
This bundle provides integration with the Slack API library, allowing you to interact with the Slack API from within your Symfony projects
A PSR-7 compatible library for making CRUD API endpoints
Wrapper for Slack Web API
A Pusher bridge for Laravel 5+
统计信息
- 总下载量: 10.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-31