Files
BE_CPONE/application/controllers/Qontak_api.php
2026-04-27 10:26:26 +07:00

181 lines
4.7 KiB
PHP

<?php
class Qontak_api extends MY_Controller
{
var $token = "WWEfmnLJXPBGBQodjshDQMQgeyhVDbNDC-VR0apf2lc";
var $wa_integration_id = "db560b73-53d1-422f-9482-b3d895191ece";
var $template_id = "54770b4e-8603-4dc2-a148-f17447cb2aa6";
function __construct()
{
parent::__construct();
}
function send_wa()
{
$url = "https://service-chat.qontak.com/api/open/v1/broadcasts/whatsapp/direct";
$hp = $this->sys_input["hp"];
$hp_name = $this->sys_input["hp_name"];
$patient_name = $this->sys_input["patient_name"];
$filename = $this->sys_input["filename"];
$result_date = $this->sys_input["result_date"];
$uploaded_url_doc = "https://cdn.qontak.com/uploads/direct/files/2bdb1add-fb1f-4d5e-aa66-5cee1cb4864e/example-report.pdf";
$param = [
"to_name" => $hp_name,
"to_number" => $hp,
"message_template_id" => $this->template_id,
"channel_integration_id" => $this->wa_integration_id,
"language" => [
"code" => "id"
],
"parameters" => [
"header" => [
"format" => "DOCUMENT",
"params" => [
[
"key" => "url",
"value" => $uploaded_url_doc
],
[
"key" => "filename",
"value" => $filename
]
]
],
"body" => [
[
"key" => 1,
"value" => "nama",
"value_text" => $hp_name
],
[
"key" => 2,
"value" => "tipe",
"value_text" => $patient_name
],
[
"key" => 3,
"value" => "berlaku",
"value_text" => $result_date
],
]
]
];
$json_param = json_encode($param);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json_param,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer {$this->token}",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error != "") {
echo json_encode(["status" => "ERR", "message" => $error]);
exit;
}
echo $response;
}
function get_integration()
{
$url = "https://service-chat.qontak.com/api/open/v1/integrations?target_channel=wa&limit=10";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => false,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->token}",
],
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error != "") {
echo json_encode(["status" => "ERR", "message" => $error]);
exit;
}
echo $response;
}
function upload_file()
{
$url = "https://service-chat.qontak.com/api/open/v1/file_uploader";
$fileName = $this->sys_input["file_name"];
$rpt_url = $this->sys_input["rpt_url"];
$mimeType = $this->sys_input["mime"];
$fileContents = file_get_contents($rpt_url);
$boundary = uniqid();
$body = "--$boundary\r\n" .
"Content-Disposition: form-data; name=\"file\"; filename=\"$fileName\"\r\n" .
"Content-Type: $mimeType\r\n\r\n" .
$fileContents . "\r\n" .
"--$boundary--\r\n";
// Set cURL options
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->token}",
"Content-Type: multipart/form-data; boundary=$boundary"
],
CURLOPT_POSTFIELDS => $body
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error) {
echo json_encode(["status" => "ERR", "message" => $error]);
exit;
}
echo $response;
}
function list_template()
{
$url = "https://service-chat.qontak.com/api/open/v1/templates/whatsapp";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => false,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer {$this->token}",
],
]);
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
if ($error != "") {
echo json_encode(["status" => "ERR", "message" => $error]);
exit;
}
echo $response;
}
}