* [Feature] Added Discord Webhook feature to forms * Remove commented out svg --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
committed by
GitHub
parent
eb3a11c992
commit
b101a5daba
@@ -42,6 +42,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
|
||||
'webhook_url' => 'url|nullable',
|
||||
'use_captcha' => 'boolean',
|
||||
'slack_webhook_url' => 'url|nullable',
|
||||
'discord_webhook_url' => 'url|nullable',
|
||||
|
||||
// Customization
|
||||
'theme' => ['required',Rule::in(Form::THEMES)],
|
||||
|
||||
@@ -30,6 +30,7 @@ class FormResource extends JsonResource
|
||||
'submissions_count' => $this->when($this->workspaceIsPro(), $this->submissions_count),
|
||||
'notifies' => $this->notifies,
|
||||
'notifies_slack' => $this->notifies_slack,
|
||||
'notifies_discord' => $this->notifies_discord,
|
||||
'send_submission_confirmation' => $this->send_submission_confirmation,
|
||||
'webhook_url' => $this->webhook_url,
|
||||
'redirect_url' => $this->redirect_url,
|
||||
@@ -45,6 +46,7 @@ class FormResource extends JsonResource
|
||||
'visibility' => $this->visibility,
|
||||
'notification_emails' => $this->notification_emails,
|
||||
'slack_webhook_url' => $this->slack_webhook_url,
|
||||
'discord_webhook_url' => $this->discord_webhook_url,
|
||||
'removed_properties' => $this->removed_properties,
|
||||
'last_edited_human' => $this->updated_at?->diffForHumans()
|
||||
] : [];
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
namespace App\Listeners\Forms;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Events\Forms\FormSubmitted;
|
||||
use App\Notifications\Forms\FormSubmissionNotification;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Spatie\WebhookServer\WebhookCall;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use App\Notifications\Forms\FormSubmissionNotification;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class NotifyFormSubmission implements ShouldQueue
|
||||
@@ -44,8 +46,16 @@ class NotifyFormSubmission implements ShouldQueue
|
||||
// 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)){
|
||||
@@ -105,4 +115,75 @@ class NotifyFormSubmission implements ShouldQueue
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ class Form extends Model
|
||||
'notification_body',
|
||||
'notifications_include_submission',
|
||||
'slack_webhook_url',
|
||||
'discord_webhook_url',
|
||||
|
||||
// integrations
|
||||
'webhook_url',
|
||||
@@ -98,6 +99,7 @@ class Form extends Model
|
||||
'workspace_id',
|
||||
'notifies',
|
||||
'slack_webhook_url',
|
||||
'discord_webhook_url',
|
||||
'webhook_url',
|
||||
'send_submission_confirmation',
|
||||
'redirect_url',
|
||||
@@ -250,6 +252,12 @@ class Form extends Model
|
||||
return FormFactory::new();
|
||||
}
|
||||
|
||||
|
||||
public function getNotifiesDiscordAttribute()
|
||||
{
|
||||
return !empty($this->discord_webhook_url);
|
||||
}
|
||||
|
||||
public function getNotifiesSlackAttribute()
|
||||
{
|
||||
return !empty($this->slack_webhook_url);
|
||||
|
||||
@@ -39,6 +39,7 @@ class FormCleaner
|
||||
'use_captcha' => false,
|
||||
'password' => null,
|
||||
'slack_webhook_url' => null,
|
||||
'discord_webhook_url' => null,
|
||||
];
|
||||
|
||||
private array $fieldDefaults = [
|
||||
@@ -70,6 +71,7 @@ class FormCleaner
|
||||
'database_fields_update' => 'Form submission will only create new records (no updates).',
|
||||
'theme' => 'Default theme was applied.',
|
||||
'slack_webhook_url' => "Slack webhook disabled.",
|
||||
'discord_webhook_url' => "Discord webhook disabled.",
|
||||
|
||||
// For fields
|
||||
'hide_field_name' => 'Hide field name removed.',
|
||||
|
||||
Reference in New Issue
Block a user