flourishlabs/saloon-slack

Saloon connectors/requests for Slack

0.0.4 2023-03-11 16:24 UTC

README

Latest Version on Packagist Tests Total Downloads

Introduction

The power of Slack powered by Saloon

Installation

Install the package via composer:

composer require flourishlabs/saloon-slack

Usage - API

Instance

Create an instance

use FlourishLabs\SaloonSlack\SlackConnector;

$slack = new SlackConnector('token');

Generic GET

$response = $slack->get('users.info', ['user' => 'W1234567890']);

$response = $slack->get('admin.emoji.add', [
    'name' => 'pikachu_wave',
    'url' => 'https://emojis.slackmojis.com/emojis/images/1643514747/7550/pikachu_wave.gif?1643514747',
]);

Generic POST

$slack->post('chat.postEphemeral', [
    'channel' => 'C1234567890',
    'text' => 'Well howdy!',
    'user' => 'U0HH0WDY',
]);

Responses

The most useful method you'll need on a Response object is json:

$response->json('channel_id');
$response->json('message_ts')

Saloon's documentation is best for responses, but there are extra Slack specific methods available too.

  • hasWarning(): bool & warning(): string From Slack docs: In the case of problematic calls that could still be completed successfully, ok will be true and the warning property will contain a short machine-readable warning code (or comma-separated list of them, in the case of multiple warnings).
    if ($response->hasWarning()) {
        Log::warning($response->warning());
    }
  • hasError(): bool & error(): string From Slack docs For failure results, the error property will contain a short machine-readable error code.
    if ($response->hasError()) {
        Log::error("Ah poo! {$response->error()}");
    }

Usage - OAuth

You can also interact with Slack's OAuth through the SlackAuthConnector.

Instance

Create an Auth instance

use FlourishLabs\SaloonSlack\SlackAuthConnector;

$oauth = new SlackAuthConnector(
    $clientId,
    $clientSecret,
    $redirectUri,
);

Generate auth URL

You'll likely want to generate & store state in session to verify during token exchange. You'll want to redirect the user to this authorization URL

$oauth->getSlackAuthorizationUrl(
    $botScopes,
    $userScopes,
);  

Exchange

If you need to access the bot and user token you should return the response.

$response = $oauth->getAccessToken(
    code: $request->get('code'),
    state: $request->get('state'),
    expectedState: $request->session()->get('slack.auth.state'),
    returnResponse: true,
);

$botToken = $response->json('access_token');
$userToken = $response->json('authed_user.access_token');

If you only need the bot token you can use the standard Saloon setup (their docs).

$authenticator = $oauth->getAccessToken($code, $state);

You can then use these tokens as normal to instantiate a SlackConnector.

use FlourishLabs\SaloonSlack\SlackConnector;

(new SlackConnector($response->json('access_token')))
->post('chat.postMessage', ['channel' => 'C123', 'text' => 'Cor, this is good eh']);

Testing

vendor/bin/pest

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.