0

i want my telegram bot to notify my php script when new message is received ... basically when someone send a message to my telegram bot ... i want my bot redirect that message to a php script

i've set the webhook

function set_webhook(){
        $adta = file_get_contents("https://api.telegram.org/bottoken/setWebhook?url=". base_url()."index/telegramcallback");
        var_dump($adta);
}

and it was hooked ok

now in my callback function i have something like

function telegramcallback(){


        $to      = 'mymail@gmail.com';
$subject = 'new telegram message';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, json_encode($_REQUEST), $headers);

}

and it kinda works , but i receive an empty email ... im not sure if im getting message body wrong or telegram bot doesnt send the messages just calls the callback url ?

hretic
  • 999
  • 9
  • 36
  • 78

1 Answers1

0

When someone interacts with your bot you will receive JSON-serialized Update objects as a result.

To retrieve this data use file_get_contents("php://input").

mail($to, $subject, json_encode(file_get_contents ( "php://input" )), $headers);

Superglobal $_REQUEST only works for certain content types.

See this question: PHP "php://input" vs $_POST

newsha
  • 1,288
  • 1
  • 10
  • 13