← All changes
|
app/Http/Controllers/ChatMessageParserController.php
+59
-84
1.5.5
→
2.4.0
View file →
| @@ -1,13 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Http\Controllers; |
| 4 | 4 | |
| 5 | -use FluentSupport\App\Models\Agent; | |
| 6 | -use FluentSupport\App\Models\Ticket; | |
| 7 | -use FluentSupport\App\Services\Tickets\ResponseService; | |
| 8 | -use FluentSupport\Framework\Request\Request; | |
| 9 | -use FluentSupportPro\App\Services\Integrations\Slack\SlackNotification; | |
| 5 | +use FluentSupport\Framework\Http\Request\Request; | |
| 6 | +use FluentSupport\App\Services\Helper; | |
| 7 | +use FluentSupport\App\Services\ThirdParty\HandleSlackEvent; | |
| 8 | +use FluentSupport\App\Services\ThirdParty\HandleTelegramEvent; | |
| 10 | 9 | |
| 11 | 10 | /** |
| 12 | 11 | * ChatMessageParserController class is responsible for getting information from integrated 3rd party request and response |
| 13 | 12 | * @package FluentSupport\App\Http\Controllers |
| @@ -19,115 +18,91 @@ | ||
| 19 | 18 | { |
| 20 | 19 | /** |
| 21 | 20 | * handleTelegramWebhook will get the content from telegram, check the data validity and create response in a ticket |
| 22 | 21 | * @param Request $request |
| 23 | - * @param $token | |
| 22 | + * @param HandleTelegramEvent $handler | |
| 23 | + * @param string $token | |
| 24 | + * @throws Exception | |
| 24 | 25 | * @return mixed |
| 25 | 26 | */ |
| 26 | - public function handleTelegramWebhook(Request $request, $token) | |
| 27 | + public function handleTelegramWebhook(Request $request, HandleTelegramEvent $handler, $token) | |
| 27 | 28 | { |
| 28 | - if (!defined('FLUENTSUPPORTPRO')) { | |
| 29 | - return $this->sendSuccess([ | |
| 30 | - 'message' => __('Telegram Integration requires pro version of Fluent Support', 'fluent-support'), | |
| 29 | + if (!$this->verifyWebhookSignature('telegram', $request)) { | |
| 30 | + return $this->sendError([ | |
| 31 | + 'message' => __('Invalid request signature.', 'fluent-support'), | |
| 31 | 32 | 'status' => false |
| 32 | - ]); | |
| 33 | + ], 403); | |
| 33 | 34 | } |
| 34 | 35 | |
| 35 | - // Validate Token | |
| 36 | - if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) { | |
| 36 | + try { | |
| 37 | 37 | return $this->sendSuccess([ |
| 38 | - 'message' => __('Bot Token could not be verified', 'fluent-support'), | |
| 38 | + 'message' => __('Response has been successfully recorded', 'fluent-support'), | |
| 39 | + 'status' => true, | |
| 40 | + 'data' => $handler->handleEvent($request->all(), $token) | |
| 41 | + ]); | |
| 42 | + } catch (\Exception $e) { | |
| 43 | + return $this->sendError([ | |
| 44 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 39 | 45 | 'status' => false |
| 40 | 46 | ]); |
| 41 | 47 | } |
| 48 | + } | |
| 42 | 49 | |
| 43 | - $payload = $request->all(); | |
| 44 | - $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload); | |
| 45 | - | |
| 46 | - if (is_wp_error($responseData)) { | |
| 47 | - /* | |
| 48 | - * Action on telegram payload error when replying ticket from telegram | |
| 49 | - * | |
| 50 | - * @since v1.0.0 | |
| 51 | - * @param object $responseData | |
| 52 | - * @param object $payload | |
| 53 | - */ | |
| 54 | - do_action('fluent_support/telegram_payload_error', $responseData, $payload); | |
| 55 | - return $this->sendSuccess([ | |
| 56 | - 'message' => $responseData->get_error_message(), | |
| 57 | - 'type' => $responseData->get_error_code(), | |
| 50 | + /** | |
| 51 | + * handleSlackEvent responsible for getting information from integrated slack request and response | |
| 52 | + * @param Request $request | |
| 53 | + * @param HandleSlackEvent $handler | |
| 54 | + * @param $token | |
| 55 | + * @return array | |
| 56 | + */ | |
| 57 | + public function handleSlackEvent(Request $request, HandleSlackEvent $handler, $token) | |
| 58 | + { | |
| 59 | + if (!$this->verifyWebhookSignature('slack', $request)) { | |
| 60 | + return $this->sendError([ | |
| 61 | + 'message' => __('Invalid request signature.', 'fluent-support'), | |
| 58 | 62 | 'status' => false |
| 59 | - ]); | |
| 63 | + ], 403); | |
| 60 | 64 | } |
| 61 | 65 | |
| 62 | - $data = [ | |
| 63 | - 'conversation_type' => 'response', | |
| 64 | - 'content' => $responseData['response_text'], | |
| 65 | - 'source' => 'telegram' | |
| 66 | - ]; | |
| 67 | - | |
| 68 | - $ticket = Ticket::find($responseData['ticket_id']); | |
| 69 | - | |
| 70 | - if (!$ticket) { | |
| 71 | - return $this->sendSuccess([ | |
| 72 | - 'message' => __('No ticket found', 'fluent-support'), | |
| 73 | - 'status' => false | |
| 66 | + if ($request->getSafe('type', 'sanitize_text_field') === 'url_verification') { | |
| 67 | + return new \WP_REST_Response($request->getSafe('challenge', 'sanitize_text_field'), 200, [ | |
| 68 | + 'Content-Type' => 'text/plain; charset=utf-8' | |
| 74 | 69 | ]); |
| 75 | 70 | } |
| 76 | 71 | |
| 77 | - $agent = Agent::find($responseData['agent_id']); | |
| 78 | - | |
| 79 | - if (!$agent) { | |
| 72 | + try { | |
| 80 | 73 | return $this->sendSuccess([ |
| 81 | - 'message' => __('No Agent found', 'fluent-support'), | |
| 74 | + 'message' => 'received', | |
| 75 | + 'result' => $handler->handleEvent($token) | |
| 76 | + ]); | |
| 77 | + } catch (\Exception $e) { | |
| 78 | + return $this->sendError([ | |
| 79 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 82 | 80 | 'status' => false |
| 83 | 81 | ]); |
| 84 | 82 | } |
| 85 | - | |
| 86 | - (new ResponseService)->createResponse($data, $agent, $ticket); | |
| 87 | - | |
| 88 | - return $this->sendSuccess([ | |
| 89 | - 'message' => __('Response has been successfully recorded', 'fluent-support'), | |
| 90 | - 'status' => true, | |
| 91 | - 'data' => $data | |
| 92 | - ]); | |
| 93 | - | |
| 94 | 83 | } |
| 95 | 84 | |
| 96 | 85 | /** |
| 97 | - * handleSlackEvent will get content from slack,check the data validity and create response in a ticket | |
| 86 | + * Verify webhook signature using platform-specific logic. | |
| 87 | + * | |
| 88 | + * Pro plugin or third-party code may hook into the filter | |
| 89 | + * 'fluent_support/verify_webhook_signature_{platform}' to enable | |
| 90 | + * cryptographic signature verification. When no verifier is | |
| 91 | + * registered, fall back to the existing token-based validation | |
| 92 | + * handled by the downstream webhook handlers. | |
| 93 | + * | |
| 94 | + * @param string $platform 'telegram' or 'slack' | |
| 98 | 95 | * @param Request $request |
| 99 | - * @param $token | |
| 100 | - * @return array|\ArrayAccess|mixed | |
| 96 | + * @return bool | |
| 101 | 97 | */ |
| 102 | - public function handleSlackEvent(Request $request, $token) | |
| 98 | + private function verifyWebhookSignature($platform, Request $request) | |
| 103 | 99 | { |
| 104 | - if (!defined('FLUENTSUPPORTPRO')) { | |
| 105 | - return $this->sendSuccess([ | |
| 106 | - 'message' => __('Slack Integration requires pro version of Fluent Support', 'fluent-support'), | |
| 107 | - 'status' => false | |
| 108 | - ]); | |
| 109 | - } | |
| 100 | + $hookName = 'fluent_support/verify_webhook_signature_' . $platform; | |
| 110 | 101 | |
| 111 | - // Validate Token | |
| 112 | - if (\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken() != $token) { | |
| 113 | - return $this->sendSuccess([ | |
| 114 | - 'message' => __('Bot Token could not be verified', 'fluent-support'), | |
| 115 | - 'status' => false | |
| 116 | - ]); | |
| 102 | + if (!has_filter($hookName)) { | |
| 103 | + return true; | |
| 117 | 104 | } |
| 118 | 105 | |
| 119 | - if ($request->get('type') == 'url_verification') { | |
| 120 | - return $request->get('challenge'); | |
| 121 | - } | |
| 122 | - | |
| 123 | - $event = $request->get('event'); | |
| 124 | - | |
| 125 | - $result = (new SlackNotification())->processSlackEvent($event); | |
| 126 | - | |
| 127 | - return $this->sendSuccess([ | |
| 128 | - 'message' => 'received', | |
| 129 | - 'result' => $result | |
| 130 | - ]); | |
| 106 | + return (bool) apply_filters($hookName, false, $request); | |
| 131 | 107 | } |
| 132 | - | |
| 133 | 108 | } |