-
Notifications
You must be signed in to change notification settings - Fork 178
/
MicrosoftTeams.php
141 lines (124 loc) · 2.81 KB
/
MicrosoftTeams.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Laravel\Envoy;
use GuzzleHttp\Client;
class MicrosoftTeams
{
use ConfigurationParser;
/**
* The Teams WebHook URL.
*
* @var string
*/
public $hook;
/**
* The message for Teams Custom Card.
*
* @var string
*/
public $message;
/**
* The theme color for Teams Custom Card.
*
* @var string
*/
public $theme;
/**
* The extra options for Teams Custom Card.
*
* @var array
*/
public $options;
/**
* The name of the task.
*
* @var string
*/
protected $task;
/**
* Create a new Teams instance.
*
* @param string $hook
* @param string $message
* @param string $theme
* @param array $options
* @return void
*/
public function __construct($hook, $message = null, $theme = 'success', $options = [])
{
$this->hook = $hook;
$this->message = $message;
$this->theme = $theme;
$this->options = $options;
}
/**
* Create a new Teams message instance.
*
* @param string $hook
* @param string $message
* @param string $theme
* @param array $options
* @return \Laravel\Envoy\MicrosoftTeams
*/
public static function make($hook, $message = null, $theme = 'success', $options = [])
{
return new static($hook, $message, $theme, $options);
}
/**
* Send the Teams message.
*
* @return void
*/
public function send()
{
(new Client())->post(
$this->hook,
[
'json' => $this->buildPayload(),
]
);
}
/**
* Build the payload to send to the webhook.
*
* @return array
*/
private function buildPayload()
{
$message = $this->message ?: ($this->task ? ucwords($this->getSystemUser()).' ran the ['.$this->task.'] task.' : ucwords($this->getSystemUser()).' ran a task.');
return array_merge(
[
'@context' => 'https://schema.org/extensions',
'@type' => 'MessageCard',
'themeColor' => $this->getTheme(),
'text' => $message,
],
$this->options
);
}
/**
* Get the theme color for Teams Custom Card.
*
* @return string
*/
private function getTheme(): string
{
$themes = [
'success' => '#198754',
'info' => '#0dcaf0',
'error' => '#dc3545',
'warning' => '#fd7e14',
];
return $themes[$this->theme];
}
/**
* Set the task for the message.
*
* @param string $task
* @return $this
*/
public function task($task)
{
$this->task = $task;
return $this;
}
}