Skip to content

Commit 6d9aa3a

Browse files
committed
Initial commit
0 parents  commit 6d9aa3a

8 files changed

Lines changed: 4479 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/.idea/
3+
/.php-cs-fixer.cache

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Jeremy Lindblom
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Slack PHP – Socket Mode
2+
3+
This package provides a slack-php `AppServer` implementation that allows an application written for the
4+
[Slack App Framework for PHP][1] to be run in [Socket Mode][2]. Socket Mode uses the websocket protocol to communicate
5+
with Slack, and allows you to run an app (even locally) without having to expose it via a public HTTP endpoint. This can
6+
be very useful for testing Slack apps in a way that does not violate most work-related firewall restrictions.
7+
8+
**This package should be used for testing only, and is not designed for production use.**
9+
10+
> This Socket Mode implementation uses the [`amphp/websocket-client`][3] package from [Amp][4], an awesome collection of
11+
PHP packages that make async and event loop style programming in PHP possible. Check it out sometime.
12+
13+
## Installation
14+
15+
- Requires PHP 7.4+
16+
- Use Composer to install: `composer require slack-php/slack-socket-mode`
17+
18+
## Usage
19+
20+
When using Socket Mode, there is no web server serving your app. When you run an app configured for Socket Mode, it
21+
establishes a connection to Slack as a websocket client. It maintains that connection to listen for Slack events until
22+
it is explicitly terminated (e.g., via `CTRL+C`), Socket Mode is disabled in your app configuration, or an unrecoverable
23+
error occurs. This is a different way of running PHP than many are used to, so please pay close attention.
24+
25+
When running an app in Slack Mode, you need an "App Token" instead of the typical "Signing Secret". The App Token is
26+
specially purposed for making the web socket connection. You can read more about [Socket Mode][2] in the Slack
27+
documentation to learn how to get an App Token.
28+
29+
### Example
30+
31+
This small app responds to the `/cool` slash command.
32+
33+
> Assumptions:
34+
>
35+
> - You have required the Composer autoloader to enable autoloading of the framework files.
36+
> - You have set `SLACK_APP_TOKEN` in the environment (e.g., `putenv("SLACK_APP_TOKEN=foo");`)
37+
38+
```php
39+
<?php
40+
41+
use SlackPhp\Framework\App;
42+
use SlackPhp\Framework\Context;
43+
use SlackPhp\SocketMode\SocketServer;
44+
45+
App::new()
46+
->command('cool', function (Context $ctx) {
47+
$ctx->ack(':thumbsup: That is so cool!');
48+
})
49+
->run(new SocketServer());
50+
```
51+
52+
[1]: https://github.com/slack-php/slack-php-app-framework
53+
[2]: https://api.slack.com/apis/connections/socket
54+
[3]: https://amphp.org/websocket-client/
55+
[4]: https://amphp.org/

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "slack-php/slack-socket-mode",
3+
"type": "library",
4+
"license": "MIT",
5+
"description": "Provides a a slack-php Socket Mode implementation",
6+
"authors": [
7+
{
8+
"name": "Jeremy Lindblom",
9+
"email": "jeremeamia@gmail.com"
10+
}
11+
],
12+
"config": {
13+
"sort-packages": true,
14+
"platform": {
15+
"php": "7.4"
16+
}
17+
},
18+
"require": {
19+
"amphp/websocket-client": "^1.0",
20+
"slack-php/slack-app-framework": "^0.4.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"SlackPhp\\SocketMode\\": "src/"
25+
}
26+
},
27+
"require-dev": {
28+
"friendsofphp/php-cs-fixer": "^3.0",
29+
"phpstan/phpstan": "^0.12.90"
30+
},
31+
"scripts": {
32+
"style": "php-cs-fixer fix --dry-run --verbose --show-progress=none src",
33+
"style-fix": "php-cs-fixer fix src",
34+
"stan": "phpstan analyse --level=5 src",
35+
"qa": ["@style", "@stan"]
36+
}
37+
}

0 commit comments

Comments
 (0)