Support » Plugin: Five Star Restaurant Reservations » Best way to call a webhook when mails get send

  • Resolved rousseauxy

    (@rousseauxy)


    I was wondering what the best way is to integrate a webhook when a mail is send?
    Before april i had an IFTTT applet that send an sms when a mail arrived, since that’s no longer possible i was thinking of using the webhook functionality of IFTTT.
    In what action would this best be implemented?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @rousseauxy,

    Can I clarify that when you say “webhook” do you mean you want your WordPress site to send a request to a remote URL when certain events happen?

    I’d recommend that you hook into the rtb_send_notification_after action. That would look like this:

    
    add_action( 'rtb_send_notification_after', function( $notification ) {
      $event = $notification->event; // eg - new_submission, or pending_to_confirmed
      $booking = $notification->booking;
      $args = [...your webhook payload...];
      wp_remote_get('http://your-remote-web-hook.com', $args );
      // you can use wp_remote_request if you need to send headers
    } );
    

    That will fire whenever an email notification is sent, and you may want to limit what events fire off the remote request. You can see all of the email notifications registered in the system here:

    https://github.com/NateWr/restaurant-reservations/blob/master/includes/Notifications.class.php#L63-L73

    Beware that this remote request will be completed before the booking request is returned to the user. So if IFTTT’s server takes 3 seconds to respond to your request, it will take 3 extra seconds to load the page after the user submits the booking form.

    • This reply was modified 5 months, 2 weeks ago by  NateWr.

    That works wonders!
    It does however trigger the webhook twice, probably because it sends two emails/notifications.
    Is there another hook that would only trigger this once? Already tried changing the event from submission to pending but didn’t do much.

    You can ignore my last reply 🙂
    Made a small mistake, got it working perfectly with following code

    
    add_action( 'rtb_send_notification_after', function( $notification ) {
      $event = $notification->event; // eg - new_submission, or pending_to_confirmed
      $booking = $notification->booking;
      $booking_date = date_i18n( 'l j F Y' , strtotime( $booking->date ) );
      $booking_time = date_i18n( get_option( 'time_format' ), strtotime( $booking->date ) );
      $reservatie = 'Reservatie ' . $booking_date . ' om ' . $booking_time . ' voor ' . $booking->party . ' personen van ' . $booking->name .' (' . $booking->phone . ')';
      $url = 'https://maker.ifttt.com/trigger/<trigger>/with/key/<key>';
      $url .= '?value1=' . $reservatie;
      if($booking->message!=""){
        $url .= '&value2=Bericht: ' . $booking->message;
      }
    	
      if($notification->event == 'new_submission' && $notification->target == 'admin'){
    	  wp_remote_get($url);
      }
      }
    );
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.