Інтеграція Slack API з веб-сайтом (сповіщення, боти)
Slack-інтеграція дозволяє отправляти сповіщення про події сайту в канали команди, створювати інтерактивні боти для управління сайтом з Slack та отримувати команди від colaborators без виходу з месенджера.
Incoming Webhooks (прості сповіщення)
Найшвидший спосіб — Incoming Webhook URL, створений у налаштуваннях Slack App:
class SlackNotifier
{
public function send(string $channel, array $message): void
{
Http::post(config('services.slack.webhook_url'), [
'channel' => $channel,
'text' => $message['text'] ?? '',
'attachments' => $message['attachments'] ?? [],
'blocks' => $message['blocks'] ?? [],
]);
}
public function notifyNewOrder(Order $order): void
{
$this->send('#orders', [
'blocks' => [
[
'type' => 'section',
'text' => ['type' => 'mrkdwn', 'text' => "*Нове замовлення #{$order->number}*"],
],
[
'type' => 'section',
'fields' => [
['type' => 'mrkdwn', 'text' => "*Клієнт:*\n{$order->customer_name}"],
['type' => 'mrkdwn', 'text' => "*Сума:*\n{$order->formatted_total}"],
],
],
[
'type' => 'actions',
'elements' => [[
'type' => 'button',
'text' => ['type' => 'plain_text', 'text' => 'Переглянути замовлення'],
'url' => route('admin.orders.show', $order),
]],
],
],
]);
}
}
Slash Commands
// Обробник /check-order {order_id}
Route::post('/slack/commands/check-order', function (Request $request) {
// Верифікація підпису Slack
$signature = $request->header('X-Slack-Signature');
$timestamp = $request->header('X-Slack-Request-Timestamp');
$sigBase = "v0:{$timestamp}:" . $request->getContent();
$expected = 'v0=' . hash_hmac('sha256', $sigBase, config('services.slack.signing_secret'));
if (!hash_equals($expected, $signature)) abort(401);
$orderId = $request->input('text');
$order = Order::find($orderId);
if (!$order) {
return response()->json(['text' => "Замовлення #{$orderId} не знайдено"]);
}
return response()->json([
'response_type' => 'in_channel',
'text' => "Замовлення #{$orderId}: {$order->status}, {$order->formatted_total}",
]);
});
Events API (Slack → сайт)
Боти отримують події (нове повідомлення, реакція, вступ у канал) через Events API:
Route::post('/slack/events', function (Request $request) {
// Верифікація challenge під час початкового налаштування
if ($request->has('challenge')) {
return response()->json(['challenge' => $request->input('challenge')]);
}
$event = $request->input('event');
if ($event['type'] === 'message' && str_contains($event['text'], 'deploy')) {
TriggerDeploy::dispatch($event['user']);
}
return response('ok');
});
Графік
Сповіщення через Incoming Webhooks: 1 день. Slash Commands + Events API: 3–4 дні.







