# fluent-support/2.4.5/app/Services/ThirdParty/HandleTelegramEvent.php

Fluent Support – Helpdesk &amp; Customer Support Ticket System, version 2.4.5. 129 lines.

- Page: https://pluginprobe.com/plugins/fluent-support/2.4.5/code/app/Services/ThirdParty/HandleTelegramEvent.php
- Raw: https://pluginprobe.com/plugins/fluent-support/2.4.5/raw/app/Services/ThirdParty/HandleTelegramEvent.php
- Modified: 2026-10-09T15:31:16+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/fluent-support/2.4.5/code/app/Services/ThirdParty/HandleTelegramEvent.php#L10-L20`.

```php
<?php

namespace FluentSupport\App\Services\ThirdParty;

use Exception;
use FluentSupport\App\Models\Agent;
use FluentSupport\App\Models\Ticket;
use FluentSupport\App\Services\Tickets\AgentTicketAccess;
use FluentSupport\App\Services\Tickets\ResponseService;
use FluentSupport\App\Services\Tickets\TicketService;

class HandleTelegramEvent
{


    /**
     * Reply to (and optionally close) a ticket from a Telegram bot message.
     *
     * @param array $payload
     * @param string $token
     * @return array|false
     * @throws Exception
     */
    public function handleEvent ($payload, $token)
    {

        $this->verifyProVersion();

        $this->validateToken($token);

        try {
            $responseData = $this->parseResponseData($payload);

        } catch (\Exception $exception) {
            return false;
        }

        $ticket = Ticket::find($responseData['ticket_id']);

        if (!$ticket) {
            return false;
        }

        $agent = Agent::find($responseData['agent_id']);

        if (!$agent) {
            return false;
        }

        // The agent comes from the Telegram sender, not a logged-in user, and every
        // mailbox posts to the same chat. Enforce mailbox restrictions here.
        if (!(new AgentTicketAccess())->canAccess($agent, $ticket)) {
            return false;
        }

        $data = [
            'conversation_type' => 'response',
            'content'           => $responseData['response_text'],
            'source'            => 'telegram'
        ];

        if(!empty($data['content'])) {
            (new ResponseService)->createResponse($data, $agent, $ticket);
        }

        if(!empty($responseData['command']) && $responseData['command'] == 'close_ticket') {
            (new TicketService())->close($ticket, $agent);
        }

        return $data;
    }


    /**
     * @throws Exception
     * @return bool
     */
    private function verifyProVersion ()
    {
        if (!defined('FLUENTSUPPORTPRO')) {
            throw new \Exception('Telegram Integration requires pro version of Fluent Support', 400);
        }

        return true;
    }

    /**
     * @param string $token
     * @throws Exception
     * @return bool
     */
    private function validateToken ($token)
    {
        if (!hash_equals(\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken(), $token)) {
            throw new \Exception('Bot Token could not be verified', 403);
        }
        return true;
    }

    /**
     * @param array $payload
     * @throws Exception
     * @return array
     */
    private function parseResponseData ($payload)
    {
        $responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload);

        if ( is_wp_error( $responseData ) ) {
            /*
             * Fires when a Telegram reply payload can't be parsed.
             *
             * @since v1.0.0
             * @param array $responseData
             * @param array $payload
             */
            do_action('fluent_support/telegram_payload_error', $responseData, $payload);

            throw new \Exception(
                esc_html($responseData->get_error_message()),
                (int) $responseData->get_error_code()
            );

        } else {
            return $responseData;
        }
    }
}

```
