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

Add Microsoft Teams notifications #224

Merged
merged 2 commits into from
Mar 25, 2021
Merged
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
14 changes: 14 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Compiler
'Slack',
'Discord',
'Telegram',
'MicrosoftTeams',
];

/**
Expand Down Expand Up @@ -468,6 +469,19 @@ protected function compileTelegram($value)
return preg_replace($pattern, '$1 if (! isset($task)) $task = null; Laravel\Envoy\Telegram::make$2->task($task)->send();', $value);
}

/**
* Compile Envoy Teams statements into valid PHP.
*
* @param string $value
* @return string
*/
protected function compileMicrosoftTeams($value)
{
$pattern = $this->createMatcher('microsoftTeams');

return preg_replace($pattern, '$1 if (! isset($task)) $task = null; Laravel\Envoy\MicrosoftTeams::make$2->task($task)->send();', $value);
}

/**
* Initialize the variables included in the Envoy template.
*
Expand Down
141 changes: 141 additions & 0 deletions src/MicrosoftTeams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace Laravel\Envoy;

use GuzzleHttp\Client;

class MicrosoftTeams
{
use ConfigurationParser;

/**
* The Teams WebHook URL.
*
* @var string
*/
public $hook;

/**
* The message for Teams Custom Card.
*
* @var string
*/
public $message;

/**
* The theme color for Teams Custom Card.
*
* @var string
*/
public $theme;

/**
* The extra options for Teams Custom Card.
*
* @var array
*/
public $options;

/**
* The name of the task.
*
* @var string
*/
protected $task;

/**
* Create a new Teams instance.
*
* @param string $hook
* @param string $message
* @param string $theme
* @param array $options
* @return void
*/
public function __construct($hook, $message = null, $theme = 'success', $options = [])
{
$this->hook = $hook;
$this->message = $message;
$this->theme = $theme;
$this->options = $options;
}

/**
* Create a new Teams message instance.
*
* @param string $hook
* @param string $message
* @param string $theme
* @param array $options
* @return \Laravel\Envoy\MicrosoftTeams
*/
public static function make($hook, $message = null, $theme = 'success', $options = [])
{
return new static($hook, $message, $theme, $options);
}

/**
* Send the Teams message.
*
* @return void
*/
public function send()
{
(new Client())->post(
$this->hook,
[
'json' => $this->buildPayload(),
]
);
}

/**
* Build the payload to send to the webhook.
*
* @return array
*/
private function buildPayload()
{
$message = $this->message ?: ($this->task ? ucwords($this->getSystemUser()).' ran the ['.$this->task.'] task.' : ucwords($this->getSystemUser()).' ran a task.');

return array_merge(
[
"@context" => "https://schema.org/extensions",
"@type" => "MessageCard",
"themeColor" => $this->getTheme(),
'text' => $message
],
$this->options
);
}

/**
* Get the theme color for Teams Custom Card.
*
* @return string
*/
private function getTheme(): string
{
$themes = [
'success' => '#198754',
'info' => '#0dcaf0',
'error' => '#dc3545',
'warning' => '#fd7e14',
];

return $themes[$this->theme];
}

/**
* Set the task for the message.
*
* @param string $task
* @return $this
*/
public function task($task)
{
$this->task = $task;

return $this;
}
}