Better webhooks (#155)

* Enable Pro plan - WIP

* no pricing page if have no paid plans

* Set pricing ids in env

* views & submissions FREE for all

* extra param for env

* form password FREE for all

* Custom Code is PRO feature

* Replace codeinput prism with codemirror

* Better form Cleaning message

* Added risky user email spam protection

* fix form cleaning

* Custom SEO

* fix custom seo formcleaner

* Better webhooks

* fix test case
This commit is contained in:
formsdev 2023-08-30 16:07:08 +05:30 committed by GitHub
parent 01a01a8c72
commit ec26c211d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 478 additions and 245 deletions

View File

@ -0,0 +1,38 @@
<?php
namespace App\Listeners;
use App\Notifications\Forms\FailedWebhookNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
class FailedWebhookListener
{
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(WebhookCallFailedEvent $event)
{
// Notify form owner
if ($event->meta['type'] == 'form_submission') {
$event->meta['form']->creator->notify(new FailedWebhookNotification($event));
\Log::error('Failed form submission webhook', [
'webhook_url' => $event->webhookUrl,
'exception' => $event->errorType,
'message' => $event->errorMessage,
'form_id' => $event->meta['form']->id
]);
return;
}
\Log::error('Failed webhook', [
'webhook_url' => $event->webhookUrl,
'exception' => $event->errorType,
'message' => $event->errorMessage
]);
}
}

View File

@ -10,8 +10,8 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
use App\Service\Forms\FormSubmissionFormatter; use App\Service\Forms\FormSubmissionFormatter;
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
use App\Notifications\Forms\FormSubmissionNotification; use App\Notifications\Forms\FormSubmissionNotification;
use Vinkla\Hashids\Facades\Hashids;
class NotifyFormSubmission implements ShouldQueue class NotifyFormSubmission implements ShouldQueue
{ {
@ -25,14 +25,40 @@ class NotifyFormSubmission implements ShouldQueue
*/ */
public function handle(FormSubmitted $event) public function handle(FormSubmitted $event)
{ {
if (!$event->form->is_pro) return; $this->sendEmailNotifications($event);
if ($event->form->notifies) { $this->sendWebhookNotification($event, WebhookHandlerProvider::SIMPLE_WEBHOOK_PROVIDER);
// Send Email Notification $this->sendWebhookNotification($event, WebhookHandlerProvider::SLACK_PROVIDER);
$subscribers = collect(preg_split("/\r\n|\n|\r/", $event->form->notification_emails))->filter(function($email) { $this->sendWebhookNotification($event, WebhookHandlerProvider::DISCORD_PROVIDER);
foreach ($event->form->zappierHooks as $hook) {
$hook->triggerHook($event->data);
}
}
private function sendWebhookNotification(FormSubmitted $event, string $provider)
{
WebhookHandlerProvider::getProvider(
$event->form,
$event->data,
$provider
)->handle();
}
/**
* Sends an email to each email address in the form's notification_emails field
* @param FormSubmitted $event
* @return void
*/
private function sendEmailNotifications(FormSubmitted $event)
{
if (!$event->form->is_pro || !$event->form->notifies) return;
$subscribers = collect(preg_split("/\r\n|\n|\r/", $event->form->notification_emails))->filter(function (
$email
) {
return filter_var($email, FILTER_VALIDATE_EMAIL); return filter_var($email, FILTER_VALIDATE_EMAIL);
}); });
\Log::debug('Sending email notification',[ \Log::debug('Sending email notification', [
'recipients' => $subscribers->toArray(), 'recipients' => $subscribers->toArray(),
'form_id' => $event->form->id, 'form_id' => $event->form->id,
'form_slug' => $event->form->slug, 'form_slug' => $event->form->slug,
@ -41,149 +67,4 @@ class NotifyFormSubmission implements ShouldQueue
Notification::route('mail', $subscriber)->notify(new FormSubmissionNotification($event)); Notification::route('mail', $subscriber)->notify(new FormSubmissionNotification($event));
}); });
} }
if ($event->form->notifies_slack) {
// Send Slack Notification
$this->sendSlackNotification($event);
}
if ($event->form->notifies_discord) {
// Send Discord Notification
$this->sendDiscordNotification($event);
}
}
private function sendSlackNotification(FormSubmitted $event)
{
if($this->validateSlackWebhookUrl($event->form->slack_webhook_url)){
$submissionString = "";
$formatter = (new FormSubmissionFormatter($event->form, $event->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
$submissionString .= ">*".ucfirst($field['name'])."*: ".$tmpVal." \n";
}
$formURL = url("forms/".$event->form->slug);
$editFormURL = url("forms/".$event->form->slug."/show");
$submissionId = Hashids::encode($event->data['submission_id']);
$externalLinks = [
'*<'.$formURL.'|🔗 Open Form>*',
'*<'.$editFormURL.'|✍️ Edit Form>*'
];
if($event->form->editable_submissions){
$externalLinks[] = '*<'.$event->form->share_url.'?submission_id='.$submissionId.'|✍️ '.$event->form->editable_submissions_button_text.'>*';
}
$finalSlackPostData = [
'blocks' => [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *<'.$formURL.'|'.$event->form->title.':>*',
]
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString
]
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
]
],
]
];
WebhookCall::create()
->url($event->form->slack_webhook_url)
->doNotSign()
->payload($finalSlackPostData)
->dispatch();
}
}
private function validateSlackWebhookUrl($url)
{
return ($url) ? str_contains($url, 'https://hooks.slack.com/') : false;
}
private function sendDiscordNotification(FormSubmitted $event)
{
if($this->validateDiscordWebhookUrl($event->form->discord_webhook_url)){
$submissionString = "";
$formatter = (new FormSubmissionFormatter($event->form, $event->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
$submissionString .= "**".ucfirst($field['name'])."**: `".$tmpVal."`\n";
}
$form_name = $event->form->title;
$form = Form::find($event->form->id);
$formURL = url("forms/".$event->form->slug."/show/submissions");
$finalDiscordPostData = [
"content" => "@here We have received a new submission for **$form_name**",
"username" => config('app.name'),
"avatar_url" => asset('img/logo.png'),
"tts" => false,
"embeds" => [
[
"title" => "🔗 Go to $form_name",
"type" => "rich",
"description" => $submissionString,
"url" => $formURL,
"color" => hexdec(str_replace('#', '', $event->form->color)),
"footer" => [
"text" => config('app.name'),
"icon_url" => asset('img/logo.png'),
],
"author" => [
"name" => config('app.name'),
"url" => config('app.url'),
],
"fields" => [
[
"name" => "Views 👀",
"value" => "$form->views_count",
"inline" => true
],
[
"name" => "Submissions 🖊️",
"value" => "$form->submissions_count",
"inline" => true
]
]
]
]
];
WebhookCall::create()
->url($event->form->discord_webhook_url)
->doNotSign()
->payload($finalDiscordPostData)
->dispatch();
}
}
private function validateDiscordWebhookUrl($url)
{
return ($url) ? str_contains($url, 'https://discord.com/api/webhooks') : false;
}
} }

