Skip to content

Commit

Permalink
Update Microsoft Teams webhook JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jul 10, 2024
1 parent e59839f commit 1dffb51
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 50 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<h1 align="center">
Laravel Alert Notifcations
</h1>
Expand Down Expand Up @@ -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

Expand Down
87 changes: 38 additions & 49 deletions src/MicrosoftTeams/ExceptionOccurredCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<b style="color:red;">'
.$this->exception->getFile()
.' on line '.$this->exception->getLine().'</b>',
],
[
'name' => 'Stack Trace:',
'value' => '<pre>'.$this->exception->getTraceAsString().'</pre>',
],
[
'name' => 'Context:',
'value' => '<pre>$context = '.var_export($this->exceptionContext, true).';</pre>',
],
],
'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),
],
],
];
Expand Down
20 changes: 20 additions & 0 deletions tests/AlertDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Kevincobain2000\LaravelAlertNotifications\Dispatcher\AlertDispatcher;
use Kevincobain2000\LaravelAlertNotifications\MicrosoftTeams\ExceptionOccurredCard;
use Mail;
use Mockery;
use Orchestra\Testbench\TestCase;
Expand Down Expand Up @@ -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']);
}
}
}

0 comments on commit 1dffb51

Please sign in to comment.