Create own bot on telegram with php
Solution 1
If you use selfsigned ssl you have to point to the ssl path ,, use the ssh to run this command after filling it with your real data ,,
curl -F "url=https://example.com/myscript.php" -F "certificate=@/etc/apache2/ssl/apache.crt" https://api.telegram.org/bot<SECRETTOKEN>/setWebhook
Solution 2
After change to WebHook method, you need to put as follows, since now we are going to handle one message at time. For me works perfectly.
instead
$chatId = $updateArray["result"][0]["message"]["chat"]["id"];
to
$chatID = $update["message"]["chat"]["id"];
Admin
Updated on June 19, 2022Comments
-
Admin almost 2 years
I saw a couple of days ago this tutorial on youtube. It was very interesting, so I decided to make a own bot. I used the code from the tutorial as a template:
<?php $bottoken = "*****"; $website = "https://api.telegram.org/bot".$bottoken; $update = file_get_contents('php://input'); $updatearray = json_decode($update, TRUE); $length = count($updatearray["result"]); $chatid = $updatearray["result"][$length-1]["message"]["chat"]["id"]; $text = $updatearray["result"][$length-1]["message"]["text"]; if($text == 'hy'){ file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=hello"); } elseif($text == 'ciao'){ file_get_contents($website."/sendmessage?chat_id=".$chatid."&text=bye"); }
The script worked if I execute the script manually. However when I use the webhook it doesn't work anymore. The tutorial said that
$update = file_get_contents('php://input');
is the right way, to be used before$update = file_get_contents($website."/getupdates");
. My question how can I usephp://input
to execute my script automatically? The script is on a server from "one.com" and the certificate is also from "one.com". -
SJDS over 7 yearsI have the same problem on two seperate domains. One with GlobalSign and one with Let's Encrypt. Could this still be the problem? I see many people struggling with this issue.
-
SJDS over 7 yearsActually this may be true, but it's the step prior to this that's causing the problem: $update remains empty when you use the webhook combined with
$update = file_get_contents('php://input');