Need help sending notification if webhook fails

After working with another developer, we found that the code Gravity Forms provides in the documentation wasn’t registering that there was an error because it was only looking for whether it returned a response, not whether the response was that the webhook was successful or not (ie, not whether it was returning a 200 response vs 404, 502, etc.). So we changed up the code, and finally got this to work:

add_action(
	'gform_webhooks_post_request',
	function ( $response, $feed, $entry, $form ) {
		gf_webhooks()->log_debug( __METHOD__ . json_encode($response) );
		if ( $response['response']['code']!=200 ) {
			gf_webhooks()->log_debug( __METHOD__ . '(): Sending email for error...' );
			$error_message = $response['body'];
			$to            = 'email@emailhere.com';
			$subject       = 'Webhook failed!';
			$body          = "Webhook for entry #$entry[id] failed. Error: $error_message";
			wp_mail( $to, $subject, $body );
			gf_webhooks()->log_debug( __METHOD__ . '(): Email sent...' );
		}
	},
	10,
	4
);

Now it it registering whether the response is 200 (successful) or not, and sending the email if the response is anything other than 200.

1 Like