DaitoGoogleChatManager.php
3.19 KB
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
<?php
namespace Daito\Lib\DaitoGoogleChat;
use Daito\Lib\DaitoGoogleChat\Jobs\DaitoGoogleChatWebhookJob;
use Daito\Lib\DaitoGoogleChat\Services\DaitoGoogleChatWebhookClient;
class DaitoGoogleChatManager
{
/**
* @var \Daito\Lib\DaitoGoogleChat\Services\DaitoGoogleChatWebhookClient
*/
private $client;
public function __construct(DaitoGoogleChatWebhookClient $client)
{
$this->client = $client;
}
public function sendText($text, array $arrContext = array(), $webhookUrl = null): array
{
return $this->sendPayload(
array(
'text' => $this->buildTextContent((string) $text, $arrContext),
),
$webhookUrl
);
}
public function sendPayload(array $arrPayload, $webhookUrl = null): array
{
return $this->client->send($arrPayload, $webhookUrl);
}
public function sendCardV2(array $arrCardV2, $webhookUrl = null): array
{
return $this->sendCardsV2(array($arrCardV2), $webhookUrl);
}
public function sendCardsV2(array $arrCardsV2, $webhookUrl = null): array
{
return $this->sendPayload(
array(
'cardsV2' => array_values($arrCardsV2),
),
$webhookUrl
);
}
public function queueText($text, array $arrContext = array(), $webhookUrl = null): void
{
$this->queuePayload(
array(
'text' => $this->buildTextContent((string) $text, $arrContext),
),
$webhookUrl
);
}
public function queuePayload(array $arrPayload, $webhookUrl = null): void
{
$job = new DaitoGoogleChatWebhookJob($arrPayload, $webhookUrl);
if (config('daito-google-chat.queue_connection') !== null) {
$job->onConnection(config('daito-google-chat.queue_connection'));
}
if (config('daito-google-chat.queue_name') !== null) {
$job->onQueue(config('daito-google-chat.queue_name'));
}
dispatch($job);
}
public function queueCardV2(array $arrCardV2, $webhookUrl = null): void
{
$this->queueCardsV2(array($arrCardV2), $webhookUrl);
}
public function queueCardsV2(array $arrCardsV2, $webhookUrl = null): void
{
$this->queuePayload(
array(
'cardsV2' => array_values($arrCardsV2),
),
$webhookUrl
);
}
private function buildTextContent(string $text, array $arrContext = array()): string
{
$textContent = trim($text);
if ($arrContext === array()) {
return $textContent;
}
$arrContextLines = array();
foreach ($arrContext as $key => $value) {
$arrContextLines[] = sprintf('%s: %s', (string) $key, $this->normalizeContextValue($value));
}
return $textContent . "\n" . implode("\n", $arrContextLines);
}
private function normalizeContextValue($value): string
{
if (is_scalar($value) || $value === null) {
return (string) $value;
}
$json = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $json !== false ? $json : '[unserializable]';
}
}