Skip to content

Commit

Permalink
Merge pull request #14191 from Godmartinz/googlechat_webhook
Browse files Browse the repository at this point in the history
Added support for Google Chat notifications
  • Loading branch information
snipe authored Feb 5, 2024
2 parents b6eea2e + 9dc428b commit 650aa25
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 27 deletions.
57 changes: 52 additions & 5 deletions app/Http/Livewire/SlackSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,33 @@ class SlackSettingsForm extends Component

public function mount() {
$this->webhook_text= [
"slack" => array(
"slack" => array(
"name" => trans('admin/settings/general.slack') ,
"icon" => 'fab fa-slack',
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
"link" => 'https://api.slack.com/messaging/webhooks',
"icon" => 'fab fa-slack',
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
"link" => 'https://api.slack.com/messaging/webhooks',
"test" => "testWebhook"
),
"general"=> array(
"name" => trans('admin/settings/general.general_webhook'),
"icon" => "fab fa-hashtag",
"placeholder" => trans('general.url'),
"link" => "",
"test" => "testWebhook"
),
"google" => array(
"name" => trans('admin/settings/general.google_workspaces'),
"icon" => "fa-brands fa-google",
"placeholder" => "https://chat.googleapis.com/v1/spaces/xxxxxxxx/messages?key=xxxxxx",
"link" => "https://developers.google.com/chat/how-tos/webhooks#register_the_incoming_webhook",
"test" => "googleWebhookTest"
),
"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",
"test" => "msTeamTestWebhook"
),
];

Expand All @@ -64,10 +74,14 @@ public function mount() {
$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_test = $this->webhook_text[$this->setting->webhook_selected]["test"];
$this->webhook_endpoint = $this->setting->webhook_endpoint;
$this->webhook_channel = $this->setting->webhook_channel;
$this->webhook_botname = $this->setting->webhook_botname;
$this->webhook_options = $this->setting->webhook_selected;
if($this->webhook_selected == 'microsoft' || $this->webhook_selected == 'google'){
$this->webhook_channel = '#NA';
}


if($this->setting->webhook_endpoint != null && $this->setting->webhook_channel != null){
Expand All @@ -87,10 +101,14 @@ public function updatedWebhookSelected() {
$this->webhook_placeholder = $this->webhook_text[$this->webhook_selected]["placeholder"];
$this->webhook_endpoint = null;
$this->webhook_link = $this->webhook_text[$this->webhook_selected]["link"];
$this->webhook_test = $this->webhook_text[$this->webhook_selected]["test"];
if($this->webhook_selected != 'slack'){
$this->isDisabled= '';
$this->save_button = trans('general.save');
}
if($this->webhook_selected == 'microsoft' || $this->webhook_selected == 'google'){
$this->webhook_channel = '#NA';
}

}

Expand Down Expand Up @@ -151,6 +169,7 @@ public function testWebhook(){

}


public function clearSettings(){

if (Helper::isDemoMode()) {
Expand Down Expand Up @@ -187,7 +206,35 @@ public function submit()
}

}
public function msTeamTestWebhook(){
public function googleWebhookTest(){

$payload = [
"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]));
}
}
public function msTeamTestWebhook(){

$payload =
[
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CheckoutableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function onCheckedOut($event)
if ($this->shouldSendWebhookNotification()) {

//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(Setting::getSettings()->webhook_selected =='slack' || Setting::getSettings()->webhook_selected =='general') {

Notification::route('slack', Setting::getSettings()->webhook_endpoint)
->notify($this->getCheckoutNotification($event));
Expand Down
35 changes: 35 additions & 0 deletions app/Notifications/CheckinAccessoryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\GoogleChat\Card;
use NotificationChannels\GoogleChat\GoogleChatChannel;
use NotificationChannels\GoogleChat\GoogleChatMessage;
use NotificationChannels\GoogleChat\Section;
use NotificationChannels\GoogleChat\Widgets\KeyValue;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

Expand Down Expand Up @@ -38,6 +43,10 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedIn
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_selected == 'google'){

$notifyBy[] = GoogleChatChannel::class;
}

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

Expand Down Expand Up @@ -132,6 +141,32 @@ public function toMicrosoftTeams()
->fact(trans('admin/consumables/general.remaining'), $item->numRemaining())
->fact(trans('mail.notes'), $note ?: '');
}
public function toGoogleChat()
{
$item = $this->item;
$note = $this->note;

return GoogleChatMessage::create()
->to($this->settings->webhook_endpoint)
->card(
Card::create()
->header(
'<strong>'.trans('mail.Accessory_Checkin_Notification').'</strong>' ?: '',
htmlspecialchars_decode($item->present()->name) ?: '',
)
->section(
Section::create(
KeyValue::create(
trans('mail.checked_into').': '.$item->location->name ? $item->location->name : '',
trans('admin/consumables/general.remaining').': '.$item->numRemaining(),
trans('admin/hardware/form.notes').": ".$note ?: '',
)
->onClick(route('accessories.show', $item->id))
)
)
);

}

/**
* Get the mail representation of the notification.
Expand Down
36 changes: 36 additions & 0 deletions app/Notifications/CheckinAssetNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\GoogleChat\Card;
use NotificationChannels\GoogleChat\GoogleChatChannel;
use NotificationChannels\GoogleChat\GoogleChatMessage;
use NotificationChannels\GoogleChat\Section;
use NotificationChannels\GoogleChat\Widgets\KeyValue;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

Expand Down Expand Up @@ -46,6 +51,10 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $not
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_selected == 'google'){

$notifyBy[] = GoogleChatChannel::class;
}

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

Expand Down Expand Up @@ -108,6 +117,33 @@ public function toMicrosoftTeams()
->fact(trans('admin/hardware/form.status'), $item->assetstatus->name)
->fact(trans('mail.notes'), $note ?: '');
}
public function toGoogleChat()
{
$target = $this->target;
$item = $this->item;
$note = $this->note;

return GoogleChatMessage::create()
->to($this->settings->webhook_endpoint)
->card(
Card::create()
->header(
'<strong>'.trans('mail.Asset_Checkin_Notification').'</strong>' ?: '',
htmlspecialchars_decode($item->present()->name) ?: '',
)
->section(
Section::create(
KeyValue::create(
trans('mail.checked_into') ?: '',
$item->location->name ? $item->location->name : '',
trans('admin/hardware/form.status').": ".$item->assetstatus->name,
)
->onClick(route('hardware.show', $item->id))
)
)
);

}

/**
* Get the mail representation of the notification.
Expand Down
37 changes: 37 additions & 0 deletions app/Notifications/CheckinLicenseSeatNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\GoogleChat\Card;
use NotificationChannels\GoogleChat\GoogleChatChannel;
use NotificationChannels\GoogleChat\GoogleChatMessage;
use NotificationChannels\GoogleChat\Section;
use NotificationChannels\GoogleChat\Widgets\KeyValue;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

Expand Down Expand Up @@ -43,6 +48,10 @@ public function via()
{
$notifyBy = [];

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

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

$notifyBy[] = MicrosoftTeamsChannel::class;
Expand Down Expand Up @@ -113,6 +122,34 @@ public function toMicrosoftTeams()
->fact(trans('admin/consumables/general.remaining'), $item->availCount()->count())
->fact(trans('mail.notes'), $note ?: '');
}
public function toGoogleChat()
{
$target = $this->target;
$item = $this->item;
$note = $this->note;

return GoogleChatMessage::create()
->to($this->settings->webhook_endpoint)
->card(
Card::create()
->header(
'<strong>'.trans('mail.License_Checkin_Notification').'</strong>' ?: '',
htmlspecialchars_decode($item->present()->name) ?: '',
)
->section(
Section::create(
KeyValue::create(
trans('mail.checkedin_from') ?: '',
$target->present()->fullName() ?: '',
trans('admin/consumables/general.remaining').': '.$item->availCount()->count(),
)
->onClick(route('licenses.show', $item->id))
)
)
);

}


/**
* Get the mail representation of the notification.
Expand Down
37 changes: 37 additions & 0 deletions app/Notifications/CheckoutAccessoryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\GoogleChat\Card;
use NotificationChannels\GoogleChat\GoogleChatChannel;
use NotificationChannels\GoogleChat\GoogleChatMessage;
use NotificationChannels\GoogleChat\Section;
use NotificationChannels\GoogleChat\Widgets\KeyValue;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsChannel;
use NotificationChannels\MicrosoftTeams\MicrosoftTeamsMessage;

Expand Down Expand Up @@ -37,6 +42,10 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOu
public function via()
{
$notifyBy = [];
if (Setting::getSettings()->webhook_selected == 'google'){

$notifyBy[] = GoogleChatChannel::class;
}

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

Expand Down Expand Up @@ -123,6 +132,34 @@ public function toMicrosoftTeams()
->fact(trans('mail.notes'), $note ?: '');

}
public function toGoogleChat()
{
$target = $this->target;
$item = $this->item;
$note = $this->note;

return GoogleChatMessage::create()
->to($this->settings->webhook_endpoint)
->card(
Card::create()
->header(
'<strong>'.trans('mail.Accessory_Checkout_Notification').'</strong>' ?: '',
htmlspecialchars_decode($item->present()->name) ?: '',
)
->section(
Section::create(
KeyValue::create(
trans('mail.assigned_to') ?: '',
$target->present()->name ?: '',
trans('admin/consumables/general.remaining').": ". $item->numRemaining(),
)
->onClick(route('users.show', $target->id))
)
)
);

}


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

0 comments on commit 650aa25

Please sign in to comment.