1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3 |
4
|
|
|
* https://github.com/wow-apps/symfony-slack-bot |
5
|
|
|
* |
6
|
|
|
* (c) 2016 WoW-Apps |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WowApps\SlackBundle\Service; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
15
|
|
|
use WowApps\SlackBundle\DTO\SlackMessage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SlackBot |
19
|
|
|
* |
20
|
|
|
* @author Alexey Samara <[email protected]> |
21
|
|
|
* @package WowApps\SlackBundle |
22
|
|
|
* @see https://github.com/wow-apps/symfony-slack-bot/wiki/2.-Using-SlackBot |
23
|
|
|
*/ |
24
|
|
|
class SlackBot |
25
|
|
|
{ |
26
|
|
|
const QUOTE_DEFAULT = 0; |
27
|
|
|
const QUOTE_DANGER = 1; |
28
|
|
|
const QUOTE_SUCCESS = 2; |
29
|
|
|
const QUOTE_WARNING = 3; |
30
|
|
|
const QUOTE_INFO = 4; |
31
|
|
|
const QUOTE_MAP = [ |
32
|
|
|
self::QUOTE_DEFAULT => 'default', |
33
|
|
|
self::QUOTE_DANGER => 'danger', |
34
|
|
|
self::QUOTE_SUCCESS => 'success', |
35
|
|
|
self::QUOTE_WARNING => 'warning', |
36
|
|
|
self::QUOTE_INFO => 'info' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** @var array */ |
40
|
|
|
private $config; |
41
|
|
|
|
42
|
|
|
/** @var GuzzleClient */ |
43
|
|
|
private $guzzleClient; |
44
|
|
|
|
45
|
|
|
/** @var SlackMessageValidator */ |
46
|
|
|
private $validator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* SlackBot constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $config |
52
|
|
|
* @param SlackMessageValidator $validator |
53
|
|
|
*/ |
54
|
3 |
|
public function __construct(array $config, SlackMessageValidator $validator) |
55
|
|
|
{ |
56
|
3 |
|
$this->setConfig($config); |
57
|
3 |
|
$this->guzzleClient = new GuzzleClient(); |
58
|
3 |
|
$this->validator = $validator; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
2 |
|
public function getConfig(): array |
65
|
|
|
{ |
66
|
2 |
|
return $this->config; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $config |
71
|
|
|
*/ |
72
|
3 |
|
public function setConfig(array $config) |
73
|
|
|
{ |
74
|
3 |
|
$this->config = $config; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $quoteType |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public function quoteTypeColor(int $quoteType): string |
82
|
|
|
{ |
83
|
1 |
|
if (!array_key_exists($quoteType, self::QUOTE_MAP)) { |
84
|
1 |
|
throw new \InvalidArgumentException('Unknown quote type'); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->config['quote_color'][self::QUOTE_MAP[$quoteType]]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param SlackMessage $slackMessage |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function sendMessage(SlackMessage $slackMessage): bool |
95
|
|
|
{ |
96
|
|
|
return $this->sendRequest($this->buildPostBody($slackMessage)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param SlackMessage $slackMessage |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
private function buildPostBody(SlackMessage $slackMessage): string |
104
|
|
|
{ |
105
|
|
|
$return = []; |
106
|
|
|
|
107
|
|
|
$this->validator->validateMessage($slackMessage); |
108
|
|
|
$slackMessage = $this->validator->setDefaultsForEmptyFields($slackMessage, $this->getConfig()); |
109
|
|
|
|
110
|
|
|
$return['text'] = $slackMessage->getText(); |
111
|
|
|
$return['mrkdwn'] = true; |
112
|
|
|
|
113
|
|
|
if ($slackMessage->isShowQuote()) { |
114
|
|
|
$return['attachments'] = [ |
115
|
|
|
'fallback' => $slackMessage->getText(), |
116
|
|
|
'pretext' => $slackMessage->getText(), |
117
|
|
|
'fields' => [ |
118
|
|
|
'title' => (!$slackMessage->getQuoteTitle() ? '' : $slackMessage->getQuoteTitle()), |
119
|
|
|
'title_link' => (!$slackMessage->getQuoteTitleLink() ? '' : $slackMessage->getQuoteTitleLink()), |
120
|
|
|
'text' => (!$slackMessage->getQuoteText() ? '' : $slackMessage->getQuoteText()), |
121
|
|
|
'color' => $this->quoteTypeColor($slackMessage->getQuoteType()), |
122
|
|
|
'mrkdwn_in' => ['text', 'pretext'] |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return json_encode($return, JSON_UNESCAPED_UNICODE); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $postBody |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
private function sendRequest(string $postBody): bool |
135
|
|
|
{ |
136
|
|
|
$request = $this->guzzleClient->post($this->config['api_url'], ['body' => $postBody]); |
137
|
|
|
if (!in_array($request->getStatusCode(), [200, 301, 302])) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|