View File

@ -1,77 +0,0 @@
<?php
namespace App\Listeners\Forms;
use App\Events\Forms\FormSubmitted;
use App\Models\Forms\Form;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Spatie\WebhookServer\WebhookCall;
use App\Service\Forms\FormSubmissionFormatter;
use Vinkla\Hashids\Facades\Hashids;
class PostFormDataToWebhook implements ShouldQueue
{
use InteractsWithQueue;
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle(FormSubmitted $event)
{
$form = $event->form;
if (!$form->is_pro) return;
$data = $this->getWebhookData($event);
$this->sendSimpleWebhook($form, $data);
$this->sendZappierWebhooks($form, $data);
}
private function sendSimpleWebhook(Form $form, array $data) {
if ($form->webhook_url) {
\Log::debug('Sending data to webhook URL',[
'webhook_url' => $form->webhook_url,
'form_id' => $form->id,
'form_slug' => $form->slug,
]);
WebhookCall::create()
->url($form->webhook_url)
->doNotSign()
->payload($data)
->dispatch();
}
}
private function sendZappierWebhooks(Form $form, array $data) {
foreach ($form->zappierHooks as $hook) {
\Log::debug('Sending data to Zapier webhook',[
'form_id' => $form->id,
'form_slug' => $form->slug,
]);
$hook->triggerHook($data);
}
}
private function getWebhookData(FormSubmitted $event): array {
$formatter = (new FormSubmissionFormatter($event->form, $event->data))->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
$formattedData[$field['name']] = $field['value'];
}
$submissionId = Hashids::encode($event->data['submission_id']);
$data = [
'form_title' => $event->form->title,
'form_slug' => $event->form->slug,
'submission' => $formattedData
];
if($event->form->editable_submissions){
$data['edit_link'] = $event->form->share_url.'?submission_id='.$submissionId;
}
return $data;
}
}

View File

