|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\MsTeams; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
7
|
|
|
use Illuminate\Notifications\Notification; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use NotificationChannels\MsTeams\Exceptions\CouldNotSendNotification; |
|
10
|
|
|
|
|
11
|
|
|
class MsTeamsChannel |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Client |
|
15
|
|
|
*/ |
|
16
|
|
|
private $client; |
|
17
|
|
|
|
|
18
|
2 |
|
public function __construct(Client $client) |
|
19
|
|
|
{ |
|
20
|
2 |
|
$this->client = $client; |
|
21
|
2 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param $notifiable |
|
25
|
|
|
* @param Notification $notification |
|
26
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
27
|
|
|
* @throws CouldNotSendNotification |
|
28
|
|
|
*/ |
|
29
|
2 |
|
public function send($notifiable, Notification $notification) |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var MsTeamsMessage $message */ |
|
32
|
2 |
|
$message = $notification->toMsTeams($notifiable); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
2 |
|
if ($message->toUnknown()) { |
|
35
|
2 |
|
if (! $to = $notifiable->routeNotificationFor('msteams')) { |
|
36
|
1 |
|
throw CouldNotSendNotification::connectorWebHookUrlMissing(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
$message->to($to); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
$data = $message->toArray(); |
|
43
|
|
|
|
|
44
|
1 |
|
if (! $url = Arr::get($data, 'url')) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$code = collect(Arr::get($data, 'code', [])) |
|
49
|
|
|
->map(function ($code){ |
|
50
|
|
|
return [ |
|
51
|
|
|
"name" => "Code", |
|
52
|
|
|
"value" => "<pre>$code</pre>" |
|
53
|
|
|
]; |
|
54
|
1 |
|
}); |
|
55
|
|
|
|
|
56
|
1 |
|
$potentialActions = collect(Arr::get($data, 'buttons', [])) |
|
57
|
|
|
->map(function ($button) { |
|
58
|
|
|
return (object) [ |
|
59
|
|
|
'@context' => 'http://schema.org', |
|
60
|
|
|
'@type' => 'ViewAction', |
|
61
|
|
|
'name' => $button['text'], |
|
62
|
|
|
'target' => [ |
|
63
|
|
|
$button['url'], |
|
64
|
|
|
], |
|
65
|
|
|
]; |
|
66
|
1 |
|
}); |
|
67
|
|
|
|
|
68
|
1 |
|
$images = collect(Arr::get($data, 'images', [])) |
|
69
|
|
|
->map(function ($image) { |
|
70
|
|
|
return (object) [ |
|
71
|
|
|
'image' => $image, |
|
72
|
|
|
]; |
|
73
|
1 |
|
}); |
|
74
|
|
|
|
|
75
|
|
|
$payload = [ |
|
76
|
1 |
|
"@type" => "MessageCard", |
|
77
|
1 |
|
"@context" => "http://schema.org/extensions", |
|
78
|
1 |
|
"summary" => Arr::get($data, 'title', 'Incoming notification'), |
|
79
|
1 |
|
"themeColor" => $this->getNotificationType(Arr::get($data, 'type', 'success')), |
|
80
|
1 |
|
"title" => Arr::get($data, 'title'), |
|
81
|
|
|
|
|
82
|
|
|
"sections" => [ |
|
83
|
|
|
[ |
|
84
|
1 |
|
"activitySubtitle" => sprintf("%s : (%s)",config('app.url'), config('app.env')), |
|
85
|
1 |
|
"text" => Arr::get($data, 'text'), |
|
86
|
1 |
|
"facts" => $code, |
|
87
|
1 |
|
"images" => $images, |
|
88
|
|
|
] |
|
89
|
|
|
], |
|
90
|
|
|
|
|
91
|
1 |
|
'potentialAction' => $potentialActions, |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
1 |
|
$response = $this->client->post($data['url'], [ |
|
96
|
1 |
|
'json' => $payload, |
|
97
|
|
|
]); |
|
98
|
|
|
} catch (ClientException $exception) { |
|
99
|
|
|
throw CouldNotSendNotification::msTeamsRespondedWithAnError($exception); |
|
100
|
|
|
} catch (\Exception $exception) { |
|
101
|
|
|
throw CouldNotSendNotification::couldNotCommunicateWithMsTeams($exception); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
return $response; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Generate a colour code use for the card accent colour... |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $type |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
private function getNotificationType($type = 'info') : string |
|
114
|
|
|
{ |
|
115
|
1 |
|
switch($type){ |
|
116
|
1 |
|
case 'error': |
|
117
|
|
|
return '#D8000C'; |
|
118
|
|
|
break; |
|
|
|
|
|
|
119
|
|
|
default: |
|
120
|
1 |
|
case 'info': |
|
|
|
|
|
|
121
|
|
|
return '#31708f'; |
|
122
|
|
|
break; |
|
123
|
1 |
|
case 'success': |
|
124
|
1 |
|
return '#4F8A10'; |
|
125
|
|
|
break; |
|
|
|
|
|
|
126
|
|
|
case 'warning': |
|
127
|
|
|
return '#FEEFB3'; |
|
128
|
|
|
break; |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.