| 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 |
/** |
| 12 |
* ChatMessageParserController class is responsible for getting information from integrated 3rd party request and response |
| 13 |
* @package FluentSupport\App\Http\Controllers |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
*/ |
| 17 |
|
| 18 |
class ChatMessageParserController extends Controller |
| 19 |
{ |
| 20 |
/** |
| 21 |
* handleTelegramWebhook will get the content from telegram, check the data validity and create response in a ticket |
| 22 |
* @param Request $request |
| 23 |
* @param $token |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public function handleTelegramWebhook(Request $request, $token) |
| 27 |
{ |
| 28 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 29 |
return $this->sendSuccess([ |
| 30 |
'message' => __('Telegram Integration requires pro version of Fluent Support', 'fluent-support'), |
| 31 |
'status' => false |
| 32 |
]); |
| 33 |
} |
| 34 |
|
| 35 |
// Validate Token |
| 36 |
if (\FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::getWebhookToken() != $token) { |
| 37 |
return $this->sendSuccess([ |
| 38 |
'message' => __('Bot Token could not be verified', 'fluent-support'), |
| 39 |
'status' => false |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
$payload = $request->all(); |
| 44 |
$responseData = \FluentSupportPro\App\Services\Integrations\Telegram\TelegramHelper::parseTelegramBotPayload($payload); |
| 45 |
|
| 46 |
if (is_wp_error($responseData)) { |
| 47 |
/* |
| 48 |
* Action on telegram payload error when replying ticket from telegram |
| 49 |
* |
| 50 |
* @since v1.0.0 |
| 51 |
* @param object $responseData |
| 52 |
* @param object $payload |
| 53 |
*/ |
| 54 |
do_action('fluent_support/telegram_payload_error', $responseData, $payload); |
| 55 |
return $this->sendSuccess([ |
| 56 |
'message' => $responseData->get_error_message(), |
| 57 |
'type' => $responseData->get_error_code(), |
| 58 |
'status' => false |
| 59 |
]); |
| 60 |
} |
| 61 |
|
| 62 |
$data = [ |
| 63 |
'conversation_type' => 'response', |
| 64 |
'content' => $responseData['response_text'], |
| 65 |
'source' => 'telegram' |
| 66 |
]; |
| 67 |
|
| 68 |
$ticket = Ticket::find($responseData['ticket_id']); |
| 69 |
|
| 70 |
if (!$ticket) { |
| 71 |
return $this->sendSuccess([ |
| 72 |
'message' => __('No ticket found', 'fluent-support'), |
| 73 |
'status' => false |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
$agent = Agent::find($responseData['agent_id']); |
| 78 |
|
| 79 |
if (!$agent) { |
| 80 |
return $this->sendSuccess([ |
| 81 |
'message' => __('No Agent found', 'fluent-support'), |
| 82 |
'status' => false |
| 83 |
]); |
| 84 |
} |
| 85 |
|
| 86 |
(new ResponseService)->createResponse($data, $agent, $ticket); |
| 87 |
|
| 88 |
return $this->sendSuccess([ |
| 89 |
'message' => __('Response has been successfully recorded', 'fluent-support'), |
| 90 |
'status' => true, |
| 91 |
'data' => $data |
| 92 |
]); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* handleSlackEvent will get content from slack,check the data validity and create response in a ticket |
| 98 |
* @param Request $request |
| 99 |
* @param $token |
| 100 |
* @return array|\ArrayAccess|mixed |
| 101 |
*/ |
| 102 |
public function handleSlackEvent(Request $request, $token) |
| 103 |
{ |
| 104 |
if (!defined('FLUENTSUPPORTPRO')) { |
| 105 |
return $this->sendSuccess([ |
| 106 |
'message' => __('Slack Integration requires pro version of Fluent Support', 'fluent-support'), |
| 107 |
'status' => false |
| 108 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
// Validate Token |
| 112 |
if (\FluentSupportPro\App\Services\Integrations\Slack\SlackHelper::getWebhookToken() != $token) { |
| 113 |
return $this->sendSuccess([ |
| 114 |
'message' => __('Bot Token could not be verified', 'fluent-support'), |
| 115 |
'status' => false |
| 116 |
]); |
| 117 |
} |
| 118 |
|
| 119 |
if ($request->get('type') == 'url_verification') { |
| 120 |
return $request->get('challenge'); |
| 121 |
} |
| 122 |
|
| 123 |
$event = $request->get('event'); |
| 124 |
|
| 125 |
$result = (new SlackNotification())->processSlackEvent($event); |
| 126 |
|
| 127 |
return $this->sendSuccess([ |
| 128 |
'message' => 'received', |
| 129 |
'result' => $result |
| 130 |
]); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|