@ -140,6 +140,11 @@ class Form extends Model
return url('/forms/'.$this->slug); return url('/forms/'.$this->slug);
} }
public function getEditUrlAttribute()
{
return url('/forms/'.$this->slug.'/show');
}
public function getSubmissionsCountAttribute() public function getSubmissionsCountAttribute()
{ {
return $this->submissions()->count(); return $this->submissions()->count();

View File

@ -3,6 +3,7 @@
namespace App\Models\Integration; namespace App\Models\Integration;
use App\Models\Forms\Form; use App\Models\Forms\Form;
use App\Service\Forms\Webhooks\WebhookHandlerProvider;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -27,11 +28,13 @@ class FormZapierWebhook extends Model
return $this->belongsTo(Form::class); return $this->belongsTo(Form::class);
} }
public function triggerHook(array $data) { public function triggerHook(array $data)
WebhookCall::create() {
->url($this->hook_url) WebhookHandlerProvider::getProvider(
->doNotSign() $this->form,
->payload($data) $data,
->dispatch(); WebhookHandlerProvider::ZAPIER_PROVIDER,
$this->hook_url
)->handle();
} }
} }

View File

@ -0,0 +1,57 @@
<?php
namespace App\Notifications\Forms;
use App\Models\Forms\Form;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Str;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
class FailedWebhookNotification extends Notification
{
use Queueable;
public Form $form;
public string $provider;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(protected WebhookCallFailedEvent $event)
{
$this->form = $this->event->meta['form'];
$this->provider = $this->event->meta['provider'];
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject("Notification issue with your NotionForm: '" . $this->form->title . "'")
->markdown('mail.form.webhook-error', [
'provider' => $this->provider,
'error' => $this->event->errorMessage,
'form' => $this->form,
]);
}
}

View File

@ -4,16 +4,17 @@ namespace App\Providers;
use App\Events\Forms\FormSubmitted; use App\Events\Forms\FormSubmitted;
use App\Events\Models\FormCreated; use App\Events\Models\FormCreated;
use App\Listeners\FailedWebhookListener;
use App\Listeners\Auth\RegisteredListener; use App\Listeners\Auth\RegisteredListener;
use App\Listeners\Forms\FormCreationConfirmation; use App\Listeners\Forms\FormCreationConfirmation;
use App\Listeners\Forms\NotifyFormSubmission; use App\Listeners\Forms\NotifyFormSubmission;
use App\Listeners\Forms\PostFormDataToWebhook;
use App\Listeners\Forms\SubmissionConfirmation; use App\Listeners\Forms\SubmissionConfirmation;
use App\Notifications\Forms\FormCreatedNotification; use App\Notifications\Forms\FormCreatedNotification;
use Illuminate\Auth\Events\Registered; use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
@ -31,8 +32,10 @@ class EventServiceProvider extends ServiceProvider
], ],
FormSubmitted::class => [ FormSubmitted::class => [
NotifyFormSubmission::class, NotifyFormSubmission::class,
PostFormDataToWebhook::class,
SubmissionConfirmation::class, SubmissionConfirmation::class,
],
WebhookCallFailedEvent::class => [
FailedWebhookListener::class
] ]
]; ];

View File

@ -53,7 +53,7 @@ class FormCleaner
'discord_webhook_url' => "Discord webhook disabled.", 'discord_webhook_url' => "Discord webhook disabled.",
'editable_submissions' => 'Users will not be able to edit their submissions.', 'editable_submissions' => 'Users will not be able to edit their submissions.',
'custom_code' => 'Custom code was disabled', 'custom_code' => 'Custom code was disabled',
'seo_meta' => 'Custom code was disabled', 'seo_meta' => 'Custom SEO was disabled',
// For fields // For fields
'file_upload' => "Link field is not a file upload.", 'file_upload' => "Link field is not a file upload.",

View File

