2 years ago

#72769

test-img

mrnixdorf

Send JSON data from Python to Telegram using webhook

i have a python application that send data to request.log using webhook.php

webhook.php working code :

<?php

$request = file_get_contents('php://input');
$req_dump = print_r( $request, true );
$fp = file_put_contents( 'request.log', $req_dump );

// Updated Answer
if($json = json_decode(file_get_contents("php://input"), true)){
   $data = $json;
}
print_r($data);

?>

content in request.log is on json format.

I get results in request.log without issue, but i need to send same details to telegram, here's the not working code :

<?php

$request = file_get_contents('php://input');
$req_dump = print_r( $request, true );
$fp = file_put_contents( 'request.log', $req_dump );

// Updated Answer
if($json = json_decode(file_get_contents("php://input"), true)){
   $data = $json;
}
print_r($data);

if($json = json_decode(file_get_contents("php://input"), true)){
    $token = "166.....:AAFlfaCuGdkG3MD1hO.......";
    $chatid = "-51....";
    $message = $data;

    function sendMessage($chatid, $message, $token)
    {
        $url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatid;
        $url = $url . "&text=" . urlencode($message);
        $ch = curl_init();
        $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
        );
        curl_setopt_array($ch, $optArray);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    sendMessage($chatid, $message, $token);
}

?>

Can't figure out how to get those data sent to telegram, if it's impossible to send to request.log and to telegram in same time, is there a way to get updated content from request.log and send them to telegram.

Thank you

python

php

telegram

webhooks

0 Answers

Your Answer

Accepted video resources