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 +66 -73 1.5.1 → 2.4.5 View file →
@@ -1,107 +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\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
10 +/**
11 + * Incoming webhooks from chat integrations (Telegram, Slack).
12 + */
13 +
11 14 class ChatMessageParserController extends Controller
12 15 {
13 - 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)
14 26 {
15 - if (!defined('FLUENTSUPPORTPRO')) {
16 - return $this->sendSuccess([
17 - 'message' => 'Telegram Integration requires pro version of Fluent Support',
27 + if (!$this->verifyWebhookSignature('telegram', $request)) {
28 + return $this->sendError([
29 + 'message' => __('Invalid request signature.', 'fluent-support'),
18 30 'status' => false
19 - ]);
31 + ], 403);
20 32 }
21 33
22 - // Validate Token
23 - if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) {
34 + try {
24 35 return $this->sendSuccess([
25 - 'message' => 'Bot Token could not be verified',
36 + 'message' => __('Response has been successfully recorded', 'fluent-support'),
37 + 'status' => true,
38 + 'data' => $handler->handleEvent($request->all(), $token)
39 + ]);
40 + } catch (\Exception $e) {
41 + return $this->sendError([
42 + 'message' => Helper::getSafeErrorMessage($e),
26 43 'status' => false
27 44 ]);
28 45 }
46 + }
29 47
30 - $payload = $request->all();
31 - $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload);
32 -
33 - if (is_wp_error($responseData)) {
34 - do_action('fluent_support/telegram_payload_error', $responseData, $payload);
35 - return $this->sendSuccess([
36 - 'message' => $responseData->get_error_message(),
37 - 'type' => $responseData->get_error_code(),
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'),
38 59 'status' => false
39 - ]);
60 + ], 403);
40 61 }
41 62
42 - $data = [
43 - 'conversation_type' => 'response',
44 - 'content' => $responseData['response_text'],
45 - 'source' => 'telegram'
46 - ];
47 -
48 - $ticket = Ticket::find($responseData['ticket_id']);
49 -
50 - if (!$ticket) {
51 - return $this->sendSuccess([
52 - 'message' => __('No ticket found', 'fluent-support'),
53 - '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'
54 66 ]);
55 67 }
56 68
57 - $agent = Agent::find($responseData['agent_id']);
58 -
59 - if (!$agent) {
69 + try {
60 70 return $this->sendSuccess([
61 - 'message' => __('No Agent found', 'fluent-support'),
71 + 'message' => 'received',
72 + 'result' => $handler->handleEvent($token)
73 + ]);
74 + } catch (\Exception $e) {
75 + return $this->sendError([
76 + 'message' => Helper::getSafeErrorMessage($e),
62 77 'status' => false
63 78 ]);
64 79 }
65 -
66 - (new ResponseService)->createResponse($data, $agent, $ticket);
67 -
68 - return $this->sendSuccess([
69 - 'message' => __('Response has been successfully recorded', 'fluent-support'),
70 - 'status' => true,
71 - 'data' => $data
72 - ]);
73 -
74 80 }
75 81
76 - public function handleSlackEvent(Request $request, $token)
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)
77 91 {
78 - if (!defined('FLUENTSUPPORTPRO')) {
79 - return $this->sendSuccess([
80 - 'message' => 'Slack Integration requires pro version of Fluent Support',
81 - 'status' => false
82 - ]);
83 - }
92 + $hookName = 'fluent_support/verify_webhook_signature_' . $platform;
84 93
85 - // Validate Token
86 - if (\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken() != $token) {
87 - return $this->sendSuccess([
88 - 'message' => 'Bot Token could not be verified',
89 - 'status' => false
90 - ]);
94 + if (!has_filter($hookName)) {
95 + return true;
91 96 }
92 97
93 - if ($request->get('type') == 'url_verification') {
94 - return $request->get('challenge');
95 - }
96 -
97 - $event = $request->get('event');
98 -
99 - $result = (new SlackNotification())->processSlackEvent($event);
100 -
101 - return $this->sendSuccess([
102 - 'message' => 'received',
103 - 'result' => $result
104 - ]);
98 + return (bool) apply_filters($hookName, false, $request);
105 99 }
106 -
107 100 }