stolfam / slack-php
Easy-to-use PHP classes for Slack integration
5.0.2
2024-05-23 10:28 UTC
Requires
- php: >= 8.2
- nette/utils: >= 3.0
- php-curl-class/php-curl-class: >=9.0
- stolfam/utils-php8: ^5.0
- tracy/tracy: ^2.6
Requires (Dev)
- nette/tester: >= 2.3
README
Provides a sending messages to Slack via a bot.
Requirements
You have to have a bot. How to do it?
- Create (or use existing) app via Slack API (https://api.slack.com/apps)
- Search for Features and Bot Users. Then click on the button Add a Bot User and fill the form.
- Now you have to add some permissions (in section OAuth & Permissions) and get Bot User OAuth Access Token
- Install (or reinstall) your app into your Slack workspace
Use
use \Ataccama\Output\Slack;
$slack = new Slack([
"token" => "xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxx", // Bot User OAuth Access Token
"enable" => true
]);
// simple message
$message = new SlackMessage("Test *message* for channel.");
// or using blocks
$message = new SlackMessage();
$message->addBlock( new Section("Some *test* section.") );
$message->addBlock( new Divider() );
$message->addBlock( new Image("Image 1", "https://example.xy/image.jpg", "Image 1 alternative text") );
$channel = new Channel("CXXXXXXXX", "Sandbox")
try {
$response = $slack->sendMessage($message, $channel);
} catch (SlackException $e) {
// fatal error
}
if($response) {
// success
} else {
// failed
$error = $slack->lastError;
}