PHP code example of brandon14 / fossabot-commander

1. Go to this page and download the library: Download brandon14/fossabot-commander 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/ */

    

brandon14 / fossabot-commander example snippets


// FooCommand.php


declare(strict_types=1);

namespace App\Fossabot\Commands;

use Brandon14\FossabotCommander\FossabotCommand;
use Brandon14\FossabotCommander\Contracts\Context\FossabotContext;

class FooCommand extends FossabotCommand
{
    /**
     * {@inheritDoc}
     */
    public function getResponse(?FossabotContext $context = null) : string
    {
        return 'Hello chat!';
    }
}

// In some Laravel Controller


declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Fossabot\Commands\FooCommand;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Brandon14\FossabotCommander\Contracts\FossabotCommander;

class Controller extends BaseController
{
    use AuthorizesRequests;
    use ValidatesRequests;

    private FossabotCommander $commander;

    public function __construct(FossabotCommander $commander)
    {
        $this->commander = $commander;
    }
    
    public function fooCommand(Request $request): string
    {
        // Get Fossabot API token.
        $apiToken = $request->header('x-fossabot-customapitoken');

        // Invoke command.
        return $this->commander->runCommand(new FooCommand(), $apiToken);
    }
}

// In some Laravel Controller


declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Fossabot\Commands\FooCommand;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Brandon14\FossabotCommander\Contracts\FossabotCommander;
use Brandon14\FossabotCommander\Contracts\Context\FossabotContext;

class Controller extends BaseController
{
    use AuthorizesRequests;
    use ValidatesRequests;

    private FossabotCommander $commander;

    public function __construct(FossabotCommander $commander)
    {
        $this->commander = $commander;
    }
    
    public function fooCommand(Request $request): string
    {
        // Get Fossabot API token.
        $apiToken = $request->header('x-fossabot-customapitoken');
        
        $command = function(?FossabotContext $context = null): string {
            return 'Hello chat!';
        }

        // Invoke command.
        return $this->commander->runCommand($command, $apiToken);
    }
}