slack-sdk
, (*1)
Simple SDK for interacting with Slack.com via the API and webhooks., (*2)
Install
You can install slack-sdk by using composer require threadmeup/slack-sdk dev-master
., (*3)
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, (*4)
$chat = $slack->chat('#general');
$chat->send('Hello World!');
We can also list all users in the team, (*5)
$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., (*6)
$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., (*7)
$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);