From 1dffb51b71d4754f0d39cf88b908114b05846789 Mon Sep 17 00:00:00 2001 From: Pulkit Kathuria Date: Wed, 10 Jul 2024 10:46:23 +0900 Subject: [PATCH] Update Microsoft Teams webhook JSON format --- README.md | 52 +++++++++++- src/MicrosoftTeams/ExceptionOccurredCard.php | 87 +++++++++----------- tests/AlertDispatcherTest.php | 20 +++++ 3 files changed, 109 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 985abbd..944877c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -

Laravel Alert Notifcations

@@ -165,6 +164,57 @@ class Handler extends ExceptionHandler | slack.enabled | (default true), false will not notify to slack | | slack.webhook | (default null), null will not notify to slack | +### Microsoft Teams Webhook JSON Format + +The code now supports the new Microsoft Teams webhook JSON format as specified in https://adaptivecards.io/schemas/adaptive-card.json. + +#### Sample JSON Payload + +```json +{ + "@type": "AdaptiveCard", + "@context": "https://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.2", + "body": [ + { + "type": "TextBlock", + "text": "Environment: production" + }, + { + "type": "TextBlock", + "text": "Server: example.com" + }, + { + "type": "TextBlock", + "text": "Request Url: https://example.com/request" + }, + { + "type": "TextBlock", + "text": "Exception: Exception" + }, + { + "type": "TextBlock", + "text": "Message: Test Exception" + }, + { + "type": "TextBlock", + "text": "Exception Code: 0" + }, + { + "type": "TextBlock", + "text": "In File: /path/to/file.php on line 123" + }, + { + "type": "TextBlock", + "text": "Stack Trace: #0 /path/to/file.php(123): function()" + }, + { + "type": "TextBlock", + "text": "Context: array()" + } + ] +} +``` ### Samples diff --git a/src/MicrosoftTeams/ExceptionOccurredCard.php b/src/MicrosoftTeams/ExceptionOccurredCard.php index 0ce787f..fd8cb2b 100644 --- a/src/MicrosoftTeams/ExceptionOccurredCard.php +++ b/src/MicrosoftTeams/ExceptionOccurredCard.php @@ -18,56 +18,45 @@ public function __construct($exception, array $exceptionContext = []) public function getCard() { return [ - '@type' => 'MessageCard', - '@context' => 'http://schema.org/extensions', - 'summary' => config('laravel_alert_notifications.microsoft_teams.cardSubject'), - 'themeColor' => config('laravel_alert_notifications.themeColor'), - 'title' => config('laravel_alert_notifications.cardSubject'), - 'sections' => [ + '@type' => 'AdaptiveCard', + '@context' => 'https://adaptivecards.io/schemas/adaptive-card.json', + 'version' => '1.2', + 'body' => [ [ - 'activityTitle' => config('laravel_alert_notifications.microsoft_teams.cardSubject'), - 'activitySubtitle' => 'Error has occurred on '.config('app.name').' - '.config('app.name'), - 'activityImage' => '', - 'facts' => [ - [ - 'name' => 'Environment:', - 'value' => config('app.env'), - ], - [ - 'name' => 'Server:', - 'value' => Request::server('SERVER_NAME'), - ], - [ - 'name' => 'Request Url:', - 'value' => Request::fullUrl(), - ], - [ - 'name' => 'Exception:', - 'value' => get_class($this->exception), - ], - [ - 'name' => 'Message:', - 'value' => $this->exception->getMessage(), - ], - [ - 'name' => 'Exception Code:', - 'value' => $this->exception->getCode(), - ], - [ - 'name' => 'In File:', - 'value' => '' - .$this->exception->getFile() - .' on line '.$this->exception->getLine().'', - ], - [ - 'name' => 'Stack Trace:', - 'value' => '
'.$this->exception->getTraceAsString().'
', - ], - [ - 'name' => 'Context:', - 'value' => '
$context = '.var_export($this->exceptionContext, true).';
', - ], - ], + 'type' => 'TextBlock', + 'text' => 'Environment: ' . config('app.env'), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Server: ' . Request::server('SERVER_NAME'), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Request Url: ' . Request::fullUrl(), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Exception: ' . get_class($this->exception), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Message: ' . $this->exception->getMessage(), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Exception Code: ' . $this->exception->getCode(), + ], + [ + 'type' => 'TextBlock', + 'text' => 'In File: ' . $this->exception->getFile() . ' on line ' . $this->exception->getLine(), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Stack Trace: ' . $this->exception->getTraceAsString(), + ], + [ + 'type' => 'TextBlock', + 'text' => 'Context: ' . var_export($this->exceptionContext, true), ], ], ]; diff --git a/tests/AlertDispatcherTest.php b/tests/AlertDispatcherTest.php index d5dbf39..a7a5cdc 100644 --- a/tests/AlertDispatcherTest.php +++ b/tests/AlertDispatcherTest.php @@ -4,6 +4,7 @@ use Exception; use Kevincobain2000\LaravelAlertNotifications\Dispatcher\AlertDispatcher; +use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\ExceptionOccurredCard; use Mail; use Mockery; use Orchestra\Testbench\TestCase; @@ -78,4 +79,23 @@ public function testBasic() $alert = new AlertDispatcher(new Exception(), []); $this->assertTrue(true); } + + public function testMicrosoftTeamsCardFormat() + { + $exception = new Exception('Test Exception'); + $card = new ExceptionOccurredCard($exception, []); + $json = $card->getCard(); + + $this->assertArrayHasKey('@context', $json); + $this->assertArrayHasKey('@type', $json); + $this->assertArrayHasKey('body', $json); + + $body = $json['body']; + $this->assertIsArray($body); + + foreach ($body as $element) { + $this->assertArrayHasKey('type', $element); + $this->assertEquals('TextBlock', $element['type']); + } + } }