-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathms_teams.php
More file actions
74 lines (65 loc) · 2.48 KB
/
ms_teams.php
File metadata and controls
74 lines (65 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Deployer;
use Deployer\Exception\GracefulShutdownException;
use Deployer\Utility\Httpie;
task('msteams:notify', function () {
sendMessage(get('msteams_text'), get('msteams_color'));
})->desc('Send ms teams notification');
/**
* @param string $message Message to be displayed in MS Teams.
* @param string $color Highlight color. Not in use with webhook v2.
* @return void
* @throws GracefulShutdownException
*/
function sendMessage(string $message = '', string $color = ''): void
{
if (!empty(getenv('DEPLOYER_CONFIG_NOTIFICATION_MUTE'))) {
debug("skipping notification because of DEPLOYER_CONFIG_NOTIFICATION_MUTE environment variable");
return;
}
$msTeamsWebhook = has('msteams_webhook') ? get('msteams_webhook') : getenv('DEPLOYER_CONFIG_NOTIFICATION_MSTEAMS_WEBHOOK');
if (!$msTeamsWebhook) {
throw new GracefulShutdownException('Missing MS Teams webhook for notification task. Use "msteams_webhook" deployer configuration or "DEPLOYER_CONFIG_NOTIFICATION_MSTEAMS_WEBHOOK" environment variable to define the necessary webhook url.');
}
if (has('msteams_webhook_version') && get('msteams_webhook_version') == 2) {
// Make it possible to define entirely customized messages and only use the simple card as a fallback.
if (has('msteams_request_body')) {
$body = get('msteams_request_body');
} else {
$body = json_encode([
"type" => "message",
"attachments" => [
[
"contentType" => "application/vnd.microsoft.card.adaptive",
"content" => [
"\$schema" => "http://adaptivecards.io/schemas/adaptive-card.json",
"type" => "AdaptiveCard",
"version" => "1.4",
"msTeams" => [
'width' => "full",
],
"body" => [
[
"type" => "TextBlock",
"text" => $message,
"size" => "medium",
"wrap" => true,
],
],
],
],
],
], \JSON_UNESCAPED_SLASHES);
}
} else {
$body = json_encode([
"themeColor" => $color,
'text' => $message
]);
}
$curlTimeout = getenv('DEPLOYER_CONFIG_NOTIFICATION_MSTEAMS_TIMEOUT') ?: 10;
Httpie::post(get('msteams_webhook'))
->setopt(CURLOPT_TIMEOUT, $curlTimeout)
->body($body)
->send();
}