cedricziel / mattermost-php
Mattermost 的 PHP 绑定
v1.4.27
2024-08-03 09:19 UTC
Requires
- php: ^8.2
- guzzlehttp/psr7: ^2.6
- php-http/discovery: ^1.19
- phpdocumentor/reflection-docblock: ^5.3
- psr/event-dispatcher: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.0 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- symfony/property-access: ^6.4|^7.0
- symfony/serializer: ^6.4|^7.0
Requires (Dev)
- nette/php-generator: ^4.1
- nikic/php-parser: ^5.0
- php-http/mock-client: ^1.6
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5 || ^11.0
- symfony/http-client: ^6.4|^7.0
- dev-main
- v1.4.27
- v1.4.26
- v1.4.25
- v1.4.24
- v1.4.23
- v1.4.22
- v1.4.21
- v1.4.20
- v1.4.19
- v1.4.18
- v1.4.17
- v1.4.16
- v1.4.15
- v1.4.14
- v1.4.13
- v1.4.12
- v1.4.11
- v1.4.10
- v1.4.9
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.12
- v1.2.11
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/symfony/http-client-7.1.5
- dev-release-please--branches--main
This package is auto-updated.
Last update: 2024-09-23 07:41:58 UTC
README
用于构建 Mattermost 应用和集成。
安装
composer require cedricziel/mattermost-php
使用
API 客户端
API 客户端是 Mattermost API 的简单包装。它提供了一个流畅的接口来与 API 交互。
<?php use CedricZiel\MattermostPhp\Client; use CedricZiel\MattermostPhp\Client\Model\CreatePostRequest; // create a client instance $client = new Client(getenv('MATTERMOST_SITE_URL')); // provide a token and authenticate $client->setToken(getenv('MATTERMOST_TOKEN')); $yourUser = $client->authenticate(); // OR authenticate with username and password $client->authenticate($loginId, $password); // get the team and a specific channel $team = $client->teams()->getTeamByName(getenv('MATTERMOST_TEAM_NAME')); $client->channels()->getAllChannels(per_page: 100); $channel = $client->channels()->getChannelByName($team->id, 'town-square'); // create a post in the channel $post = $client->posts()->createPost(new CreatePostRequest($channel->id, 'Hello World!')); var_dump($post);
斜杠命令
斜杠命令是集成 Mattermost 最常见的方式之一。它们通过在 Mattermost 的消息框中输入一个斜杠后跟命令名和任何参数来调用(例如 /weather New York
)。
要实现一个响应特定城市天气的斜杠命令,你可以这样做
注意: 此库建议使用 PSR-15 兼容的中间件栈来处理请求和响应。此库提供了一个 AbstractSlashCommand
类,可以扩展来实现一个处理请求所需所有功能的斜杠命令。
<?php // your PSR-15 ServerRequestInterface implementation $serverRequest = '...'; $slashCommand = new class('weather') extends \CedricZiel\Mattermost\SlashCommands\AbstractSlashCommand { public function execute(SlashCommandInput $input): SlashCommandOutput { $city = $input->getParameters(); /** * Your business logic here */ $weather = $this->getWeatherForCity($city); return SlashCommandOutput::create() ->setText(sprintf('The weather in %s is %s', $city, $weather)); } }; // handle the request. this will invoke the slash command and return a PSR-7 compatible response $slashCommand->handle($serverRequest);
请查看测试以获取更多示例。
许可证
MIT