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

3

2

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

Geoffrey answered 27/10, 2015 at 20:49 Comment(1)
You can't use localhost because 127.0.0.1 is only reachable from your inner computer. Telegram servers can't reach it.Housecoat
I
8

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

Intaglio answered 5/5, 2016 at 7:10 Comment(0)
B
8

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>
Brassbound answered 21/2, 2017 at 18:16 Comment(2)
this is simple post form , so you can upload the certificate to the telegram bot, just replace it with your bot informationBrassbound
<YOUR_PHP_URL> url to what?(file, action)Wineglass
P
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);
Perdue answered 31/10, 2015 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.