← All changes
|
app/Http/Controllers/ChatMessageParserController.php
+80
-78
1.4.0
→
2.4.0
View file →
| @@ -1,106 +1,108 @@ | ||
| 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\Meta; | |
| 7 | -use FluentSupport\App\Models\Ticket; | |
| 8 | -use FluentSupportPro\App\Services\Integrations\Slack\SlackHelper; | |
| 9 | -use FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper; | |
| 10 | -use FluentSupport\App\Services\Tickets\ResponseService; | |
| 11 | -use FluentSupport\Framework\Request\Request; | |
| 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; | |
| 12 | 9 | |
| 10 | +/** | |
| 11 | + * ChatMessageParserController class is responsible for getting information from integrated 3rd party request and response | |
| 12 | + * @package FluentSupport\App\Http\Controllers | |
| 13 | + * | |
| 14 | + * @version 1.0.0 | |
| 15 | + */ | |
| 16 | + | |
| 13 | 17 | class ChatMessageParserController extends Controller |
| 14 | 18 | { |
| 15 | - public function handleTelegramWebhook(Request $request, $token) | |
| 19 | + /** | |
| 20 | + * handleTelegramWebhook will get the content from telegram, check the data validity and create response in a ticket | |
| 21 | + * @param Request $request | |
| 22 | + * @param HandleTelegramEvent $handler | |
| 23 | + * @param string $token | |
| 24 | + * @throws Exception | |
| 25 | + * @return mixed | |
| 26 | + */ | |
| 27 | + public function handleTelegramWebhook(Request $request, HandleTelegramEvent $handler, $token) | |
| 16 | 28 | { |
| 17 | - // Validate Token | |
| 18 | - if(TelegramHelper::getWebhookToken() != $token) { | |
| 19 | - return $this->sendSuccess([ | |
| 20 | - 'message' => __('Bot Token could not be verified', 'fluent-support'), | |
| 21 | - 'status' => false | |
| 22 | - ]); | |
| 29 | + if (!$this->verifyWebhookSignature('telegram', $request)) { | |
| 30 | + return $this->sendError([ | |
| 31 | + 'message' => __('Invalid request signature.', 'fluent-support'), | |
| 32 | + 'status' => false | |
| 33 | + ], 403); | |
| 23 | 34 | } |
| 24 | 35 | |
| 25 | - $payload = $request->all(); | |
| 26 | - $responseData = TelegramHelper::parseTelegramBotPayload($payload); | |
| 27 | - | |
| 28 | - if(is_wp_error($responseData)) { | |
| 29 | - do_action('fluent_support/telegram_payload_error', $responseData, $payload); | |
| 36 | + try { | |
| 30 | 37 | return $this->sendSuccess([ |
| 31 | - 'message' => $responseData->get_error_message(), | |
| 32 | - 'type' => $responseData->get_error_code(), | |
| 33 | - 'status' => false | |
| 38 | + 'message' => __('Response has been successfully recorded', 'fluent-support'), | |
| 39 | + 'status' => true, | |
| 40 | + 'data' => $handler->handleEvent($request->all(), $token) | |
| 34 | 41 | ]); |
| 42 | + } catch (\Exception $e) { | |
| 43 | + return $this->sendError([ | |
| 44 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 45 | + 'status' => false | |
| 46 | + ]); | |
| 35 | 47 | } |
| 48 | + } | |
| 36 | 49 | |
| 37 | - $data = [ | |
| 38 | - 'conversation_type' => 'response', | |
| 39 | - 'content' => $responseData['response_text'], | |
| 40 | - 'source' => 'telegram' | |
| 41 | - ]; | |
| 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'), | |
| 62 | + 'status' => false | |
| 63 | + ], 403); | |
| 64 | + } | |
| 42 | 65 | |
| 43 | - $ticket = Ticket::find($responseData['ticket_id']); | |
| 44 | - | |
| 45 | - if(!$ticket) { | |
| 46 | - return $this->sendSuccess([ | |
| 47 | - 'message' => __('No ticket found', 'fluent-support'), | |
| 48 | - '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' | |
| 49 | 69 | ]); |
| 50 | 70 | } |
| 51 | 71 | |
| 52 | - $agent = Agent::find($responseData['agent_id']); | |
| 53 | - | |
| 54 | - if(!$agent) { | |
| 72 | + try { | |
| 55 | 73 | return $this->sendSuccess([ |
| 56 | - 'message' => __('No Agent found', 'fluent-support'), | |
| 57 | - 'status' => false | |
| 74 | + 'message' => 'received', | |
| 75 | + 'result' => $handler->handleEvent($token) | |
| 58 | 76 | ]); |
| 77 | + } catch (\Exception $e) { | |
| 78 | + return $this->sendError([ | |
| 79 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 80 | + 'status' => false | |
| 81 | + ]); | |
| 59 | 82 | } |
| 60 | - | |
| 61 | - (new ResponseService)->createResponse($data, $agent, $ticket); | |
| 62 | - | |
| 63 | - return $this->sendSuccess([ | |
| 64 | - 'message' => __('Response has been successfully recorded', 'fluent-support'), | |
| 65 | - 'status' => true, | |
| 66 | - 'data' => $data | |
| 67 | - ]); | |
| 68 | - | |
| 69 | 83 | } |
| 70 | 84 | |
| 71 | - public function handleSlackEvent(Request $request) | |
| 85 | + /** | |
| 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' | |
| 95 | + * @param Request $request | |
| 96 | + * @return bool | |
| 97 | + */ | |
| 98 | + private function verifyWebhookSignature($platform, Request $request) | |
| 72 | 99 | { |
| 73 | - $slackSettings = SlackHelper::getSettings(); | |
| 100 | + $hookName = 'fluent_support/verify_webhook_signature_' . $platform; | |
| 74 | 101 | |
| 75 | - if ($slackSettings['reply_from_slack'] == 'yes'){ | |
| 102 | + if (!has_filter($hookName)) { | |
| 103 | + return true; | |
| 104 | + } | |
| 76 | 105 | |
| 77 | - $eventSubType = $request->get('event.subtype'); | |
| 78 | - $channelId = $request->get('event.channel'); | |
| 79 | - | |
| 80 | - if($request->get('type') == 'url_verification') { | |
| 81 | - return $request->get('challenge'); | |
| 82 | - } | |
| 83 | - | |
| 84 | - if($slackSettings['channel_id'] == $channelId && $eventSubType== 'message_changed') { | |
| 85 | - $user = $request->get('event.message.user'); | |
| 86 | - $message = $request->get('event.message'); | |
| 87 | - $ticketId = explode('#', $message['root']['attachments'][0]['title']); | |
| 88 | - $ticket = Ticket::find(intval($ticketId[1])); | |
| 89 | - | |
| 90 | - $userSlackObject = Meta::where('key','slack_user_id')->where('value', $user)->first(); | |
| 91 | - $agent = Agent::where('id', $userSlackObject->object_id)->first(); | |
| 92 | - | |
| 93 | - $data = [ | |
| 94 | - 'person_id' => $agent->user_id, | |
| 95 | - 'ticket_id' => $ticket->id, | |
| 96 | - 'conversation_type' => 'response', | |
| 97 | - 'content' => $message['text'], | |
| 98 | - 'source' => 'slack' | |
| 99 | - ]; | |
| 100 | - | |
| 101 | - return (new ResponseService)->createResponse($data, $agent, $ticket); | |
| 102 | - } | |
| 103 | - } | |
| 106 | + return (bool) apply_filters($hookName, false, $request); | |
| 104 | 107 | } |
| 105 | - | |
| 106 | 108 | } |