cedricziel / mattermost-php
PHP Bindings for Mattermost
Fund package maintenance!
cedricziel
Installs: 4 169
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 2
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 || ^2.0
- phpunit/phpunit: ^10.5 || ^11.0
- symfony/http-client: ^6.4|^7.0
- dev-main
- v1.4.32
- v1.4.31
- v1.4.30
- v1.4.29
- v1.4.28
- 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-release-please--branches--main
- dev-dependabot/composer/phpdocumentor/reflection-docblock-5.6.0
- dev-dependabot/composer/phpstan/phpstan-2.0.2
This package is auto-updated.
Last update: 2024-11-19 08:08:40 UTC
README
Built for building apps and integrations for Mattermost.
Installation
composer require cedricziel/mattermost-php
Usage
API Client
The API client is a simple wrapper around the Mattermost API. It provides a fluent interface to interact with the 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);
Slash commands
Slash commands are one of the most common ways to integrate with Mattermost. They are invoked by typing a slash followed by the command name and any arguments into the message box in Mattermost (e.g. /weather New York
).
To implement a slash command that would respond with the weather in a given city, you would do something like this:
Note: This library does recommend using a PSR-15 compatible middleware stack to handle the request and response.
This library provides an AbstractSlashCommand
class that can be extended to implement a slash command that does everything
once it needs to handle a request.
<?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);
Please look at the tests for more examples.
License
MIT