Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Microsoft Teams webhook JSON format #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}
}
Loading