Интеграция Slack API с сайтом (уведомления, боты)
Slack-интеграция позволяет отправлять уведомления о событиях сайта в каналы команды, создавать интерактивные боты для управления сайтом из Slack, и принимать команды от сотрудников без выхода из мессенджера.
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 verification при первой настройке
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 дня.







