PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
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 1.5.5 All 68 releases
fluent-support / app / Services / ThirdParty / HandleTelegramEvent.php

HandleTelegramEvent.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Services/ThirdParty/HandleTelegramEvent.php

137 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\ThirdParty;
4
5 use Exception;
6 use FluentSupport\App\Models\Agent;
7 use FluentSupport\App\Models\Ticket;
8 use FluentSupport\App\Services\Tickets\AgentTicketAccess;
9 use FluentSupport\App\Services\Tickets\ResponseService;
10 use FluentSupport\App\Services\Tickets\TicketService;
11
12 class HandleTelegramEvent
13 {
14
15
16 /**
17 * handleEvent method is responsible for handling telegram events
18 * @param array $payload
19 * @param string $token
20 * @return array
21 * @throws Exception
22 */
23 public function handleEvent ($payload, $token)
24 {
25
26 // Check if user have the pro version installed or not
27 $this->verifyProVersion();
28
29 // Check if the token is valid or not
30 $this->validateToken($token);
31
32 try {
33 // Build the response or throw error
34 $responseData = $this->parseResponseData($payload);
35
36 } catch (\Exception $exception) {
37 return false;
38 }
39
40 $ticket = Ticket::find($responseData['ticket_id']);
41
42 if (!$ticket) {
43 return false;
44 }
45
46 $agent = Agent::find($responseData['agent_id']);
47
48 if (!$agent) {
49 return false;
50 }
51
52 // The agent comes from the Telegram sender id, not a logged-in user, so check
53 // access explicitly: every mailbox notifies the same chat, and an agent excluded
54 // from a box must not reply to or close that box's tickets from here.
55 if (!(new AgentTicketAccess())->canAccess($agent, $ticket)) {
56 return false;
57 }
58
59 $data = [
60 'conversation_type' => 'response',
61 'content' => $responseData['response_text'],
62 'source' => 'telegram'
63 ];
64
65 if(!empty($data['content'])) {
66 // Create the response
67 (new ResponseService)->createResponse($data, $agent, $ticket);
68 }
69
70 if(!empty($responseData['command']) && $responseData['command'] == 'close_ticket') {
71 (new TicketService())->close($ticket, $agent);
72 }
73
74 return $data;
75 }
76
77
78 /**
79 * verifyProVersion method will check if the pro version is installed or not
80 * @throws Exception
81 * @return boolean | Exception
82 */
83 private function verifyProVersion ()
84 {
85 if (!defined('FLUENTSUPPORTPRO')) {
86 throw new \Exception('Telegram Integration requires pro version of Fluent Support', 400);
87 }
88
89 return true;
90 }
91
92 /**
93 * validateToken method will check if the token is valid or not
94 * @param string $token
95 * @throws Exception
96 * @return boolean | Exception
97 */
98 private function validateToken ($token)
99 {
100 if (!hash_equals(\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken(), $token)) {
101 throw new \Exception('Bot Token could not be verified', 403);
102 }
103 return true;
104 }
105
106 /**
107 * parseResponseData method will parse the response data and return an array with
108 * response text and ticket id and agent id
109 * @param array $payload
110 * @throws Exception
111 * @return array
112 */
113 private function parseResponseData ($payload)
114 {
115 $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload);
116
117 if ( is_wp_error( $responseData ) ) {
118 /*
119 * Action on telegram payload error when replying ticket from telegram
120 *
121 * @since v1.0.0
122 * @param array $responseData
123 * @param array $payload
124 */
125 do_action('fluent_support/telegram_payload_error', $responseData, $payload);
126
127 throw new \Exception(
128 esc_html($responseData->get_error_message()),
129 (int) $responseData->get_error_code()
130 );
131
132 } else {
133 return $responseData;
134 }
135 }
136 }
137