1010use Illuminate \Notifications \Notification ;
1111use Illuminate \Support \Facades \Log ;
1212use Illuminate \Support \Str ;
13+ use NotificationChannels \GoogleChat \Card ;
14+ use NotificationChannels \GoogleChat \GoogleChatChannel ;
15+ use NotificationChannels \GoogleChat \GoogleChatMessage ;
16+ use NotificationChannels \GoogleChat \Section ;
17+ use NotificationChannels \GoogleChat \Widgets \KeyValue ;
1318use NotificationChannels \MicrosoftTeams \MicrosoftTeamsChannel ;
1419use NotificationChannels \MicrosoftTeams \MicrosoftTeamsMessage ;
1520use Symfony \Component \Mime \Email ;
@@ -33,6 +38,10 @@ public function __construct($params)
3338 //
3439 $ this ->settings = Setting::getSettings ();
3540 $ this ->params = $ params ;
41+ $ item = $ params ['item ' ];
42+ if (!$ item || !is_object ($ item )) {
43+ throw new \InvalidArgumentException ('Notification requires a valid item. ' );
44+ }
3645 }
3746
3847 /**
@@ -51,6 +60,10 @@ public function via()
5160
5261 $ notifyBy [] = MicrosoftTeamsChannel::class;
5362 }
63+ if (Setting::getSettings ()->webhook_selected == 'google ' && Setting::getSettings ()->webhook_endpoint ) {
64+ Log::debug ('using google webhook ' );
65+ $ notifyBy [] = GoogleChatChannel::class;
66+ }
5467 return $ notifyBy ;
5568 }
5669
@@ -84,21 +97,57 @@ public static function toMicrosoftTeams($params)
8497 $ location = $ params ['location ' ] ?? '' ;
8598 $ setting = Setting::getSettings ();
8699
100+ //if somehow a notification triggers without an item, bail out.
101+ if (!$ item || !is_object ($ item )){
102+ return null ;
103+ }
104+
87105 if (!Str::contains ($ setting ->webhook_endpoint , 'workflows ' )) {
88106 return MicrosoftTeamsMessage::create ()
89107 ->to ($ setting ->webhook_endpoint )
90108 ->type ('success ' )
91- ->title (class_basename (get_class ( $ params [ ' item ' ])) .' ' .trans ('general.audited ' ))
109+ ->title (class_basename ($ item) .' ' .trans ('general.audited ' ))
92110 ->addStartGroupToSection ('activityText ' )
93111 ->fact (trans ('mail.asset ' ), $ item )
94112 ->fact (trans ('general.administrator ' ), $ admin_user ->present ()->viewUrl () . '| ' . $ admin_user ->display_name );
95113 }
96- $ message = class_basename (get_class ($ params ['item ' ])) . ' Audited By ' .$ admin_user ->display_name ;
114+ $ message = class_basename (get_class ($ params ['item ' ])) . trans ( ' general.audited_by ' ). ' ' .$ admin_user ->display_name ;
97115 $ details = [
98116 trans ('mail.asset ' ) => htmlspecialchars_decode ($ item ->display_name ),
99117 trans ('mail.notes ' ) => $ note ?: '' ,
100118 trans ('general.location ' ) => $ location ?: '' ,
101119 ];
102120 return [$ message , $ details ];
103121 }
122+ public function toGoogleChat ()
123+ {
124+ $ item = $ this ->params ['item ' ] ?? null ;
125+ $ admin_user = $ this ->params ['admin ' ] ?? null ;
126+ $ note = $ this ->params ['note ' ] ?? '' ;
127+ $ setting = $ this ->settings ?? Setting::getSettings ();
128+
129+ $ title = '<strong> ' . class_basename ($ item ) . ' ' . trans ('general.audited ' ) . '</strong> ' ;
130+ $ subtitle = htmlspecialchars_decode ($ item ->display_name ?? '' );
131+ \Log::debug ('Google Chat audit payload ' , [
132+ 'title ' => $ title ,
133+ 'subtitle ' => $ subtitle ,
134+ 'admin ' => $ admin_user ->display_name ,
135+ 'note ' => $ note ,
136+ ]);
137+ return GoogleChatMessage::create ()
138+ ->to ($ setting ->webhook_endpoint )
139+ ->card (
140+ Card::create ()
141+ ->header ($ title , $ subtitle )
142+ ->section (
143+ Section::create (
144+ KeyValue::create (
145+ trans ('general.audited_by ' ),
146+ $ admin_user ?->display_name ?? '' ,
147+ $ note ?? ''
148+ )->onClick (route ('hardware.show ' , $ item ->id ))
149+ )
150+ )
151+ );
152+ }
104153}
0 commit comments