-
Notifications
You must be signed in to change notification settings - Fork 178
/
Slack.php
105 lines (90 loc) · 1.99 KB
/
Slack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Laravel\Envoy;
use GuzzleHttp\Client;
class Slack
{
use ConfigurationParser;
/**
* The webhook URL.
*
* @var string
*/
public $hook;
/**
* The Slack channel.
*
* @var mixed
*/
public $channel;
/**
* The message.
*
* @var string
*/
public $message;
/**
* The options.
*
* @var array
*/
public $options;
/**
* The task name.
*
* @var string
*/
protected $task;
/**
* Create a new Slack instance.
*
* @param string $hook
* @param mixed $channel
* @param string $message
* @param array $options
* @return void
*/
public function __construct($hook, $channel = '', $message = null, $options = [])
{
$this->hook = $hook;
$this->channel = $channel;
$this->message = $message;
$this->options = $options;
}
/**
* Create a new Slack message instance.
*
* @param string $hook
* @param mixed $channel
* @param string $message
* @param array $options
* @return \Laravel\Envoy\Slack
*/
public static function make($hook, $channel = '', $message = null, $options = [])
{
return new static($hook, $channel, $message, $options);
}
/**
* Send the Slack message.
*
* @return void
*/
public function send()
{
$message = $this->message ?: ($this->task ? ucwords($this->getSystemUser()).' ran the ['.$this->task.'] task.' : ucwords($this->getSystemUser()).' ran a task.');
$payload = array_merge(['text' => $message, 'channel' => $this->channel], $this->options);
(new Client())->post($this->hook, [
'json' => $payload,
]);
}
/**
* Set the task for the message.
*
* @param string $task
* @return $this
*/
public function task($task)
{
$this->task = $task;
return $this;
}
}