Skip to content

Commit

Permalink
Merge pull request #14153 from Godmartinz/ms_teams_webhook
Browse files Browse the repository at this point in the history
Added support for Microsoft Teams Notifications
  • Loading branch information
snipe authored Jan 25, 2024
2 parents 45b253a + c65b32b commit ab926f2
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 64 deletions.
48 changes: 45 additions & 3 deletions app/Http/Livewire/SlackSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Livewire;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Http;
use Livewire\Component;
use App\Models\Setting;
use App\Helpers\Helper;
Expand Down Expand Up @@ -45,17 +46,24 @@ public function mount() {
"general"=> array(
"name" => trans('admin/settings/general.general_webhook'),
"icon" => "fab fa-hashtag",
"placeholder" => "",
"placeholder" => trans('general.url'),
"link" => "",
),
"microsoft" => array(
"name" => trans('admin/settings/general.ms_teams'),
"icon" => "fa-brands fa-microsoft",
"placeholder" => "https://abcd.webhook.office.com/webhookb2/XXXXXXX",
"link" => "https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook?tabs=dotnet#create-incoming-webhooks-1",
),
];

$this->setting = Setting::getSettings();
$this->save_button = trans('general.save');
$this->webhook_selected = $this->setting->webhook_selected;
$this->webhook_placeholder = $this->webhook_text[$this->setting->webhook_selected]["placeholder"];
$this->webhook_name = $this->webhook_text[$this->setting->webhook_selected]["name"];
$this->webhook_icon = $this->webhook_text[$this->setting->webhook_selected]["icon"];
$this->webhook_placeholder = $this->webhook_text[$this->setting->webhook_selected]["placeholder"];
$this->webhook_link = $this->webhook_text[$this->setting->webhook_selected]["link"];
$this->webhook_endpoint = $this->setting->webhook_endpoint;
$this->webhook_channel = $this->setting->webhook_channel;
$this->webhook_botname = $this->setting->webhook_botname;
Expand All @@ -77,11 +85,13 @@ public function updatedWebhookSelected() {
$this->webhook_name = $this->webhook_text[$this->webhook_selected]['name'];
$this->webhook_icon = $this->webhook_text[$this->webhook_selected]["icon"]; ;
$this->webhook_placeholder = $this->webhook_text[$this->webhook_selected]["placeholder"];
$this->webhook_endpoint = null;
$this->webhook_link = $this->webhook_text[$this->webhook_selected]["link"];
if($this->webhook_selected != 'slack'){
$this->isDisabled= '';
$this->save_button = trans('general.save');
}

}

private function isButtonDisabled() {
Expand Down Expand Up @@ -174,8 +184,40 @@ public function submit()
$this->setting->save();

session()->flash('success',trans('admin/settings/message.update.success'));

}

}
public function msTeamTestWebhook(){

$payload =
[
"@type" => "MessageCard",
"@context" => "http://schema.org/extensions",
"summary" => trans('mail.snipe_webhook_summary'),
"title" => trans('mail.snipe_webhook_test'),
"text" => trans('general.webhook_test_msg', ['app' => $this->webhook_name]),
];

try {
$response = Http::withHeaders([
'content-type' => 'applications/json',
])->post($this->webhook_endpoint,
$payload)->throw();

if(($response->getStatusCode() == 302)||($response->getStatusCode() == 301)){
return session()->flash('error' , trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint]));
}
$this->isDisabled='';
$this->save_button = trans('general.save');
return session()->flash('success' , trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name]));

} catch (\Exception $e) {

$this->isDisabled='disabled';
$this->save_button = trans('admin/settings/general.webhook_presave');
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
}

return session()->flash('error' , trans('admin/settings/message.webhook.error_misc'));
}
}
22 changes: 16 additions & 6 deletions app/Listeners/CheckoutableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ public function onCheckedOut($event)
}

if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckoutNotification($event));

//slack doesn't include the url in its messaging format so this is needed to hit the endpoint
if(Setting::getSettings()->webhook_selected =='slack') {

Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckoutNotification($event));
}
}
} catch (ClientException $e) {
Log::debug("Exception caught during checkout notification: " . $e->getMessage());
Expand Down Expand Up @@ -107,11 +112,15 @@ public function onCheckedIn($event)
$this->getCheckinNotification($event)
);
}
//slack doesn't include the url in its messaging format so this is needed to hit the endpoint
if(Setting::getSettings()->webhook_selected =='slack') {

if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckinNotification($event));
if ($this->shouldSendWebhookNotification()) {
Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckinNotification($event));
}
}

} catch (ClientException $e) {
Log::debug("Exception caught during checkout notification: " . $e->getMessage());
} catch (Exception $e) {
Expand Down Expand Up @@ -216,9 +225,10 @@ private function getCheckoutNotification($event, $acceptance = null)
break;
case LicenseSeat::class:
$notificationClass = CheckoutLicenseSeatNotification::class;
break;
break;
}


return new $notificationClass($event->checkoutable, $event->checkedOutTo, $event->checkedOutBy, $acceptance, $event->note);
}

