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