Files
BE_IBL/application/controllers/Wa_webhook.php
2026-04-15 15:24:53 +07:00

59 lines
1.9 KiB
PHP

<?php
class Wa_webhook extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function hook()
{
$bd = file_get_contents("php://input");
file_put_contents("/xtmp/webhook-raw", $bd);
header("Content-type: application/json");
$json = json_decode($bd, true);
if ($json) {
$message = $json["message"]["text"];
if (strpos($message, "#ts") !== false) {
file_put_contents("/xtmp/webhook-raw", "\npost to json\n", FILE_APPEND);
$this->post($json);
} else {
file_put_contents("/xtmp/webhook-raw", "\nno tag : $message\n", FILE_APPEND);
}
} else {
file_put_contents("/xtmp/webhook-raw", "\ninvalid json\n", FILE_APPEND);
}
echo json_encode(["status" => "OK"]);
}
function post($json)
{
//$url = 'http://10.9.10.205:5678/webhook-test/aea7f5ff-f24d-4afe-a52e-8b12d328a3b5';
$url = "http://10.9.10.205:5678/webhook/428e0945-f0fc-4dce-a043-2f789a0cafda";
$jsonData = json_encode($json);
// Initialize cURL
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]);
// Execute request and capture response
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
$c_msg = 'cURL error: ' . curl_error($ch);
} else {
$c_msg = 'Response: ' . $response;
}
file_put_contents("/xtmp/webhook-raw", "\n$c_msg\n", FILE_APPEND);
// Close cURL
curl_close($ch);
}
}