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
fluent-support / app / Services / ThirdParty / HandleTelegramEvent.php

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

129 lines 3.3 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 * Reply to (and optionally close) a ticket from a Telegram bot message.
18 *
19 * @param array $payload
20 * @param string $token
21 * @return array|false
22 * @throws Exception
23 */
24 public function handleEvent ($payload, $token)
25 {
26
27 $this->verifyProVersion();
28
29 $this->validateToken($token);
30
31 try {
32 $responseData = $this->parseResponseData($payload);
33
34 } catch (\Exception $exception) {
35 return false;
36 }
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
56 $data = [
57 'conversation_type' => 'response',
58 'content' => $responseData['response_text'],
59 'source' => 'telegram'
60 ];
61
62 if(!empty($data['content'])) {
63 (new ResponseService)->createResponse($data, $agent, $ticket);
64 }
65
66 if(!empty($responseData['command']) && $responseData['command'] == 'close_ticket') {
67 (new TicketService())->close($ticket, $agent);
68 }
69
70 return $data;
71 }
72
73
74 /**
75 * @throws Exception
76 * @return bool
77 */
78 private function verifyProVersion ()
79 {
80 if (!defined('FLUENTSUPPORTPRO')) {
81 throw new \Exception('Telegram Integration requires pro version of Fluent Support', 400);
82 }
83
84 return true;
85 }
86
87 /**
88 * @param string $token
89 * @throws Exception
90 * @return bool
91 */
92 private function validateToken ($token)
93 {
94 if (!hash_equals(\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken(), $token)) {
95 throw new \Exception('Bot Token could not be verified', 403);
96 }
97 return true;
98 }
99
100 /**
101 * @param array $payload
102 * @throws Exception
103 * @return array
104 */
105 private function parseResponseData ($payload)
106 {
107 $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload);
108
109 if ( is_wp_error( $responseData ) ) {
110 /*
111 * Fires when a Telegram reply payload can't be parsed.
112 *
113 * @since v1.0.0
114 * @param array $responseData
115 * @param array $payload
116 */
117 do_action('fluent_support/telegram_payload_error', $responseData, $payload);
118
119 throw new \Exception(
120 esc_html($responseData->get_error_message()),
121 (int) $responseData->get_error_code()
122 );
123
124 } else {
125 return $responseData;
126 }
127 }
128 }
129