2024-03-28 18:14:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
namespace App\Integrations\Handlers;
|
2024-03-28 18:14:30 +01:00
|
|
|
|
|
|
|
|
use App\Models\Integration\FormIntegration;
|
|
|
|
|
use App\Events\Forms\FormSubmitted;
|
2024-08-12 11:14:02 +02:00
|
|
|
use App\Models\Forms\Form;
|
2024-03-28 18:14:30 +01:00
|
|
|
use App\Models\Integration\FormIntegrationsEvent;
|
|
|
|
|
use App\Service\Forms\FormSubmissionFormatter;
|
|
|
|
|
use App\Service\Forms\FormLogicConditionChecker;
|
|
|
|
|
use Illuminate\Http\Client\RequestException;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2025-01-14 14:04:34 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-03-28 18:14:30 +01:00
|
|
|
use Vinkla\Hashids\Facades\Hashids;
|
|
|
|
|
|
|
|
|
|
abstract class AbstractIntegrationHandler
|
|
|
|
|
{
|
|
|
|
|
protected $form = null;
|
|
|
|
|
protected $submissionData = null;
|
|
|
|
|
protected $integrationData = null;
|
Add Telegram Notification Integration (#732)
* Add Telegram Notification Integration
- Introduce Telegram Notification integration to the forms configuration.
- Add necessary properties including name, icon, section name, file name, actions file name, and pro status.
This update enhances the notification options available in the application, allowing users to integrate Telegram for notifications.
* Add Telegram integration support
- Updated .env.example to include Telegram bot configuration variables.
- Added the Telegram driver to OAuthProviderService for handling Telegram authentication.
- Created OAuthTelegramDriver class to manage Telegram OAuth interactions.
- Registered Telegram service in services.php with necessary credentials.
- Updated ProviderModal and TelegramIntegration components to support Telegram account connection and widget integration.
- Refactored integration forms to utilize FlatSelectInput for selecting Telegram accounts.
These changes enable users to connect their Telegram accounts for notifications, enhancing the integration capabilities of the application.
* Enhance Telegram integration and refactor settings components
- Added Telegram configuration to FeatureFlagsController for bot and redirect settings.
- Updated nuxt.config.ts to include settings components path for better organization.
- Refactored TelegramIntegration component to navigate to settings on connect.
- Renamed and updated ProviderModal and ProviderWidgetModal for consistency.
- Introduced TelegramWidget component for dynamic loading of the Telegram login widget.
- Refactored access tokens and connections pages to use updated component names.
These changes improve the Telegram integration and streamline the settings management interface.
* Refactor Telegram integration for widget-based authentication
- Updated .env.example to rename Telegram bot configuration variable from TELEGRAM_BOT_NAME to TELEGRAM_BOT_ID.
- Removed the Telegram provider from composer.json to streamline dependencies.
- Enhanced FeatureFlagsController to support new bot_id configuration.
- Implemented handleWidgetRedirect method in OAuthProviderController for handling widget authentication.
- Created WidgetOAuthDriver interface and refactored OAuthTelegramDriver to implement widget-based authentication methods.
- Updated services.php to reflect changes in Telegram configuration.
- Added widget callback route for handling authentication responses.
- Refactored TelegramWidget component to utilize the new widget authentication flow.
These changes improve the Telegram integration by enabling widget-based authentication, enhancing user experience and security.
* Enhance Telegram integration by adding provider support and refactoring data retrieval
- Introduced a new protected property `provider` in `AbstractIntegrationHandler` to store provider information.
- Updated the constructor to assign the provider from `formIntegration`.
- Refactored `getChatId` method in `TelegramIntegration` to utilize `provider_user_id` instead of the removed bot token.
- Adjusted the `shouldRun` method to include a check for `oauth_id` in `formIntegration`.
These changes improve the handling of provider data within the Telegram integration, enhancing its functionality and reliability.
* Enhance Telegram integration by adding MarkdownV2 escaping functionality
- Updated the message text formatting in the TelegramIntegration class to escape special characters for MarkdownV2.
- Introduced a new protected method `escapeMarkdownV2` to handle the escaping of special characters, improving message rendering in Telegram.
These changes enhance the Telegram integration by ensuring that messages are properly formatted for MarkdownV2, preventing potential rendering issues.
* fix pint
* Fix ESLint warning for unused variable in TelegramWidget component
* Remove unused variable declaration in TelegramWidget component to address ESLint warning. This change enhances code quality by adhering to linting rules and improving maintainability.
* Refactor Telegram integration components by removing unused code and improving maintainability
- Removed the 'refresh_token' field from the OAuthTelegramDriver as it is no longer needed.
- Eliminated unused variable declarations and interval logic in the TelegramIntegrationActions component to address ESLint warnings and enhance code quality.
These changes streamline the codebase and adhere to best practices for maintainability.
* Enhance Telegram integration by adding validation messages and improving message formatting
- Introduced custom validation messages for the `oauth_id` field in `FormIntegrationsRequest`, enhancing user feedback during integration setup.
- Refactored message construction in `TelegramIntegration` to utilize an array for building the message text, improving readability and maintainability.
- Updated the `handle` method to include error logging for missing `chat_id` and `bot token`, enhancing error handling and debugging capabilities.
These changes improve the user experience and reliability of the Telegram integration by providing clearer validation feedback and more robust error handling.
* Update environment variables documentation for clarity and consistency
- Reformatted the environment variables table for improved readability by aligning headers and descriptions.
- Added new environment variables for Telegram bot integration, including `TELEGRAM_BOT_ID` and `TELEGRAM_BOT_TOKEN`, to support enhanced notification features.
- Adjusted the warning message regarding Docker commands to improve clarity on environment variable reloading.
These changes enhance the documentation by ensuring that it is clear, consistent, and up-to-date with the latest configuration requirements.
---------
Co-authored-by: JhumanJ <julien@nahum.net>
2025-04-08 12:42:47 +02:00
|
|
|
protected $provider = null;
|
2024-03-28 18:14:30 +01:00
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
protected FormSubmitted $event,
|
|
|
|
|
protected FormIntegration $formIntegration,
|
|
|
|
|
protected array $integration
|
|
|
|
|
) {
|
|
|
|
|
$this->form = $event->form;
|
|
|
|
|
$this->submissionData = $event->data;
|
|
|
|
|
$this->integrationData = $formIntegration->data;
|
Add Telegram Notification Integration (#732)
* Add Telegram Notification Integration
- Introduce Telegram Notification integration to the forms configuration.
- Add necessary properties including name, icon, section name, file name, actions file name, and pro status.
This update enhances the notification options available in the application, allowing users to integrate Telegram for notifications.
* Add Telegram integration support
- Updated .env.example to include Telegram bot configuration variables.
- Added the Telegram driver to OAuthProviderService for handling Telegram authentication.
- Created OAuthTelegramDriver class to manage Telegram OAuth interactions.
- Registered Telegram service in services.php with necessary credentials.
- Updated ProviderModal and TelegramIntegration components to support Telegram account connection and widget integration.
- Refactored integration forms to utilize FlatSelectInput for selecting Telegram accounts.
These changes enable users to connect their Telegram accounts for notifications, enhancing the integration capabilities of the application.
* Enhance Telegram integration and refactor settings components
- Added Telegram configuration to FeatureFlagsController for bot and redirect settings.
- Updated nuxt.config.ts to include settings components path for better organization.
- Refactored TelegramIntegration component to navigate to settings on connect.
- Renamed and updated ProviderModal and ProviderWidgetModal for consistency.
- Introduced TelegramWidget component for dynamic loading of the Telegram login widget.
- Refactored access tokens and connections pages to use updated component names.
These changes improve the Telegram integration and streamline the settings management interface.
* Refactor Telegram integration for widget-based authentication
- Updated .env.example to rename Telegram bot configuration variable from TELEGRAM_BOT_NAME to TELEGRAM_BOT_ID.
- Removed the Telegram provider from composer.json to streamline dependencies.
- Enhanced FeatureFlagsController to support new bot_id configuration.
- Implemented handleWidgetRedirect method in OAuthProviderController for handling widget authentication.
- Created WidgetOAuthDriver interface and refactored OAuthTelegramDriver to implement widget-based authentication methods.
- Updated services.php to reflect changes in Telegram configuration.
- Added widget callback route for handling authentication responses.
- Refactored TelegramWidget component to utilize the new widget authentication flow.
These changes improve the Telegram integration by enabling widget-based authentication, enhancing user experience and security.
* Enhance Telegram integration by adding provider support and refactoring data retrieval
- Introduced a new protected property `provider` in `AbstractIntegrationHandler` to store provider information.
- Updated the constructor to assign the provider from `formIntegration`.
- Refactored `getChatId` method in `TelegramIntegration` to utilize `provider_user_id` instead of the removed bot token.
- Adjusted the `shouldRun` method to include a check for `oauth_id` in `formIntegration`.
These changes improve the handling of provider data within the Telegram integration, enhancing its functionality and reliability.
* Enhance Telegram integration by adding MarkdownV2 escaping functionality
- Updated the message text formatting in the TelegramIntegration class to escape special characters for MarkdownV2.
- Introduced a new protected method `escapeMarkdownV2` to handle the escaping of special characters, improving message rendering in Telegram.
These changes enhance the Telegram integration by ensuring that messages are properly formatted for MarkdownV2, preventing potential rendering issues.
* fix pint
* Fix ESLint warning for unused variable in TelegramWidget component
* Remove unused variable declaration in TelegramWidget component to address ESLint warning. This change enhances code quality by adhering to linting rules and improving maintainability.
* Refactor Telegram integration components by removing unused code and improving maintainability
- Removed the 'refresh_token' field from the OAuthTelegramDriver as it is no longer needed.
- Eliminated unused variable declarations and interval logic in the TelegramIntegrationActions component to address ESLint warnings and enhance code quality.
These changes streamline the codebase and adhere to best practices for maintainability.
* Enhance Telegram integration by adding validation messages and improving message formatting
- Introduced custom validation messages for the `oauth_id` field in `FormIntegrationsRequest`, enhancing user feedback during integration setup.
- Refactored message construction in `TelegramIntegration` to utilize an array for building the message text, improving readability and maintainability.
- Updated the `handle` method to include error logging for missing `chat_id` and `bot token`, enhancing error handling and debugging capabilities.
These changes improve the user experience and reliability of the Telegram integration by providing clearer validation feedback and more robust error handling.
* Update environment variables documentation for clarity and consistency
- Reformatted the environment variables table for improved readability by aligning headers and descriptions.
- Added new environment variables for Telegram bot integration, including `TELEGRAM_BOT_ID` and `TELEGRAM_BOT_TOKEN`, to support enhanced notification features.
- Adjusted the warning message regarding Docker commands to improve clarity on environment variable reloading.
These changes enhance the documentation by ensuring that it is clear, consistent, and up-to-date with the latest configuration requirements.
---------
Co-authored-by: JhumanJ <julien@nahum.net>
2025-04-08 12:42:47 +02:00
|
|
|
$this->provider = $formIntegration->provider;
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getProviderName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->integration['name'] ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function logicConditionsMet(): bool
|
|
|
|
|
{
|
2024-08-12 11:14:02 +02:00
|
|
|
if (!$this->formIntegration->logic || empty((array) $this->formIntegration->logic)) {
|
2024-03-28 18:14:30 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return FormLogicConditionChecker::conditionsMet(
|
|
|
|
|
json_decode(json_encode($this->formIntegration->logic), true),
|
|
|
|
|
$this->submissionData
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function shouldRun(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->logicConditionsMet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getWebhookUrl(): ?string
|
|
|
|
|
{
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default webhook payload. Can be changed in child classes.
|
|
|
|
|
*/
|
|
|
|
|
protected function getWebhookData(): array
|
|
|
|
|
{
|
2024-08-12 11:14:02 +02:00
|
|
|
return self::formatWebhookData($this->form, $this->submissionData);
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final public function run(): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->handle();
|
|
|
|
|
$this->formIntegration->events()->create([
|
|
|
|
|
'status' => FormIntegrationsEvent::STATUS_SUCCESS,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->formIntegration->events()->create([
|
|
|
|
|
'status' => FormIntegrationsEvent::STATUS_ERROR,
|
|
|
|
|
'data' => $this->extractEventDataFromException($e),
|
|
|
|
|
]);
|
2025-01-14 14:04:34 +01:00
|
|
|
Log::error('Integration failed', array_merge([
|
|
|
|
|
'form_id' => $this->form->id,
|
|
|
|
|
'integration_id' => $this->formIntegration->id,
|
|
|
|
|
], $this->extractEventDataFromException($e)));
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
public function created(): void
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:14:30 +01:00
|
|
|
/**
|
|
|
|
|
* Default handle. Can be changed in child classes.
|
|
|
|
|
*/
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
|
|
|
|
if (!$this->shouldRun()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Http::throw()->post($this->getWebhookUrl(), $this->getWebhookData());
|
|
|
|
|
}
|
|
|
|
|
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
abstract public static function getValidationRules(?Form $form): array;
|
2024-03-28 18:14:30 +01:00
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
public static function isOAuthRequired(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getValidationAttributes(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 11:14:02 +02:00
|
|
|
public static function formatWebhookData(Form $form, array $submissionData): array
|
2024-03-28 18:14:30 +01:00
|
|
|
{
|
2024-08-12 11:14:02 +02:00
|
|
|
$formatter = (new FormSubmissionFormatter($form, $submissionData))
|
|
|
|
|
->useSignedUrlForFiles()
|
|
|
|
|
->showHiddenFields();
|
|
|
|
|
|
|
|
|
|
// Old format - kept for retro-compatibility
|
|
|
|
|
$oldFormatData = [];
|
|
|
|
|
$formattedData = [];
|
2024-10-01 12:42:03 +02:00
|
|
|
$fieldsWithValue = $formatter->getFieldsWithValue();
|
|
|
|
|
|
|
|
|
|
foreach ($fieldsWithValue as $field) {
|
|
|
|
|
$oldFormatData[$field['name']] = $field['value'];
|
|
|
|
|
// New format using ID
|
2024-08-12 11:14:02 +02:00
|
|
|
$formattedData[$field['id']] = [
|
|
|
|
|
'value' => $field['value'],
|
|
|
|
|
'name' => $field['name'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'form_title' => $form->title,
|
|
|
|
|
'form_slug' => $form->slug,
|
|
|
|
|
'submission' => $oldFormatData,
|
|
|
|
|
'data' => $formattedData,
|
|
|
|
|
'message' => 'Please do not use the `submission` field. It is deprecated and will be removed in the future.'
|
|
|
|
|
];
|
|
|
|
|
if ($form->is_pro && $form->editable_submissions) {
|
|
|
|
|
$data['edit_link'] = $form->share_url . '?submission_id=' . Hashids::encode(
|
|
|
|
|
$submissionData['submission_id']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:14:30 +01:00
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function extractEventDataFromException(\Exception $e): array
|
|
|
|
|
{
|
|
|
|
|
if ($e instanceof RequestException) {
|
|
|
|
|
return [
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'response' => $e->response->json(),
|
|
|
|
|
'status' => $e->response->status(),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return [
|
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
|
];
|
|
|
|
|
}
|
2024-08-12 11:14:02 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Used in FormIntegrationRequest to format integration
|
|
|
|
|
*/
|
|
|
|
|
public static function formatData(array $data): array
|
|
|
|
|
{
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|