anonymouse703's avatar

How to make a Laravel notification for Github push event?

Hello everyone, I'm already depressed in searching but I don't find the right answer....

Anyways, How to make a real time notification for Github push event?

  1. If I will used the Laravel Notification (Make:Notification) what is the configuration of it? from user model -> route -> notification.php

  2. If I will install guzzle where to put the guzzle request for the webhook url?

Any ideas?

Thank you and God bless.

0 likes
4 replies
D9705996's avatar

You would use the notify method wherever in your workflow you want the notification to be generated. This could be in a route, controller, model, etc. Just whatever fits you application

https://laravel.com/docs/5.7/notifications#sending-notifications

I'm not sure from you description exactly what you are trying to do. Are you expecting your application to do something when a push vevent is genertaed in GitHub?

Can you elaborate and if possible show some exampel code that you have tried. Please ensure you format your code to help readability

anonymouse703's avatar

@D9705996 - Hello sir,, Yeah push generated from github.... webhook in github...

I already change the implementation I don't used guzzle since github has it's own event listener.... I only used ngrok...

The problem now is it doesn't notified the admin user...

here are the codes:

in my controller:

public function handle(Request $request){

        $data = $request->all();
        \Log::info($data);
        auth()->user()->notify(new WebhookNotification($data));

    }

and my route:

Route::post('hook', 'WebhookController@handle');

and my webhook payload url in github is like this..

https://33d553164.ngrok.io/api/hook (push)

What I missed? or my controller is wrong?

D9705996's avatar

@ANONYMOUSE703 - You would need to identify if the hook route is being hit by the webhook from GitHub. The first thing I would do is check php artisan route:list and confirm that there is a POST route for api/hook in the output.

If you see a route then I would look at using laravel dump server if you are using Larvel 5.7 and change your controller function to

    public function handle(Request $request){
    dump($request);
    }

If you start the server on the command line when you generate a push event in GitHub you should see the content of the request in the terminal. If not then the request isn't getting to the Controller. I would double check for any errors in storage/logs/laravel.log e.g. 500 server errors

You might also find that your middleware is preventing access. Remember that when you push the event you will not have an authenticated user so if you are using the auth or api:auth middleware they will reject the request. Try disabling them temporarily.

If none of these work it might be an issues with using ngrok. If possible I would try with HTTP instead of HTTPS when testing (assuming you aren't passing sensitive data) as you can inspect the traffic on the tunnel. Obviously in production when using a real server you would revert to HTTPS

Once you get to the point that you can see the event in Laravel you can deal with your notifications. Again bear in mind that you wont have an authenticated user so you will probably have to have some other setup for who to notify e.g. a database table/eloquent model that stores the user ids of the users to notify and you can then do a lookup on the database when you receive the web hook. However lets cross that bridge when you get to it.

anonymouse703's avatar

@D9705996 I already get the notification.... However I need to refresh the page to popup the notification... Is there any way to automatically popup the notification without reloading/refreshing the page?

Please or to participate in this conversation.