Skip to content

Commit 1dffb51

Browse files
Update Microsoft Teams webhook JSON format
1 parent e59839f commit 1dffb51

File tree

3 files changed

+109
-50
lines changed

3 files changed

+109
-50
lines changed

README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<h1 align="center">
32
Laravel Alert Notifcations
43
</h1>
@@ -165,6 +164,57 @@ class Handler extends ExceptionHandler
165164
| slack.enabled | (default true), false will not notify to slack |
166165
| slack.webhook | (default null), null will not notify to slack |
167166

167+
### Microsoft Teams Webhook JSON Format
168+
169+
The code now supports the new Microsoft Teams webhook JSON format as specified in https://adaptivecards.io/schemas/adaptive-card.json.
170+
171+
#### Sample JSON Payload
172+
173+
```json
174+
{
175+
"@type": "AdaptiveCard",
176+
"@context": "https://adaptivecards.io/schemas/adaptive-card.json",
177+
"version": "1.2",
178+
"body": [
179+
{
180+
"type": "TextBlock",
181+
"text": "Environment: production"
182+
},
183+
{
184+
"type": "TextBlock",
185+
"text": "Server: example.com"
186+
},
187+
{
188+
"type": "TextBlock",
189+
"text": "Request Url: https://example.com/request"
190+
},
191+
{
192+
"type": "TextBlock",
193+
"text": "Exception: Exception"
194+
},
195+
{
196+
"type": "TextBlock",
197+
"text": "Message: Test Exception"
198+
},
199+
{
200+
"type": "TextBlock",
201+
"text": "Exception Code: 0"
202+
},
203+
{
204+
"type": "TextBlock",
205+
"text": "In File: /path/to/file.php on line 123"
206+
},
207+
{
208+
"type": "TextBlock",
209+
"text": "Stack Trace: #0 /path/to/file.php(123): function()"
210+
},
211+
{
212+
"type": "TextBlock",
213+
"text": "Context: array()"
214+
}
215+
]
216+
}
217+
```
168218

169219
### Samples
170220

src/MicrosoftTeams/ExceptionOccurredCard.php

+38-49
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,45 @@ public function __construct($exception, array $exceptionContext = [])
1818
public function getCard()
1919
{
2020
return [
21-
'@type' => 'MessageCard',
22-
'@context' => 'http://schema.org/extensions',
23-
'summary' => config('laravel_alert_notifications.microsoft_teams.cardSubject'),
24-
'themeColor' => config('laravel_alert_notifications.themeColor'),
25-
'title' => config('laravel_alert_notifications.cardSubject'),
26-
'sections' => [
21+
'@type' => 'AdaptiveCard',
22+
'@context' => 'https://adaptivecards.io/schemas/adaptive-card.json',
23+
'version' => '1.2',
24+
'body' => [
2725
[
28-
'activityTitle' => config('laravel_alert_notifications.microsoft_teams.cardSubject'),
29-
'activitySubtitle' => 'Error has occurred on '.config('app.name').' - '.config('app.name'),
30-
'activityImage' => '',
31-
'facts' => [
32-
[
33-
'name' => 'Environment:',
34-
'value' => config('app.env'),
35-
],
36-
[
37-
'name' => 'Server:',
38-
'value' => Request::server('SERVER_NAME'),
39-
],
40-
[
41-
'name' => 'Request Url:',
42-
'value' => Request::fullUrl(),
43-
],
44-
[
45-
'name' => 'Exception:',
46-
'value' => get_class($this->exception),
47-
],
48-
[
49-
'name' => 'Message:',
50-
'value' => $this->exception->getMessage(),
51-
],
52-
[
53-
'name' => 'Exception Code:',
54-
'value' => $this->exception->getCode(),
55-
],
56-
[
57-
'name' => 'In File:',
58-
'value' => '<b style="color:red;">'
59-
.$this->exception->getFile()
60-
.' on line '.$this->exception->getLine().'</b>',
61-
],
62-
[
63-
'name' => 'Stack Trace:',
64-
'value' => '<pre>'.$this->exception->getTraceAsString().'</pre>',
65-
],
66-
[
67-
'name' => 'Context:',
68-
'value' => '<pre>$context = '.var_export($this->exceptionContext, true).';</pre>',
69-
],
70-
],
26+
'type' => 'TextBlock',
27+
'text' => 'Environment: ' . config('app.env'),
28+
],
29+
[
30+
'type' => 'TextBlock',
31+
'text' => 'Server: ' . Request::server('SERVER_NAME'),
32+
],
33+
[
34+
'type' => 'TextBlock',
35+
'text' => 'Request Url: ' . Request::fullUrl(),
36+
],
37+
[
38+
'type' => 'TextBlock',
39+
'text' => 'Exception: ' . get_class($this->exception),
40+
],
41+
[
42+
'type' => 'TextBlock',
43+
'text' => 'Message: ' . $this->exception->getMessage(),
44+
],
45+
[
46+
'type' => 'TextBlock',
47+
'text' => 'Exception Code: ' . $this->exception->getCode(),
48+
],
49+
[
50+
'type' => 'TextBlock',
51+
'text' => 'In File: ' . $this->exception->getFile() . ' on line ' . $this->exception->getLine(),
52+
],
53+
[
54+
'type' => 'TextBlock',
55+
'text' => 'Stack Trace: ' . $this->exception->getTraceAsString(),
56+
],
57+
[
58+
'type' => 'TextBlock',
59+
'text' => 'Context: ' . var_export($this->exceptionContext, true),
7160
],
7261
],
7362
];

tests/AlertDispatcherTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Kevincobain2000\LaravelAlertNotifications\Dispatcher\AlertDispatcher;
7+
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\ExceptionOccurredCard;
78
use Mail;
89
use Mockery;
910
use Orchestra\Testbench\TestCase;
@@ -78,4 +79,23 @@ public function testBasic()
7879
$alert = new AlertDispatcher(new Exception(), []);
7980
$this->assertTrue(true);
8081
}
82+
83+
public function testMicrosoftTeamsCardFormat()
84+
{
85+
$exception = new Exception('Test Exception');
86+
$card = new ExceptionOccurredCard($exception, []);
87+
$json = $card->getCard();
88+
89+
$this->assertArrayHasKey('@context', $json);
90+
$this->assertArrayHasKey('@type', $json);
91+
$this->assertArrayHasKey('body', $json);
92+
93+
$body = $json['body'];
94+
$this->assertIsArray($body);
95+
96+
foreach ($body as $element) {
97+
$this->assertArrayHasKey('type', $element);
98+
$this->assertEquals('TextBlock', $element['type']);
99+
}
100+
}
81101
}

0 commit comments

Comments
 (0)