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/Services/ThirdParty/HandleTelegramEvent.php +28 -48 2.3.1 → 2.4.5 View file →
@@ -4,8 +4,9 @@
4 4
5 5 use Exception;
6 6 use FluentSupport\App\Models\Agent;
7 7 use FluentSupport\App\Models\Ticket;
8 +use FluentSupport\App\Services\Tickets\AgentTicketAccess;
8 9 use FluentSupport\App\Services\Tickets\ResponseService;
9 10 use FluentSupport\App\Services\Tickets\TicketService;
10 11
11 12 class HandleTelegramEvent
@@ -12,25 +13,23 @@
12 13 {
13 14
14 15
15 16 /**
16 - * handleEvent method is responsible for handling telegram events
17 + * Reply to (and optionally close) a ticket from a Telegram bot message.
18 + *
17 19 * @param array $payload
18 20 * @param string $token
19 - * @return array
21 + * @return array|false
20 22 * @throws Exception
21 23 */
22 24 public function handleEvent ($payload, $token)
23 25 {
24 26
25 - // Check if user have the pro version installed or not
26 27 $this->verifyProVersion();
27 28
28 - // Check if the token is valid or not
29 29 $this->validateToken($token);
30 30
31 31 try {
32 - // Build the response or throw error
33 32 $responseData = $this->parseResponseData($payload);
34 33
35 34 } catch (\Exception $exception) {
36 35 return false;
@@ -35,8 +34,26 @@
35 34 } catch (\Exception $exception) {
36 35 return false;
37 36 }
38 37
38 + $ticket = Ticket::find($responseData['ticket_id']);
39 +
40 + if (!$ticket) {
41 + return false;
42 + }
43 +
44 + $agent = Agent::find($responseData['agent_id']);
45 +
46 + if (!$agent) {
47 + return false;
48 + }
49 +
50 + // The agent comes from the Telegram sender, not a logged-in user, and every
51 + // mailbox posts to the same chat. Enforce mailbox restrictions here.
52 + if (!(new AgentTicketAccess())->canAccess($agent, $ticket)) {
53 + return false;
54 + }
55 +
39 56 $data = [
40 57 'conversation_type' => 'response',
41 58 'content' => $responseData['response_text'],
42 59 'source' => 'telegram'
@@ -42,22 +59,13 @@
42 59 'source' => 'telegram'
43 60 ];
44 61
45 62 if(!empty($data['content'])) {
46 - // Create the response
47 - $this->createResponse($data, $responseData['ticket_id'], $responseData['agent_id']);
63 + (new ResponseService)->createResponse($data, $agent, $ticket);
48 64 }
49 65
50 - if(!empty($responseData['command'])) {
51 - $command = $responseData['command'];
52 -
53 - if($command == 'close_ticket') {
54 - $ticket = Ticket::find($responseData['ticket_id']);
55 - $agent = Agent::find($responseData['agent_id']);
56 - if($ticket && $agent) {
57 - (new TicketService())->close( $ticket, $agent );
58 - }
59 - }
66 + if(!empty($responseData['command']) && $responseData['command'] == 'close_ticket') {
67 + (new TicketService())->close($ticket, $agent);
60 68 }
61 69
62 70 return $data;
63 71 }
@@ -63,11 +71,10 @@
63 71 }
64 72
65 73
66 74 /**
67 - * verifyProVersion method will check if the pro version is installed or not
68 75 * @throws Exception
69 - * @return boolean | Exception
76 + * @return bool
70 77 */
71 78 private function verifyProVersion ()
72 79 {
73 80 if (!defined('FLUENTSUPPORTPRO')) {
@@ -77,12 +84,11 @@
77 84 return true;
78 85 }
79 86
80 87 /**
81 - * validateToken method will check if the token is valid or not
82 88 * @param string $token
83 89 * @throws Exception
84 - * @return boolean | Exception
90 + * @return bool
85 91 */
86 92 private function validateToken ($token)
87 93 {
88 94 if (!hash_equals(\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken(), $token)) {
@@ -91,10 +97,8 @@
91 97 return true;
92 98 }
93 99
94 100 /**
95 - * parseResponseData method will parse the response data and return an array with
96 - * response text and ticket id and agent id
97 101 * @param array $payload
98 102 * @throws Exception
99 103 * @return array
100 104 */
@@ -103,9 +107,9 @@
103 107 $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload);
104 108
105 109 if ( is_wp_error( $responseData ) ) {
106 110 /*
107 - * Action on telegram payload error when replying ticket from telegram
111 + * Fires when a Telegram reply payload can't be parsed.
108 112 *
109 113 * @since v1.0.0
110 114 * @param array $responseData
111 115 * @param array $payload
@@ -119,30 +123,6 @@
119 123
120 124 } else {
121 125 return $responseData;
122 126 }
123 - }
124 -
125 - /**
126 - * createResponse method will create the response in a ticket
127 - * @param array $data
128 - * @param int $ticket_id
129 - * @param int $agent_id
130 - * @return array
131 - */
132 - private function createResponse ($data, $ticketId, $agentId)
133 - {
134 - $ticket = Ticket::find($ticketId);
135 -
136 - if ( !$ticket ) {
137 - throw new \Exception('No ticket found', 400);
138 - }
139 -
140 - $agent = Agent::find($agentId);
141 -
142 - if ( !$agent ) {
143 - throw new \Exception('No Agent found', 400);
144 - }
145 -
146 - (new ResponseService)->createResponse($data, $agent, $ticket);
147 127 }
148 128 }