| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Telegram Record Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Telegram; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 12 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Record insert,upsert |
| 16 |
*/ |
| 17 |
class RecordApiHelper |
| 18 |
{ |
| 19 |
private $_defaultHeader; |
| 20 |
private $_integrationID; |
| 21 |
private $_logID; |
| 22 |
private $_logResponse; |
| 23 |
private $_entryID; |
| 24 |
private $_apiEndPoint; |
| 25 |
|
| 26 |
public function __construct($apiEndPoint, $integId, $logID, $entryID) |
| 27 |
{ |
| 28 |
// wp_send_json_success($tokenDetails); |
| 29 |
$this->_defaultHeader['Content-Type'] = 'multipart/form-data'; |
| 30 |
$this->_integrationID = $integId; |
| 31 |
$this->_logID = $logID; |
| 32 |
$this->_logResponse = new UtilApiResponse(); |
| 33 |
$this->_entryID = $entryID; |
| 34 |
$this->_apiEndPoint = $apiEndPoint; |
| 35 |
} |
| 36 |
|
| 37 |
public function sendMessages($data) |
| 38 |
{ |
| 39 |
$insertRecordEndpoint = $this->_apiEndPoint . '/sendMessage'; |
| 40 |
return HttpHelper::get($insertRecordEndpoint, $data, $this->_defaultHeader); |
| 41 |
} |
| 42 |
|
| 43 |
public function executeRecordApi($integrationDetails, $fieldValues, $formID, $entryID) |
| 44 |
{ |
| 45 |
$msg = FieldValueHandler::replaceFieldWithValue($integrationDetails->body, $fieldValues); |
| 46 |
$messagesBody = str_replace(['<p>', '</p>'], ' ', $msg); |
| 47 |
|
| 48 |
if (!empty($integrationDetails->actions->attachments)) { |
| 49 |
foreach ($fieldValues as $fieldKey => $fieldValue) { |
| 50 |
if ($integrationDetails->actions->attachments === $fieldKey) { |
| 51 |
$file = $fieldValue; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if (!empty($file)) { |
| 56 |
if (is_array($file) && count($file) > 1) { |
| 57 |
$data = [ |
| 58 |
'chat_id' => $integrationDetails->chat_id, |
| 59 |
'caption' => $messagesBody, |
| 60 |
'media' => $file |
| 61 |
]; |
| 62 |
|
| 63 |
$sendPhotoApiHelper = new FilesApiHelper($formID, $entryID); |
| 64 |
$recordApiResponse = $sendPhotoApiHelper->uploadMultipleFiles($this->_apiEndPoint, $data); |
| 65 |
} else { |
| 66 |
$data = [ |
| 67 |
'chat_id' => $integrationDetails->chat_id, |
| 68 |
'caption' => $messagesBody, |
| 69 |
'parse_mode' => $integrationDetails->parse_mode, |
| 70 |
'photo' => is_array($file) ? $file[0] : $file |
| 71 |
]; |
| 72 |
|
| 73 |
$sendPhotoApiHelper = new FilesApiHelper($formID, $entryID); |
| 74 |
$recordApiResponse = $sendPhotoApiHelper->uploadFiles($this->_apiEndPoint, $data); |
| 75 |
} |
| 76 |
} else { |
| 77 |
$data = [ |
| 78 |
'chat_id' => $integrationDetails->chat_id, |
| 79 |
'text' => $messagesBody, |
| 80 |
'parse_mode' => $integrationDetails->parse_mode |
| 81 |
]; |
| 82 |
$recordApiResponse = $this->sendMessages($data); |
| 83 |
} |
| 84 |
|
| 85 |
$type = 'insert'; |
| 86 |
} else { |
| 87 |
$data = [ |
| 88 |
'chat_id' => $integrationDetails->chat_id, |
| 89 |
'text' => $messagesBody, |
| 90 |
'parse_mode' => $integrationDetails->parse_mode |
| 91 |
]; |
| 92 |
$recordApiResponse = $this->sendMessages($data); |
| 93 |
$type = 'insert'; |
| 94 |
} |
| 95 |
if (is_string($recordApiResponse)) { |
| 96 |
$recordApiResponse = json_decode($recordApiResponse); |
| 97 |
} |
| 98 |
|
| 99 |
if ($recordApiResponse && $recordApiResponse->ok) { |
| 100 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'success', $recordApiResponse); |
| 101 |
} else { |
| 102 |
$this->_logResponse->apiResponse($this->_logID, $this->_integrationID, ['type' => 'record', 'type_name' => $type], 'error', $recordApiResponse); |
| 103 |
} |
| 104 |
return $recordApiResponse; |
| 105 |
} |
| 106 |
} |
| 107 |
|