Slack-Discord extra feature (#176)

* 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

* Slack-Discord extra feature

* fix conflict
This commit is contained in:
formsdev
2023-08-30 17:50:14 +05:30
committed by GitHub
parent 057bfde8b7
commit 662088e20f
12 changed files with 248 additions and 111 deletions

View File

@@ -43,6 +43,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'use_captcha' => 'boolean',
'slack_webhook_url' => 'url|nullable',
'discord_webhook_url' => 'url|nullable',
'notification_settings' => 'nullable',
// Customization
'theme' => ['required',Rule::in(Form::THEMES)],

View File

@@ -47,6 +47,7 @@ class FormResource extends JsonResource
'notification_emails' => $this->notification_emails,
'slack_webhook_url' => $this->slack_webhook_url,
'discord_webhook_url' => $this->discord_webhook_url,
'notification_settings' => $this->notification_settings,
'removed_properties' => $this->removed_properties,
'last_edited_human' => $this->updated_at?->diffForHumans(),
'seo_meta' => $this->seo_meta

View File

@@ -41,6 +41,7 @@ class Form extends Model
'notifications_include_submission',
'slack_webhook_url',
'discord_webhook_url',
'notification_settings',
// integrations
'webhook_url',
@@ -83,10 +84,7 @@ class Form extends Model
// Security & Privacy
'can_be_indexed',
'password',
// Custom SEO
'seo_meta'
'password'
];
protected $casts = [
@@ -95,7 +93,8 @@ class Form extends Model
'closes_at' => 'datetime',
'tags' => 'array',
'removed_properties' => 'array',
'seo_meta' => 'object'
'seo_meta' => 'object',
'notification_settings' => 'object'
];
protected $appends = [

View File

@@ -3,7 +3,8 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Support\Arr;
class DiscordHandler extends AbstractWebhookHandler
{
@@ -20,58 +21,60 @@ class DiscordHandler extends AbstractWebhookHandler
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";
$settings = (array) Arr::get((array)$this->form->notification_settings, 'discord', []);
$externalLinks = [];
if(Arr::get($settings, 'link_open_form', true)){
$externalLinks[] = '[**🔗 Open Form**](' . $this->form->share_url . ')';
}
if(Arr::get($settings, 'link_edit_form', true)){
$editFormURL = url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '[**✍️ Edit Form**](' . $editFormURL . ')';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->data['submission_id']);
$externalLinks[] = '[**✍️ ' . $this->form->editable_submissions_button_text . '**](' . $this->form->share_url . '?submission_id=' . $submissionId . ')';
}
$form_name = $this->form->title;
$formURL = url("forms/" . $this->form->slug . "/show/submissions");
$color = hexdec(str_replace('#', '', $this->form->color));
$blocks = [];
if(Arr::get($settings, 'include_submission_data', true)){
$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";
}
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => $submissionString
];
}
if(Arr::get($settings, 'views_submissions_count', true)){
$countString = '**👀 Views**: ' . (string)$this->form->views_count . " \n";
$countString .= '**🖊️ Submissions**: ' . (string)$this->form->submissions_count;
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => $countString
];
}
if(count($externalLinks) > 0){
$blocks[] = [
"type" => "rich",
"color" => $color,
"description" => implode(' - ', $externalLinks)
];
}
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
]
]
]
]
'content' => 'New submission for your form **' . $this->form->title . '**',
'tts' => false,
'username' => config('app.name'),
'avatar_url' => asset('img/logo.png'),
'embeds' => $blocks
];
}

View File

@@ -3,8 +3,8 @@
namespace App\Service\Forms\Webhooks;
use App\Service\Forms\FormSubmissionFormatter;
use Illuminate\Support\Str;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Support\Arr;
class SlackHandler extends AbstractWebhookHandler
{
@@ -21,48 +21,70 @@ class SlackHandler extends AbstractWebhookHandler
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";
$settings = (array) Arr::get((array)$this->form->notification_settings, 'slack', []);
$externalLinks = [];
if(Arr::get($settings, 'link_open_form', true)){
$externalLinks[] = '*<' . $this->form->share_url . '|🔗 Open Form>*';
}
$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) {
if(Arr::get($settings, 'link_edit_form', true)){
$editFormURL = url('forms/' . $this->form->slug . '/show');
$externalLinks[] = '*<' . $editFormURL . '|✍️ Edit Form>*';
}
if (Arr::get($settings, 'link_edit_submission', true) && $this->form->editable_submissions) {
$submissionId = Hashids::encode($this->data['submission_id']);
$externalLinks[] = '*<' . $this->form->share_url . '?submission_id=' . $submissionId . '|✍️ ' . $this->form->editable_submissions_button_text . '>*';
}
$blocks = [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *' . $this->form->title . '*',
]
]
];
if(Arr::get($settings, 'include_submission_data', true)){
$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";
}
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString,
]
];
}
if(Arr::get($settings, 'views_submissions_count', true)){
$countString = '*👀 Views*: ' . (string)$this->form->views_count . " \n";
$countString .= '*🖊️ Submissions*: ' . (string)$this->form->submissions_count;
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $countString,
]
];
}
if(count($externalLinks) > 0){
$blocks[] = [
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => implode(' ', $externalLinks),
]
];
}
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),
],
],
],
'blocks' => $blocks
];
}