PHP code example of marcuspi / slacker

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

    

marcuspi / slacker example snippets




$token = "MY_TOKEN';
$trigger = 'MY_TRIGGER';
$command = 'COMMAND';

try {
    $slack = new Slacker\SlackRequest([$token]); # Look for a request in POST data
} catch catch(Slacker\InvalidRequestException $e) {
    die(echo $e->getMessage());
} catch(Slacker\InvalidTokenException $e) {
    die($e->getMessage() . '(got: *' . $e->getToken() . '*)');
}

# Responding to a trigger
if ($slack->isWebhook() && ($slack->triggerWord() == $trigger)) {
    $response = new Slacker\SlackResponse($slack);
    $response->setText('I\'m responding to the *trigger word* `' . $trigger . '` in the message `' . $slack->text() . '`');
    $response->respond();
}

# Responding to commands
if ($slack->isCommand() && ($slack->command() == $command)) {
    $response = new Slacker\SlackResponse($slack);
    $response->setText('I\'m responding to the *command* `' . $command . '` with the arguments `' . implode('`, `', $slack->args() . '`');
    $response->respond();
}



$att = new Slacker\SlackAttachment();
$att->setColor('good');
$att->setTitle('Everything is fine');
$att->setText($out);
$att->markdown(true, true, false);
$someResponse->addAttachment($att);



$channel = 'CHANNEL';
$username = 'USERNAME';
$webhookUrl = 'GET YOUR OWN TOKEN';

$incoming = new Slacker\SlackMessage($webhookUrl);
    $incoming->setText(':star2: Hello :star2:');
    $incoming->setChannel($channel);
    $incoming->setUsername($username);

$att = new Slacker\SlackAttachment();
    $att->setText('This is an attachment text!');
    $att->setColor('warning');
    $incoming->addAttachment($att);

$incoming->send(); # sends the request