PHP code example of cedricziel / mattermost-php

1. Go to this page and download the library: Download cedricziel/mattermost-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cedricziel / mattermost-php example snippets




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);



// 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);
shell
composer