| 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\ResponseService; |
| 9 |
use FluentSupport\App\Services\Tickets\TicketService; |
| 10 |
|
| 11 |
class HandleTelegramEvent |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* handleEvent method is responsible for handling telegram events |
| 17 |
* @param array $payload |
| 18 |
* @param string $token |
| 19 |
* @return array |
| 20 |
* @throws Exception |
| 21 |
*/ |
| 22 |
public function handleEvent ($payload, $token) |
| 23 |
{ |
| 24 |
|
| 25 |
// Check if user have the pro version installed or not |
| 26 |
$this->verifyProVersion(); |
| 27 |
|
| 28 |
// Check if the token is valid or not |
| 29 |
$this->validateToken($token); |
| 30 |
|
| 31 |
try { |
| 32 |
// Build the response or throw error |
| 33 |
$responseData = $this->parseResponseData($payload); |
| 34 |
|
| 35 |
} catch (\Exception $exception) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
$data = [ |
| 40 |
'conversation_type' => 'response', |
| 41 |
'content' => $responseData['response_text'], |
| 42 |
'source' => 'telegram' |
| 43 |
]; |
| 44 |
|
| 45 |
if(!empty($data['content'])) { |
| 46 |
// Create the response |
| 47 |
$this->createResponse($data, $responseData['ticket_id'], $responseData['agent_id']); |
| 48 |
} |
| 49 |
|
| 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 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return $data; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* verifyProVersion method will check if the pro version is installed or not |
| 68 |
* @throws Exception |
| 69 |
* @return boolean | Exception |
| 70 |
*/ |
| 71 |
private function verifyProVersion () |
| 72 |
{ |
| 73 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 74 |
throw new \Exception('Telegram Integration requires pro version of Fluent Support', 400); |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* validateToken method will check if the token is valid or not |
| 82 |
* @param string $token |
| 83 |
* @throws Exception |
| 84 |
* @return boolean | Exception |
| 85 |
*/ |
| 86 |
private function validateToken ($token) |
| 87 |
{ |
| 88 |
if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) { |
| 89 |
throw new \Exception('Bot Token could not be verified', 404); |
| 90 |
} |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* parseResponseData method will parse the response data and return an array with |
| 96 |
* response text and ticket id and agent id |
| 97 |
* @param array $payload |
| 98 |
* @throws Exception |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
private function parseResponseData ($payload) |
| 102 |
{ |
| 103 |
$responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload); |
| 104 |
|
| 105 |
if ( is_wp_error( $responseData ) ) { |
| 106 |
/* |
| 107 |
* Action on telegram payload error when replying ticket from telegram |
| 108 |
* |
| 109 |
* @since v1.0.0 |
| 110 |
* @param array $responseData |
| 111 |
* @param array $payload |
| 112 |
*/ |
| 113 |
do_action('fluent_support/telegram_payload_error', $responseData, $payload); |
| 114 |
|
| 115 |
throw new \Exception( |
| 116 |
esc_html($responseData->get_error_message()), |
| 117 |
(int) $responseData->get_error_code() |
| 118 |
); |
| 119 |
|
| 120 |
} else { |
| 121 |
return $responseData; |
| 122 |
} |
| 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 |
} |
| 148 |
} |
| 149 |
|