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

Added support for Microsoft Teams Notifications #14153

Merged
merged 15 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
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" => "Insert URL",
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
"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" => "Snipe-IT Integration Test Summary",
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
"title" => "Snipe-IT Integration 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
25 changes: 24 additions & 1 deletion 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,7 +37,10 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedIn
*/
public function via()
{
\Log::debug('via called');
if (Setting::getSettings()->webhook_selected == 'microsoft'){

return [MicrosoftTeamsChannel::class];
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
}
$notifyBy = [];

if (Setting::getSettings()->webhook_endpoint != '') {
Expand Down Expand Up @@ -108,6 +113,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("Accessory Checked In")
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
->fact('Checked into ', $item->location->name ? $item->location->name : '')
->fact(trans('mail.Accessory_Checkin_Notification')." by ", $admin->present()->fullName())
->fact('Number Remaining', $item->numRemaining())
->fact('Notes', $note ?: 'No notes');
}

/**
* Get the mail representation of the notification.
Expand Down
23 changes: 23 additions & 0 deletions 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 @@ -43,6 +45,10 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $not
*/
public function via()
{
if (Setting::getSettings()->webhook_selected == 'microsoft'){

return [MicrosoftTeamsChannel::class];
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
}
$notifyBy = [];
if (Setting::getSettings()->webhook_endpoint != '') {
\Log::debug('use webhook');
Expand Down Expand Up @@ -84,6 +90,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("Asset Checked in")
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
->fact('Checked into ', $item->location->name ? $item->location->name : $item->defaultLoc()->name)
->fact(trans('mail.Asset_Checkin_Notification')." by ", $admin->present()->fullName())
->fact('Asset Status', $item->assetstatus->name)
->fact('Notes', $note ?: 'No notes');
}

/**
* Get the mail representation of the notification.
Expand Down
26 changes: 26 additions & 0 deletions 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,6 +43,11 @@ public function via()
{
$notifyBy = [];

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

$notifyBy[] = MicrosoftTeamsChannel::class;
}

if (Setting::getSettings()->webhook_endpoint != '') {
$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("License Checked in")
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'header')
->fact(trans('mail.License_Checkin_Notification')." by ", $admin->present()->fullName() ?: 'ClI tool')
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
->fact('Checked in from', $target->present()->fullName())
->fact('Seats Remaining', $item->availCount()->count())
->fact('Notes', $note ?: 'No notes');
}

/**
* Get the mail representation of the notification.
Expand Down
29 changes: 27 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,13 @@ public function via()
{
$notifyBy = [];

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

// return [MicrosoftTeamsChannel::class];
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
$notifyBy[] = MicrosoftTeamsChannel::class;
}

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

Expand Down Expand Up @@ -96,6 +103,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("Accessory Checked Out")
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityTitle')
->fact('Checked out from ', $item->location->name ? $item->location->name : '')
->fact(trans('mail.Accessory_Checkout_Notification')." by ", $admin->present()->fullName())
->fact('Number Remaining', $item->numRemaining())
->fact('Notes', $note ?: 'No notes');
}

/**
* Get the mail representation of the notification.
Expand Down
31 changes: 31 additions & 0 deletions 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,6 +54,10 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $ac
*/
public function via()
{
if (Setting::getSettings()->webhook_selected == 'microsoft'){

return [MicrosoftTeamsChannel::class];
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved
}
$notifyBy = [];

if ((Setting::getSettings()) && (Setting::getSettings()->webhook_endpoint != '')) {
Expand Down Expand Up @@ -117,6 +124,30 @@ public function toSlack()
->content($note);
});
}
public function toMicrosoftTeams()
{
$admin = $this->admin;
$item = $this->item;
$note = $this->note;

try {

return MicrosoftTeamsMessage::create()
->to($this->settings->webhook_endpoint)
->type('success')
->title("Asset Checked Out")
->addStartGroupToSection('activityText')
->fact(htmlspecialchars_decode($item->present()->name), '', 'activityText')
->fact('Checked out from ', $item->location ? $item->location->name : $item->assetLoc()->name)
->fact(trans('mail.Asset_Checkout_Notification') . " by ", $admin->present()->fullName())
->fact('Asset Status', $item->assetstatus->name)
->fact('Notes', $note ?: 'No notes');
}
catch(Exception $e) {
dd($e);
Godmartinz marked this conversation as resolved.
Show resolved Hide resolved

}
}

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