定制 slack-php/slack-socket-mode 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

slack-php/slack-socket-mode

Composer 安装命令:

composer require slack-php/slack-socket-mode

包简介

Provides a a slack-php Socket Mode implementation

README 文档

README

This package provides a slack-php AppServer implementation that allows an application written for the Slack App Framework for PHP to be run in Socket Mode. Socket Mode uses the websocket protocol to communicate with Slack, and allows you to run an app (even locally) without having to expose it via a public HTTP endpoint. This can be very useful for testing Slack apps in a way that does not violate most work-related firewall restrictions.

This package should be used for testing only, and is not designed for production use.

This Socket Mode implementation uses the amphp/websocket-client package from Amp, an awesome collection of PHP packages that make async and event loop style programming in PHP possible. Check it out sometime.

Installation

  • Requires PHP 7.4+
  • Use Composer to install: composer require slack-php/slack-socket-mode

Usage

When using Socket Mode, there is no web server serving your app. When you run an app configured for Socket Mode, it establishes a connection to Slack as a websocket client. It maintains that connection to listen for Slack events until it is explicitly terminated (e.g., via CTRL+C), Socket Mode is disabled in your app configuration, or an unrecoverable error occurs. This is a different way of running PHP than many aren't used to, so please pay close attention.

When running an app in Slack Mode, you need an "App Token" instead of the typical "Signing Secret". The App Token is specially purposed for making the web socket connection. You can read more about Socket Mode in the Slack documentation to learn how to get an App Token.

Example

This small app responds to the /cool slash command.

Assumptions:

  • You have required the Composer autoloader to enable autoloading of the framework files.
  • You have set SLACK_APP_TOKEN in the environment (e.g., putenv("SLACK_APP_TOKEN=foo");)
<?php

use Psr\Log\LogLevel;
use SlackPhp\Framework\{App, Context, StderrLogger};
use SlackPhp\SocketMode\SocketServer;

App::new()
    ->command('cool', function (Context $ctx) {
        $ctx->ack(':thumbsup: That is so cool!');
    })
    ->withLogger(new StderrLogger(LogLevel::DEBUG))
    ->run(new SocketServer());

Switching Servers

The relationship between app and server can also be flipped, which might be helpful if you bootstrap script toggles between the Socket and HTTP server.

<?php

use Psr\Log\LogLevel;
use SlackPhp\Framework\{App, Context, StderrLogger};
use SlackPhp\Framework\Http\HttpServer;
use SlackPhp\SocketMode\SocketServer;

$app = App::new()
    ->command('cool', function (Context $ctx) {
        $ctx->ack(':thumbsup: That is so cool!');
    });

if (getenv('SOCKET_MODE')) {
    $server = SocketServer::new()->withLogger(new StderrLogger(LogLevel::DEBUG));
} else {
    $server = HttpServer::new()->withLogger(new StderrLogger(LogLevel::NOTICE));
}

$server->withApp($app)->start();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固