Commit adb3eac9 by Regan

Initial commit

parents
/vendor
composer.phar
composer.lock
.DS_Store
Copyright (c) 2014-2016, Regan McEntyre (maknz)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Slack for Laravel
This package allows you to use [Slack for PHP](https://github.com/maknz/slack) easily and elegantly in your Laravel 4 or 5 app. Read the instructions below to get setup, and then head on over to [Slack for PHP](https://github.com/maknz/slack) for usage details.
## Requirements
Laravel 4 or 5.
## Installation
You can install the package using the [Composer](https://getcomposer.org/) package manager. You can install it by running this command in your project root:
```sh
composer require maknz/slack-laravel
```
Then [create an incoming webhook](https://my.slack.com/services/new/incoming-webhook) for each Slack team you'd like to send messages to. You'll need the webhook URL(s) in order to configure this package.
## Laravel 5
Add the `Maknz\Slack\Laravel\ServiceProvider` provider to the `providers` array in `config/app.php`:
```php
'providers' => [
Maknz\Slack\Laravel\ServiceProvider::class,
],
```
Then add the facade to your `aliases` array:
```php
'aliases' => [
...
'Slack' => Maknz\Slack\Laravel\Facade::class,
],
```
Finally, publish the config file with `php artisan vendor:publish`. You'll find it at `config/slack.php`.
## Laravel 4
Add the `Maknz\Slack\Laravel\ServiceProvider` provider to the `providers` array in `app/config.php`:
```php
'providers' => [
...
'Maknz\Slack\Laravel\ServiceProvider',
],
```
Then add the facade to your `aliases` array:
```php
'aliases' => [
...
'Slack' => 'Maknz\Slack\Laravel\Facade',
],
```
Finally, publish the config file with `php artisan config:publish maknz/slack`. You'll find the config file at `app/config/packages/maknz/slack-laravel/config.php`.
## Configuration
The config file comes with defaults and placeholders. Configure at least one team and any defaults you'd like to change.
## Usage
The Slack facade is now your interface to the library. Any method you see being called an instance of `Maknz\Slack\Client` is available on the `Slack` facade for easy use.
Note that if you're using the facade in a namespace (e.g. `App\Http\Controllers` in Laravel 5) you'll need to either `use Slack` at the top of your class to import it, or append a backslash to access the root namespace directly when calling methods, e.g. `\Slack::method()`.
```php
// Send a message to the default channel
Slack::send('Hello world!');
// Send a message to a different channel
Slack::to('#accounting')->send('Are we rich yet?');
// Send a private message
Slack::to('@username')->send('psst!');
```
Now head on over to [Slack for PHP](https://github.com/maknz/slack) for more examples, including attachments and message buttons.
{
"name": "maknz/slack-laravel",
"description": "Laravel 4 and 5 integration for the maknz/slack package, including facades and service providers.",
"keywords": ["slack", "laravel"],
"license": "BSD-2-Clause",
"authors": [
{
"name": "maknz",
"email": "github@mak.geek.nz"
}
],
"require": {
"php": ">=5.5.0",
"maknz/slack": "@dev"
},
"autoload": {
"psr-4": {
"Maknz\\Slack\\Laravel\\": "src/"
}
},
"minimum-stability": "stable"
}
<?php
namespace Maknz\Slack\Laravel;
class Facade extends \Illuminate\Support\Facades\Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'maknz.slack';
}
}
<?php
namespace Maknz\Slack\Laravel;
use RuntimeException;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* The actual provider.
*
* @var \Illuminate\Support\ServiceProvider
*/
protected $provider;
/**
* Instantiate the service provider.
*
* @param mixed $app
* @return void
*/
public function __construct($app)
{
parent::__construct($app);
$this->provider = $this->getProvider();
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
return $this->provider->boot();
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
return $this->provider->register();
}
/**
* Return the service provider for the particular Laravel version.
*
* @return mixed
*/
private function getProvider()
{
$app = $this->app;
$version = intval($app::VERSION);
switch ($version) {
case 4:
return new ServiceProviderLaravel4($app);
case 5:
return new ServiceProviderLaravel5($app);
default:
throw new RuntimeException('Your version of Laravel is not supported');
}
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['maknz.slack'];
}
}
<?php
namespace Maknz\Slack\Laravel;
use Maknz\Slack\Client as Client;
use GuzzleHttp\Client as Guzzle;
class ServiceProviderLaravel4 extends \Illuminate\Support\ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('maknz/slack-laravel', null, __DIR__);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['maknz.slack'] = $this->app->share(function ($app) {
$allow_markdown = $app['config']->get('slack::allow_markdown');
$markdown_in_attachments = $app['config']->get('slack::markdown_in_attachments');
$unfurl_media = $app['config']->get('slack::unfurl_media');
return new Client(
$app['config']->get('slack::endpoint'),
[
'channel' => $app['config']->get('slack::channel'),
'username' => $app['config']->get('slack::username'),
'icon' => $app['config']->get('slack::icon'),
'link_names' => $app['config']->get('slack::link_names'),
'unfurl_links' => $app['config']->get('slack::unfurl_links'),
'unfurl_media' => is_bool($unfurl_media) ? $unfurl_media : true,
'allow_markdown' => is_bool($allow_markdown) ? $allow_markdown : true,
'markdown_in_attachments' => is_array($markdown_in_attachments) ? $markdown_in_attachments : [],
],
new Guzzle
);
});
$this->app->bind('Maknz\Slack\Client', 'maknz.slack');
}
}
<?php
namespace Maknz\Slack\Laravel;
use Maknz\Slack\Client as Client;
use GuzzleHttp\Client as Guzzle;
class ServiceProviderLaravel5 extends \Illuminate\Support\ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->publishes([__DIR__.'/config/config.php' => config_path('slack.php')]);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/config/config.php', 'slack');
$this->app['maknz.slack'] = $this->app->share(function ($app) {
return new Client(
$app['config']->get('slack.endpoint'),
[
'channel' => $app['config']->get('slack.channel'),
'username' => $app['config']->get('slack.username'),
'icon' => $app['config']->get('slack.icon'),
'link_names' => $app['config']->get('slack.link_names'),
'unfurl_links' => $app['config']->get('slack.unfurl_links'),
'unfurl_media' => $app['config']->get('slack.unfurl_media'),
'allow_markdown' => $app['config']->get('slack.allow_markdown'),
'markdown_in_attachments' => $app['config']->get('slack.markdown_in_attachments'),
],
new Guzzle
);
});
$this->app->bind('Maknz\Slack\Client', 'maknz.slack');
}
}
<?php
return [
/*
|-------------------------------------------------------------
| Incoming webhook endpoint
|-------------------------------------------------------------
|
| The endpoint which Slack generates when creating a
| new incoming webhook. It will look something like
| https://hooks.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXX
|
*/
'endpoint' => '',
/*
|-------------------------------------------------------------
| Default channel
|-------------------------------------------------------------
|
| The default channel we should post to. The channel can either be a
| channel like #general, a private #group, or a @username. Set to
| null to use the default set on the Slack webhook
|
*/
'channel' => '#general',
/*
|-------------------------------------------------------------
| Default username
|-------------------------------------------------------------
|
| The default username we should post as. Set to null to use
| the default set on the Slack webhook
|
*/
'username' => 'Robot',
/*
|-------------------------------------------------------------
| Default icon
|-------------------------------------------------------------
|
| The default icon to use. This can either be a URL to an image or Slack
| emoji like :ghost: or :heart_eyes:. Set to null to use the default
| set on the Slack webhook
|
*/
'icon' => null,
/*
|-------------------------------------------------------------
| Link names
|-------------------------------------------------------------
|
| Whether names like @regan should be converted into links
| by Slack
|
*/
'link_names' => false,
/*
|-------------------------------------------------------------
| Unfurl links
|-------------------------------------------------------------
|
| Whether Slack should unfurl links to text-based content
|
*/
'unfurl_links' => false,
/*
|-------------------------------------------------------------
| Unfurl media
|-------------------------------------------------------------
|
| Whether Slack should unfurl links to media content such
| as images and YouTube videos
|
*/
'unfurl_media' => true,
/*
|-------------------------------------------------------------
| Markdown in message text
|-------------------------------------------------------------
|
| Whether message text should be interpreted in Slack's Markdown-like
| language. For formatting options, see Slack's help article: http://goo.gl/r4fsdO
|
*/
'allow_markdown' => true,
/*
|-------------------------------------------------------------
| Markdown in attachments
|-------------------------------------------------------------
|
| Which attachment fields should be interpreted in Slack's Markdown-like
| language. By default, Slack assumes that no fields in an attachment
| should be formatted as Markdown.
|
*/
'markdown_in_attachments' => [],
// Allow Markdown in just the text and title fields
// 'markdown_in_attachments' => ['text', 'title']
// Allow Markdown in all fields
// 'markdown_in_attachments' => ['pretext', 'text', 'title', 'fields', 'fallback']
];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment