How to use setWebhook in Telegram with self certificates on Windows 7 and PHP?

15,397

Solution 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>

Solution 2

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

Solution 3

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);
Share:
15,397
Cesare
Author by

Cesare

Updated on June 14, 2022

Comments

  • Cesare
    Cesare almost 2 years

    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