← All changes
|
app/Http/Controllers/ChatMessageParserController.php
+26
-96
1.5.5
→
2.1.2
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,46 @@ | ||
| 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 | + try { | |
| 29 | 30 | return $this->sendSuccess([ |
| 30 | - 'message' => __('Telegram Integration requires pro version of Fluent Support', 'fluent-support'), | |
| 31 | - 'status' => false | |
| 31 | + 'message' => __('Response has been successfully recorded', 'fluent-support'), | |
| 32 | + 'status' => true, | |
| 33 | + 'data' => $handler->handleEvent($request->all(), $token) | |
| 32 | 34 | ]); |
| 33 | - } | |
| 34 | - | |
| 35 | - // Validate Token | |
| 36 | - if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) { | |
| 37 | - return $this->sendSuccess([ | |
| 38 | - 'message' => __('Bot Token could not be verified', 'fluent-support'), | |
| 35 | + } catch (\Exception $e) { | |
| 36 | + return $this->sendError([ | |
| 37 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 39 | 38 | 'status' => false |
| 40 | 39 | ]); |
| 41 | 40 | } |
| 42 | - | |
| 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(), | |
| 58 | - 'status' => false | |
| 59 | - ]); | |
| 60 | - } | |
| 61 | - | |
| 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 | |
| 74 | - ]); | |
| 75 | - } | |
| 76 | - | |
| 77 | - $agent = Agent::find($responseData['agent_id']); | |
| 78 | - | |
| 79 | - if (!$agent) { | |
| 80 | - return $this->sendSuccess([ | |
| 81 | - 'message' => __('No Agent found', 'fluent-support'), | |
| 82 | - 'status' => false | |
| 83 | - ]); | |
| 84 | - } | |
| 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 | 41 | } |
| 95 | 42 | |
| 96 | 43 | /** |
| 97 | - * handleSlackEvent will get content from slack,check the data validity and create response in a ticket | |
| 98 | - * @param Request $request | |
| 44 | + * handleSlackEvent responsible for getting information from integrated slack request and response | |
| 45 | + * @param HandleSlackEvent $handler | |
| 99 | 46 | * @param $token |
| 100 | - * @return array|\ArrayAccess|mixed | |
| 47 | + * @return array | |
| 101 | 48 | */ |
| 102 | - public function handleSlackEvent(Request $request, $token) | |
| 49 | + public function handleSlackEvent(HandleSlackEvent $handler, $token) | |
| 103 | 50 | { |
| 104 | - if (!defined('FLUENTSUPPORTPRO')) { | |
| 105 | - return $this->sendSuccess([ | |
| 106 | - 'message' => __('Slack Integration requires pro version of Fluent Support', 'fluent-support'), | |
| 107 | - 'status' => false | |
| 51 | + try{ | |
| 52 | + $this->sendSuccess([ | |
| 53 | + 'message' => 'received', | |
| 54 | + 'result' => $handler->handleEvent($token) | |
| 108 | 55 | ]); |
| 109 | - } | |
| 110 | - | |
| 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'), | |
| 56 | + } catch (\Exception $e) { | |
| 57 | + return $this->sendError([ | |
| 58 | + 'message' => Helper::getSafeErrorMessage($e), | |
| 115 | 59 | 'status' => false |
| 116 | 60 | ]); |
| 117 | 61 | } |
| 118 | - | |
| 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 | - ]); | |
| 131 | 62 | } |
| 132 | - | |
| 133 | 63 | } |