threadmeup / slack-sdk
This package is abandoned and no longer maintained.
No replacement package was suggested.
Slack SDK for PHP
1.1.3
2014-07-30 20:50 UTC
Requires
- guzzle/guzzle: ~3
This package is not auto-updated.
Last update: 2022-02-01 12:32:25 UTC
README
Simple SDK for interacting with Slack.com via the API and webhooks.
Install
You can install slack-sdk by using composer require threadmeup/slack-sdk dev-master
.
Configure
include 'vendor/autoload.php'; use ThreadMeUp\Slack\Client; $config = [ 'token' => 'USER-API-TOKEN', 'team' => 'YOUR-TEAM', 'username' => 'BOT-NAME', 'icon' => 'ICON', // Auto detects if it's an icon_url or icon_emoji 'parse' => '', // __construct function in Client.php calls for the parse parameter ]; $slack = new Client($config);
Examples
What we're doing here is sending the message Hello World!
to the #general
channel
$chat = $slack->chat('#general'); $chat->send('Hello World!');
We can also list all users in the team
$users = $slack->users(); foreach ($users as $user) { echo ($user->isAdmin() ? 'Admin' : 'User').': '.$user->name().' <'.$user->email().'>'.PHP_EOL; }
Or even listen to outgoing webhooks from Slack themselves.
$incoming = $slack->listen(); if ($incoming) { switch($incoming->text()) { case "What time is it?": $incoming->respond("It is currently ".date('g:m A T')); break; default: $incoming->respond("I don't understand what you're asking."); break; } }
For testing reasons you can pass Client::listen()
an array of the payload to simulate which will then ignore any $_POST
values and use the $payload
you supplied instead.
$payload = [ 'token' => 'YNgeXsCXyWgAMfCvjc7NUUpz', 'team_id' => 'T0001', 'channel_id' => 'C2147483705', 'channel_name' => 'test', 'timestamp' => '1355517523.000005', 'user_id' => 'U2147483697', 'user_name' => 'Steve', 'text' => 'googlebot: What is the air-speed velocity of an unladen swallow?' ]; $incoming = $slack->listen($payload);