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"ap-southeast-1_60ed15bd-eb92-4d89-a5e0-85595ac26530";
  var $template = "qrorder";
  var $client;
  function __construct()
  {
    $this->client = new ClientV3([
      "deviceId" => $this->device_id,
      "refreshToken" => $this->refresh_token,
      "idToken" => $this->id_token
    ]);
  }
  function upload($url)
  {
    //create tmp file 
    $fname = tempnam("/tmp", "rpt") . "-rpt.pdf";
    file_put_contents($fname, file_get_contents($url));
    $resp = $this->client->upload($fname);
    unlink($fname);
    return $resp;
  }
  function send_qrcode($phone, $url, $name, $date)
  {
    $urlImg = $this->upload($url);
    $message = [$name, $date];
    $resp = $this->client->sendMessageTemplateImage($phone, $this->template, "id", $message, $urlImg);
    return json_decode($resp, true);
  }
  function send_otp($phone, $otp)
  {
    $resp = $this->client->sendMessageTemplateAuthentication($phone, "pramitamobileotp", "id", $otp);
    return json_decode($resp, true);
  }
  function send_rujukan_external_process($phone, $phone_cabang)
  {

    $message = [$phone_cabang];
    $resp = $this->client->sendMessageTemplateText($phone, "rujukan01", "id", $message);
    return json_decode($resp, true);
  }
  function send_eform($phone, $kode_cabang, $nolab, $type = "")
  {

    $message = [$kode_cabang, $nolab];
    $resp = $this->client->sendMessageTemplateText($phone, "eform_fisik", "id", $message);
    return json_decode($resp, true);
  }
  function send_msg_button($phone, $url)
  {
    $body = ["hello", "world", "there"];
    $resp = $this->client->sendMessageTemplateButton($phone, "sample-message-button", "id", $body, $url);
    return json_decode($resp, true);
  }
}
