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
15 changed files with 478 additions and 245 deletions

View File

@@ -53,7 +53,7 @@ class FormCleaner
'discord_webhook_url' => "Discord webhook disabled.",
'editable_submissions' => 'Users will not be able to edit their submissions.',
'custom_code' => 'Custom code was disabled',
'seo_meta' => 'Custom code was disabled',
'seo_meta' => 'Custom SEO was disabled',
// For fields
'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());
}
}