PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
← All changes | app/Http/Controllers/ChatMessageParserController.php +72 -78 1.4.0 → 2.4.5 View file →
@@ -1,106 +1,100 @@
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 + * Incoming webhooks from chat integrations (Telegram, Slack).
12 + */
13 +
13 14 class ChatMessageParserController extends Controller
14 15 {
15 - public function handleTelegramWebhook(Request $request, $token)
16 + /**
17 + * Records a Telegram reply as a ticket response.
18 + *
19 + * @param Request $request
20 + * @param HandleTelegramEvent $handler
21 + * @param string $token
22 + * @throws Exception
23 + * @return mixed
24 + */
25 + public function handleTelegramWebhook(Request $request, HandleTelegramEvent $handler, $token)
16 26 {
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 - ]);
27 + if (!$this->verifyWebhookSignature('telegram', $request)) {
28 + return $this->sendError([
29 + 'message' => __('Invalid request signature.', 'fluent-support'),
30 + 'status' => false
31 + ], 403);
23 32 }
24 33
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);
34 + try {
30 35 return $this->sendSuccess([
31 - 'message' => $responseData->get_error_message(),
32 - 'type' => $responseData->get_error_code(),
33 - 'status' => false
36 + 'message' => __('Response has been successfully recorded', 'fluent-support'),
37 + 'status' => true,
38 + 'data' => $handler->handleEvent($request->all(), $token)
34 39 ]);
40 + } catch (\Exception $e) {
41 + return $this->sendError([
42 + 'message' => Helper::getSafeErrorMessage($e),
43 + 'status' => false
44 + ]);
35 45 }
46 + }
36 47
37 - $data = [
38 - 'conversation_type' => 'response',
39 - 'content' => $responseData['response_text'],
40 - 'source' => 'telegram'
41 - ];
48 + /**
49 + * @param Request $request
50 + * @param HandleSlackEvent $handler
51 + * @param string $token
52 + * @return array
53 + */
54 + public function handleSlackEvent(Request $request, HandleSlackEvent $handler, $token)
55 + {
56 + if (!$this->verifyWebhookSignature('slack', $request)) {
57 + return $this->sendError([
58 + 'message' => __('Invalid request signature.', 'fluent-support'),
59 + 'status' => false
60 + ], 403);
61 + }
42 62
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
63 + if ($request->getSafe('type', 'sanitize_text_field') === 'url_verification') {
64 + return new \WP_REST_Response($request->getSafe('challenge', 'sanitize_text_field'), 200, [
65 + 'Content-Type' => 'text/plain; charset=utf-8'
49 66 ]);
50 67 }
51 68
52 - $agent = Agent::find($responseData['agent_id']);
53 -
54 - if(!$agent) {
69 + try {
55 70 return $this->sendSuccess([
56 - 'message' => __('No Agent found', 'fluent-support'),
57 - 'status' => false
71 + 'message' => 'received',
72 + 'result' => $handler->handleEvent($token)
58 73 ]);
74 + } catch (\Exception $e) {
75 + return $this->sendError([
76 + 'message' => Helper::getSafeErrorMessage($e),
77 + 'status' => false
78 + ]);
59 79 }
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 80 }
70 81
71 - public function handleSlackEvent(Request $request)
82 + /**
83 + * Signature checks are opt-in via fluent_support/verify_webhook_signature_{platform}.
84 + * With no verifier hooked, the handlers' token check is the only guard.
85 + *
86 + * @param string $platform 'telegram' or 'slack'
87 + * @param Request $request
88 + * @return bool
89 + */
90 + private function verifyWebhookSignature($platform, Request $request)
72 91 {
73 - $slackSettings = SlackHelper::getSettings();
92 + $hookName = 'fluent_support/verify_webhook_signature_' . $platform;
74 93
75 - if ($slackSettings['reply_from_slack'] == 'yes'){
94 + if (!has_filter($hookName)) {
95 + return true;
96 + }
76 97
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 - }
98 + return (bool) apply_filters($hookName, false, $request);
104 99 }
105 -
106 100 }