| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Ticket; |
| 7 |
use FluentSupport\App\Services\Tickets\ResponseService; |
| 8 |
use FluentSupport\Framework\Request\Request; |
| 9 |
use FluentSupportPro\App\Services\Integrations\Slack\SlackNotification; |
| 10 |
|
| 11 |
class ChatMessageParserController extends Controller |
| 12 |
{ |
| 13 |
public function handleTelegramWebhook(Request $request, $token) |
| 14 |
{ |
| 15 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 16 |
return $this->sendSuccess([ |
| 17 |
'message' => 'Telegram Integration requires pro version of Fluent Support', |
| 18 |
'status' => false |
| 19 |
]); |
| 20 |
} |
| 21 |
|
| 22 |
// Validate Token |
| 23 |
if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) { |
| 24 |
return $this->sendSuccess([ |
| 25 |
'message' => 'Bot Token could not be verified', |
| 26 |
'status' => false |
| 27 |
]); |
| 28 |
} |
| 29 |
|
| 30 |
$payload = $request->all(); |
| 31 |
$responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload); |
| 32 |
|
| 33 |
if (is_wp_error($responseData)) { |
| 34 |
do_action('fluent_support/telegram_payload_error', $responseData, $payload); |
| 35 |
return $this->sendSuccess([ |
| 36 |
'message' => $responseData->get_error_message(), |
| 37 |
'type' => $responseData->get_error_code(), |
| 38 |
'status' => false |
| 39 |
]); |
| 40 |
} |
| 41 |
|
| 42 |
$data = [ |
| 43 |
'conversation_type' => 'response', |
| 44 |
'content' => $responseData['response_text'], |
| 45 |
'source' => 'telegram' |
| 46 |
]; |
| 47 |
|
| 48 |
$ticket = Ticket::find($responseData['ticket_id']); |
| 49 |
|
| 50 |
if (!$ticket) { |
| 51 |
return $this->sendSuccess([ |
| 52 |
'message' => __('No ticket found', 'fluent-support'), |
| 53 |
'status' => false |
| 54 |
]); |
| 55 |
} |
| 56 |
|
| 57 |
$agent = Agent::find($responseData['agent_id']); |
| 58 |
|
| 59 |
if (!$agent) { |
| 60 |
return $this->sendSuccess([ |
| 61 |
'message' => __('No Agent found', 'fluent-support'), |
| 62 |
'status' => false |
| 63 |
]); |
| 64 |
} |
| 65 |
|
| 66 |
(new ResponseService)->createResponse($data, $agent, $ticket); |
| 67 |
|
| 68 |
return $this->sendSuccess([ |
| 69 |
'message' => __('Response has been successfully recorded', 'fluent-support'), |
| 70 |
'status' => true, |
| 71 |
'data' => $data |
| 72 |
]); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public function handleSlackEvent(Request $request, $token) |
| 77 |
{ |
| 78 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 79 |
return $this->sendSuccess([ |
| 80 |
'message' => 'Slack Integration requires pro version of Fluent Support', |
| 81 |
'status' => false |
| 82 |
]); |
| 83 |
} |
| 84 |
|
| 85 |
// Validate Token |
| 86 |
if (\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken() != $token) { |
| 87 |
return $this->sendSuccess([ |
| 88 |
'message' => 'Bot Token could not be verified', |
| 89 |
'status' => false |
| 90 |
]); |
| 91 |
} |
| 92 |
|
| 93 |
if ($request->get('type') == 'url_verification') { |
| 94 |
return $request->get('challenge'); |
| 95 |
} |
| 96 |
|
| 97 |
$event = $request->get('event'); |
| 98 |
|
| 99 |
$result = (new SlackNotification())->processSlackEvent($event); |
| 100 |
|
| 101 |
return $this->sendSuccess([ |
| 102 |
'message' => 'received', |
| 103 |
'result' => $result |
| 104 |
]); |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|