You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rework the send method and implement attachments and icons
Rather than bogging down the send() function, we now have nice chainable methods for setting the username, channel and icon. The send function is now used to send the composed message along with the text for the message.
Also implemented some unit testing.
Copy file name to clipboardExpand all lines: README.md
+177-16
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,31 @@
1
1
# Slack
2
2
3
-
A simple Laravel package for sending messages to [Slack](https://slack.com) with [incoming webhooks](https://my.slack.com/services/new/incoming-webhook).
3
+
A simple PHP package for sending messages to [Slack](https://slack.com) with [incoming webhooks](https://my.slack.com/services/new/incoming-webhook), focussed on ease-of-use and elegant syntax. Includes Laravel 4 support out of the box.
4
4
5
5
## Requirements
6
6
7
7
* PHP 5.4 or greater
8
-
* Laravel 4.1 or greater
8
+
9
+
## Contributors
10
+
11
+
I will happily look at any pull requests or suggestions to improve the package and provide attribution for your contributions. Help share the improvements with everyone!
You can install the package using the [Composer](https://getcomposer.org/) package manager. Assuming you have Composer installed globally:
13
19
14
20
```sh
15
-
composer require maknz/slack:0.1.*
21
+
composer require maknz/slack:0.2.*
16
22
```
17
23
18
-
### Service provider and alias
24
+
##Laravel 4
19
25
20
-
Next, add the `Maknz\Slack\SlackServiceProvider` service provider to the `providers` array in your `app/config.php` file.
26
+
We include a Laravel 4 facade which provides a nicer syntax for using the client and allows for automatic configuration of username, channel and icon.
27
+
28
+
Firstly, add the `Maknz\Slack\SlackServiceProvider` service provider to the `providers` array in your `app/config.php` file.
21
29
22
30
```php
23
31
'providers' => array(
@@ -43,27 +51,180 @@ Publish the configuration with
43
51
php artisan config:publish maknz/slack
44
52
```
45
53
46
-
This will add the boilerplate configuration to `app/config/packages/maknz/slack/config.php`. You need to add the URL to the webhook the package should use. If you haven't already created an incoming webhook for the package to use, [create one in your Slack backend](https://my.slack.com/services/new/incoming-webhook). The URL will be available under the "Instructions for creating Incoming WebHooks" panel. You can also configure the default channeland username in the config file.
54
+
This will add the boilerplate configuration to `app/config/packages/maknz/slack/config.php`. You need to add the URL to the webhook the package should use. If you haven't already created an incoming webhook for the package to use, [create one in your Slack backend](https://my.slack.com/services/new/incoming-webhook). The URL will be available under the "Instructions for creating Incoming WebHooks" panel. You can also configure the default channel, username and icon in the config file.
47
55
48
-
You can change the icon that will be used when editing the webhook in the Slack backend.
56
+
You can change the default icon to be used in the Slack backend, or it can be changed to a URL or emoji client-side.
49
57
50
58
## Usage
51
59
60
+
### Using Laravel
61
+
62
+
#### Sending a message using the defaults in the config
63
+
52
64
```php
53
-
// Sending a message using the defaults in the configuration
54
65
Slack::send('Hello world!');
66
+
```
67
+
68
+
#### Sending a message to a different channel
69
+
```php
70
+
Slack::to('#accounting')->send('Are we rich yet?');
71
+
```
72
+
73
+
#### Sending a message to a user
74
+
```php
75
+
Slack::to('@regan')->send('Yo!');
76
+
```
77
+
78
+
#### Sending a message to a channel as a different username
79
+
```php
80
+
Slack::from('Jake the Dog')->to('@FinnTheHuman')->send('Adventure time!');
All the same methods from the Laravel examples apply, the only difference is needing to instantiate a client manually.
146
+
147
+
You will need to `use` the Client at the top of your class:
148
+
149
+
```php
150
+
use Maknz\Slack\Client;
151
+
```
152
+
153
+
#### Send using the class defaults
154
+
155
+
This example sends 'Yo!' to #general as the user 'Robot', with the default webhook icon that Slack provide.
156
+
157
+
```php
158
+
$client = new Client('http://the.slack.endpoint');
159
+
160
+
$client->send('Yo!');
161
+
```
162
+
163
+
#### Instantiate the client with config/defaults
164
+
165
+
This example changes the default username, channel and icon from the class defaults. This is how the Laravel service provider works to set the defaults from the configuration file.
166
+
167
+
```php
168
+
$config = [
169
+
'username' => 'The Website Bot',
170
+
'channel' => '#operations',
171
+
'icon' => ':heart_eyes:'
172
+
];
173
+
174
+
$client = new Client('http://the.slack.endpoint', $config);
175
+
176
+
$client->send('Test message');
177
+
```
178
+
179
+
### Changing the config on the fly
180
+
181
+
As with the Laravel examples, the config can be changed on the fly.
182
+
183
+
```php
184
+
$client = new Client('http://the.slack.endpoint', ...);
When using attachments, the easiest way is to provide an array of data as shown in the examples, which is actually converted to an Attachment object under the hood. You can also attach an Attachment object to the client:
61
192
62
-
// Sending a message to a channel, overriding the default username
63
-
Slack::send('Hello world!', '#general', 'Robot');
193
+
```php
194
+
$attachment = new Attachment([
195
+
'fallback' => 'Some fallback text',
196
+
'text' => 'The attachment text'
197
+
]);
64
198
199
+
$client->attach($attachment);
65
200
```
66
201
67
-
## Contributing
202
+
Each attachment field is also an object, an AttachmentField. They can be used as well instead of their data in array form:
68
203
69
-
I will happily look at any pull requests to improve the package and provide attribution for your contributions. Help share the improvements with everyone!
204
+
```php
205
+
$attachment = new Attachment([
206
+
'fallback' => 'Some fallback text',
207
+
'text' => 'The attachment text',
208
+
'fields' => [
209
+
new AttachmentField([
210
+
'title' => 'A title',
211
+
'value' => 'A value',
212
+
'short' => true
213
+
]);
214
+
]
215
+
]);
216
+
```
217
+
218
+
You can also set the attachments and fields directly if you have a whole lot of them:
0 commit comments