renzojohnson/slack-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

renzojohnson/slack-api

最新稳定版本:v1.0.0

Composer 安装命令:

composer require renzojohnson/slack-api

包简介

Lightweight PHP wrapper for Slack Web API. Send messages, blocks, and files to channels. Incoming webhooks and slash command verification. Zero dependencies.

README 文档

README

Latest Version PHP Version License

Lightweight PHP wrapper for Slack Web API. Zero dependencies.

Send messages, blocks, and attachments to channels. Incoming webhooks and slash command verification with HMAC-SHA256.

Author: Renzo Johnson

Requirements

  • PHP 8.4+
  • ext-curl
  • ext-json

Installation

composer require renzojohnson/slack-api

Quick Start

use RenzoJohnson\Slack\Slack;

$slack = new Slack('xoxb-your-bot-token');

// Send a text message
$slack->sendText('#general', 'Hello from PHP!');

Web API Methods

Send Text

$slack->sendText('#general', 'Hello World');

// Thread reply
$slack->sendText('#general', 'Replying...', threadTs: '1234567890.123456');

Send Blocks (Block Kit)

use RenzoJohnson\Slack\Message\Block;

$slack->sendBlocks('#alerts', [
    Block::header('Deploy Alert'),
    Block::divider(),
    Block::section('Production deploy completed for *v2.1.0*'),
    Block::image('https://example.com/chart.png', 'CPU chart'),
], text: 'Deploy Alert — fallback for notifications');

Send Attachment (Legacy)

$slack->sendAttachment(
    '#ops',
    fallback: 'Server CPU at 95%',
    color: '#ff0000',
    title: 'Server Alert',
    text: 'CPU usage has exceeded threshold.',
    fields: [
        ['title' => 'Server', 'value' => 'web-01', 'short' => true],
        ['title' => 'CPU', 'value' => '95%', 'short' => true],
    ],
);

Update Message

$response = $slack->sendText('#general', 'Loading...');
$ts = $response['ts'];

$slack->updateMessage('#general', $ts, 'Done!');

Delete Message

$slack->deleteMessage('#general', '1234567890.123456');

Add Reaction

$slack->addReaction('#general', '1234567890.123456', 'thumbsup');

List Conversations

$channels = $slack->listConversations();

Auth Test

$info = $slack->authTest();
echo $info['user']; // bot username

Incoming Webhooks

No bot token needed — just a webhook URL.

use RenzoJohnson\Slack\Webhook\IncomingWebhook;

$webhook = new IncomingWebhook('https://hooks.slack.com/services/T.../B.../xxx');

// Simple text
$webhook->send('Deploy complete!');

// Rich message
use RenzoJohnson\Slack\Message\WebhookMessage;

$webhook->send(new WebhookMessage(
    'Deploy complete!',
    username: 'DeployBot',
    iconEmoji: ':rocket:',
));

Slash Commands

Verify and Parse

use RenzoJohnson\Slack\Webhook\SlashCommand;

// Verifies X-Slack-Signature via HMAC-SHA256, returns parsed params
$params = SlashCommand::verify('your-signing-secret');

echo $params['command'];    // /weather
echo $params['text'];       // 94070
echo $params['user_name'];  // john

Respond

// Ephemeral (only visible to user)
SlashCommand::respond('Processing your request...');

// Visible to entire channel
SlashCommand::respond('Weather: 72F, Sunny', inChannel: true);

Error Handling

use RenzoJohnson\Slack\Exception\AuthenticationException;
use RenzoJohnson\Slack\Exception\RateLimitException;
use RenzoJohnson\Slack\Exception\SlackException;

try {
    $slack->sendText('#general', 'Hello');
} catch (AuthenticationException $e) {
    // Invalid bot token
} catch (RateLimitException $e) {
    $retryAfter = $e->getRetryAfter(); // seconds
} catch (SlackException $e) {
    // Other API errors (channel_not_found, etc.)
    $errorData = $e->getErrorData();
}

Testing

composer install
vendor/bin/phpunit

Links

License

MIT License. Copyright (c) 2026 Renzo Johnson.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-24