问题
I'm quite a newbye in Telegram and I'm trying moving my first steps with it.
I've found a good tutorial here https://www.youtube.com/watch?v=hJBYojK7DO4: I've configured my Apache 2.4 with PHP and SSL and all works fine, also the samples in the tutorial.
Troubles are using the setWebhook method .... when I try to put in my browser
https://api.telegram.org/<my_bot_code>/setWebHook?url=https://localhost/Telegram/MyYouTubeTutorialBot/YouTubeTutorialBot.php
the response is
{"ok":false,"error_code":400,"description":"Error: Bad webhook: Error: Ip is reserved"}
Note that I'm using a self generated certificate ....
I've found in the api Telegram documentation (ref. https://core.telegram.org/bots/faq#i-39m-having-problems-with-webhooks), that
"..... To use a self-signed certificate, you need to upload your public key certificate using the certificate parameter in setWebhook. Please upload as InputFile, sending a String will not work."
I don't understand how to upload my public key certificate file .... any examples somewhere?
The problem could be because I'm using localhost and the default IP address 127.0.0.1 for my local Apache? Should I change my IP address using the current one that change every time I connect to the web (I'm using a internet key to connect me to the web .....)?
Thank you very much in advance
回答1:
use following simple html code
<html>
<body>
<form action="https://api.telegram.org/bot<BOT_TOCKEN>/setwebhook" method="post" enctype="multipart/form-data">
Select Certificate to upload:
<input type="file" name="certificate" id="fileToUpload">
URL: <input type="text" name="url" value="https://<YOURWEBSITE>/<YOUR_PHP_URL>"><br>
<input type="submit" value="Upload Certificate" name="submit">
</form>
</body>
</html>
回答2:
The following library allows you to easily do that (and quickly set up a bot):
https://github.com/auino/php-telegram-bot-library
It essentially calls Telegram's setWebhook
function/page, passing the self-signed certificate as a file, via a POST
request:
$data = array("url"=>$YOURCALLBACKURL,"certificate"=>"@$CERTIFICATEFILE");
$telegramurl = "https://api.telegram.org/bot$TOKEN/setWebhook";
// now you have to make a request on $telegramurl passing $data via POST (e.g. using curl library)
If you want to use php-telegram-bot-library
, you can easily set it up through the install.php file, or using the following code (it works on Linux, it should work too on Windows systems):
$bot = new telegram_bot($TOKEN);
$bot->set_webhook($WEBHOOKURL, $SSLCERTIFICATEFILE);
回答3:
your local machine is not accessible over Internet by localhost
or your local IP (127.0.0.1
) or local network IP (192.168.1.2
)
each machine has it self's localhost
so telegram's servers localhost is different with your's
you should to use a web hosting
or VPS
to run your script and use its address
I know a free developer VPS : heroku
来源:https://stackoverflow.com/questions/33378216/how-to-use-setwebhook-in-telegram-with-self-certificates-on-windows-7-and-php