Expand Down
28 changes: 26 additions & 2 deletions app/Notifications/CheckinAccessoryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class CheckinAccessoryNotification extends Notification
{
Expand All @@ -35,10 +37,14 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedIn
*/
public function via()
{
\Log::debug('via called');
$notifyBy = [];

if (Setting::getSettings()->webhook_endpoint != '') {
if (Setting::getSettings()->webhook_selected == 'microsoft'){

$notifyBy[] = MicrosoftTeamsChannel::class;
}

if (Setting::getSettings()->webhook_selected == 'slack') {
$notifyBy[] = 'slack';
}

Expand Down Expand Up @@ -108,6 +114,24 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->addStartGroupToSection('activityTitle')
->title(trans('Accessory_Checkin_Notification'))
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
->fact(trans('mail.checked_into'), $item->location->name ? $item->location->name : '')
->fact(trans('mail.Accessory_Checkin_Notification')." by ", $admin->present()->fullName())
->fact(trans('admin/consumables/general.remaining'), $item->numRemaining())
->fact(trans('mail.notes'), $note ?: '');
}

/**
* Get the mail representation of the notification.
Expand Down
26 changes: 25 additions & 1 deletion app/Notifications/CheckinAssetNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class CheckinAssetNotification extends Notification
{
Expand Down Expand Up @@ -44,7 +46,12 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $not
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_endpoint != '') {

if (Setting::getSettings()->webhook_selected == 'microsoft'){

$notifyBy[] = MicrosoftTeamsChannel::class;
}
if (Setting::getSettings()->webhook_selected == 'slack') {
\Log::debug('use webhook');
$notifyBy[] = 'slack';
}
Expand Down Expand Up @@ -84,6 +91,23 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->title(trans('mail.Asset_Checkin_Notification'))
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
->fact(trans('mail.checked_into'), $item->location->name ? $item->location->name : '')
->fact(trans('mail.Asset_Checkin_Notification')." by ", $admin->present()->fullName())
->fact(trans('admin/hardware/form.status'), $item->assetstatus->name)
->fact(trans('mail.notes'), $note ?: '');
}

/**
* Get the mail representation of the notification.
Expand Down
28 changes: 27 additions & 1 deletion app/Notifications/CheckinLicenseSeatNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class CheckinLicenseSeatNotification extends Notification
{
Expand Down Expand Up @@ -41,7 +43,12 @@ public function via()
{
$notifyBy = [];

if (Setting::getSettings()->webhook_endpoint != '') {
if (Setting::getSettings()->webhook_selected == 'microsoft'){

$notifyBy[] = MicrosoftTeamsChannel::class;
}

if (Setting::getSettings()->webhook_selected == 'slack') {
$notifyBy[] = 'slack';
}

Expand Down Expand Up @@ -87,6 +94,25 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->addStartGroupToSection('activityTitle')
->title(trans('mail.License_Checkin_Notification'))
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'header')
->fact(trans('mail.License_Checkin_Notification')." by ", $admin->present()->fullName() ?: 'CLI tool')
->fact(trans('mail.checkedin_from'), $target->present()->fullName())
->fact(trans('admin/consumables/general.remaining'), $item->availCount()->count())
->fact(trans('mail.notes'), $note ?: '');
}

/**
* Get the mail representation of the notification.
Expand Down
31 changes: 29 additions & 2 deletions app/Notifications/CheckoutAccessoryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class CheckoutAccessoryNotification extends Notification
{
Expand All @@ -24,7 +26,6 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOu
$this->note = $note;
$this->target = $checkedOutTo;
$this->acceptance = $acceptance;

$this->settings = Setting::getSettings();
}

Expand All @@ -37,7 +38,12 @@ public function via()
{
$notifyBy = [];

if (Setting::getSettings()->webhook_endpoint != '') {
if (Setting::getSettings()->webhook_selected == 'microsoft'){

$notifyBy[] = MicrosoftTeamsChannel::class;
}

if (Setting::getSettings()->webhook_selected == 'slack') {
$notifyBy[] = 'slack';
}

Expand Down Expand Up @@ -96,6 +102,27 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->addStartGroupToSection('activityTitle')
->title(trans('mail.Accessory_Checkout_Notification'))
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
->fact(trans('mail.assigned_to'), $target->present()->name)
->fact(trans('mail.checkedout_from'), $item->location->name ? $item->location->name : '')
->fact(trans('mail.Accessory_Checkout_Notification') . " by ", $admin->present()->fullName())
->fact(trans('admin/consumables/general.remaining'), $item->numRemaining())
->fact(trans('mail.notes'), $note ?: '');

}

/**
* Get the mail representation of the notification.
Expand Down
28 changes: 27 additions & 1 deletion app/Notifications/CheckoutAssetNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use App\Models\Asset;
use App\Models\Setting;
use App\Models\User;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

class CheckoutAssetNotification extends Notification
{
Expand Down Expand Up @@ -51,9 +54,13 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $ac
*/
public function via()
{
if (Setting::getSettings()->webhook_selected == 'microsoft'){

return [MicrosoftTeamsChannel::class];
}
$notifyBy = [];

if ((Setting::getSettings()) && (Setting::getSettings()->webhook_endpoint != '')) {
if ((Setting::getSettings()) && (Setting::getSettings()->webhook_selected == 'slack')) {
\Log::debug('use webhook');
$notifyBy[] = 'slack';
}
Expand Down Expand Up @@ -117,6 +124,25 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$target = $this->target;
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->title(trans('mail.Asset_Checkout_Notification'))
->addStartGroupToSection('activityText')
->fact(trans('mail.assigned_to'), $target->present()->name)
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
->fact(trans('mail.Asset_Checkout_Notification') . " by ", $admin->present()->fullName())
->fact(trans('mail.notes'), $note ?: '');


}

/**
* Get the mail representation of the notification.
Expand Down
Loading

0 comments on commit ab926f2

Please sign in to comment.