1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* T3Bot. |
4
|
|
|
* |
5
|
|
|
* @author Frank Nägler <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link http://www.t3bot.de |
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
9
|
|
|
*/ |
10
|
|
|
namespace T3Bot\Controller; |
11
|
|
|
|
12
|
|
|
use T3Bot\Slack\Message; |
13
|
|
|
use T3Bot\Traits\LoggerTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class WebHookController. |
17
|
|
|
*/ |
18
|
|
|
class WebHookController extends AbstractHookController |
19
|
|
|
{ |
20
|
|
|
use LoggerTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $colorMap = [ |
26
|
|
|
'info' => Message\Attachment::COLOR_INFO, |
27
|
|
|
'ok' => Message\Attachment::COLOR_GOOD, |
28
|
|
|
'warning' => Message\Attachment::COLOR_WARNING, |
29
|
|
|
'danger' => Message\Attachment::COLOR_DANGER, |
30
|
|
|
'notice' => Message\Attachment::COLOR_NOTICE, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* public method to start processing the request. |
35
|
|
|
* |
36
|
|
|
* @param string $hook |
37
|
|
|
* @param string $input |
38
|
|
|
* |
39
|
|
|
* Input example: |
40
|
|
|
* { |
41
|
|
|
* "securityToken": "a valid security token", |
42
|
|
|
* "color": [info|ok|warning|danger|notice|#HEXCODE], |
43
|
|
|
* "title": "Title of the message" |
44
|
|
|
* "text": "Text of the message" |
45
|
|
|
* } |
46
|
|
|
* |
47
|
|
|
* @throws \Doctrine\DBAL\DBALException |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
9 |
|
public function process($hook, $input = 'php://input') |
51
|
|
|
{ |
52
|
9 |
|
$entityBody = file_get_contents($input); |
53
|
9 |
|
$this->getLogger()->debug($entityBody); |
54
|
|
|
|
55
|
9 |
|
$json = json_decode($entityBody); |
56
|
9 |
|
$hookConfiguration = $this->configuration['webhook'][$hook] ?? []; |
57
|
|
|
|
58
|
9 |
|
if (empty($hookConfiguration) || $hookConfiguration['securityToken'] !== $json->securityToken) { |
59
|
2 |
|
return; |
60
|
|
|
} |
61
|
7 |
|
$this->sendMessage($json, $hookConfiguration); |
62
|
7 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \stdClass $json |
66
|
|
|
* @param array $hookConfiguration |
67
|
|
|
* |
68
|
|
|
* @throws \Doctrine\DBAL\DBALException |
69
|
|
|
*/ |
70
|
7 |
|
protected function sendMessage(\stdClass $json, array $hookConfiguration) |
71
|
|
|
{ |
72
|
7 |
|
$color = $this->colorMap[$json->color] ?? $json->color; |
73
|
|
|
|
74
|
7 |
|
$message = new Message(); |
75
|
7 |
|
$message->setText(' '); |
76
|
7 |
|
$attachment = new Message\Attachment(); |
77
|
|
|
|
78
|
7 |
|
$attachment->setColor($color); |
79
|
7 |
|
$attachment->setTitle($json->title); |
80
|
7 |
|
$attachment->setText($json->text); |
81
|
7 |
|
$attachment->setFallback($json->text); |
82
|
7 |
|
$message->addAttachment($attachment); |
83
|
|
|
|
84
|
7 |
|
if (is_array($hookConfiguration['channels'])) { |
85
|
7 |
|
foreach ($hookConfiguration['channels'] as $channel) { |
86
|
7 |
|
$this->postToSlack($message, $channel); |
87
|
|
|
} |
88
|
|
|
} |
89
|
7 |
|
} |
90
|
|
|
} |
91
|
|
|
|