@ -0,0 +1,66 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Models\Forms\Form;
use App\Service\Forms\FormSubmissionFormatter;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Str;
use Spatie\WebhookServer\WebhookCall;
use Vinkla\Hashids\Facades\Hashids;
abstract class AbstractWebhookHandler
{
public function __construct(protected Form $form, protected array $data)
{
}
abstract protected function getProviderName(): ?string;
abstract protected function getWebhookUrl(): ?string;
/**
* Default webhook payload. Can be changed in child classes.
* @return array
*/
protected function getWebhookData(): array
{
$formatter = (new FormSubmissionFormatter($this->form, $this->data))->showHiddenFields();
$formattedData = [];
foreach ($formatter->getFieldsWithValue() as $field) {
$formattedData[$field['name']] = $field['value'];
}
$data = [
'form_title' => $this->form->title,
'form_slug' => $this->form->slug,
'submission' => $formattedData,
];
if ($this->form->is_pro && $this->form->editable_submissions) {
$data['edit_link'] = $this->form->share_url . '?submission_id=' . Hashids::encode($this->data['submission_id']);
}
return $data;
}
abstract protected function shouldRun(): bool;
public function handle()
{
if (!$this->shouldRun()) return;
WebhookCall::create()
// Add context on error, used to notify form owner
->meta([
'type' => 'form_submission',
'data' => $this->data,
'form' => $this->form,
'provider' => $this->getProviderName(),
])
->url($this->getWebhookUrl())
->doNotSign()
->payload($this->getWebhookData())
->dispatchSync();
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
class DiscordHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
{
return 'Discord';
}
protected function getWebhookUrl(): ?string
{
return $this->form->discord_webhook_url;
}
protected function getWebhookData(): array
{
$submissionString = "";
$formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
$submissionString .= "**" . ucfirst($field['name']) . "**: `" . $tmpVal . "`\n";
}
$form_name = $this->form->title;
$formURL = url("forms/" . $this->form->slug . "/show/submissions");
return [
"content" => "@here We have received a new submission for **$form_name**",
"username" => config('app.name'),
"avatar_url" => asset('img/logo.png'),
"tts" => false,
"embeds" => [
[
"title" => "🔗 Go to $form_name",
"type" => "rich",
"description" => $submissionString,
"url" => $formURL,
"color" => hexdec(str_replace('#', '', $this->form->color)),
"footer" => [
"text" => config('app.name'),
"icon_url" => asset('img/logo.png'),
],
"author" => [
"name" => config('app.name'),
"url" => config('app.url'),
],
"fields" => [
[
"name" => "Views 👀",
"value" => (string)$this->form->views_count,
"inline" => true
],
[
"name" => "Submissions 🖊️",
"value" => (string)$this->form->submissions_count,
"inline" => true
]
]
]
]
];
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl())
&& str_contains($this->getWebhookUrl(), 'https://discord.com/api/webhooks')
&& $this->form->is_pro;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
class SimpleWebhookHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
{
return 'webhook';
}
protected function getWebhookUrl(): ?string
{
return $this->form->webhook_url;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl()) && $this->form->is_pro;
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
use Vinkla\Hashids\Facades\Hashids;
class SlackHandler extends AbstractWebhookHandler
{
protected function getProviderName(): string
{
return 'Slack';
}
protected function getWebhookUrl(): ?string
{
return $this->form->slack_webhook_url;
}
protected function getWebhookData(): array
{
$submissionString = '';
$formatter = (new FormSubmissionFormatter($this->form, $this->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(',', $field['value']) : $field['value'];
$submissionString .= '>*' . ucfirst($field['name']) . '*: ' . $tmpVal . " \n";
}
$formURL = url('forms/' . $this->form->slug);
$editFormURL = url('forms/' . $this->form->slug . '/show');
$submissionId = Hashids::encode($this->data['submission_id']);
$externalLinks = [
'*<' . $formURL . '|🔗 Open Form>*',
'*<' . $editFormURL . '|✍️ Edit Form>*'
];
if ($this->form->editable_submissions) {
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
}
return [
'blocks' => [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *<' . $formURL . '|' . $this->form->title . ':>*',
],
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString,
],
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
],
],
],
];
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl())
&& str_contains($this->getWebhookUrl(), 'https://hooks.slack.com/')
&& $this->form->is_pro;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Models\Forms\Form;
class WebhookHandlerProvider
{
const SLACK_PROVIDER = 'slack';
const DISCORD_PROVIDER = 'discord';
const SIMPLE_WEBHOOK_PROVIDER = 'webhook';
const ZAPIER_PROVIDER = 'zapier';
public static function getProvider(Form $form, array $data, string $provider, ?string $webhookUrl = null)
{
switch ($provider) {
case self::SLACK_PROVIDER:
return new SlackHandler($form, $data);
case self::DISCORD_PROVIDER:
return new DiscordHandler($form, $data);
case self::SIMPLE_WEBHOOK_PROVIDER:
return new SimpleWebhookHandler($form, $data);
case self::ZAPIER_PROVIDER:
if (is_null($webhookUrl)) {
throw new \Exception('Zapier webhook url is required');
}
return new ZapierHandler($form, $data, $webhookUrl);
default:
throw new \Exception('Unknown webhook provider');
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Service\Forms\Webhooks;
use App\Models\Forms\Form;
class ZapierHandler extends AbstractWebhookHandler
{
public function __construct(protected Form $form, protected array $data, protected string $webhookUrl)
{
}
protected function getProviderName(): ?string
{
return 'zapier';
}
protected function getWebhookUrl(): string
{
return $this->webhookUrl;
}
protected function shouldRun(): bool
{
return !is_null($this->getWebhookUrl());
}
}

View File

@ -0,0 +1,15 @@
@component('mail::message')
Hello,
We tried to trigger a **{{$provider}}** notification for your form "{{$form->title}}", but it failed. Here is the error that we got:
@component('mail::panel')
{{$error}}
@endcomponent
Click [here to edit your form]({{$form->edit_url}}).
Contact us via the website live chat if you need any help.
@endcomponent