telegramtelegram-botngrokphp-telegram-bottelegram-webhook

How to get Telegram webhook with ngrok working?


I'm developing a Telegram bot and want to use ngrok to receive messages from the webhook.

Setting a webhook works. When I set my publicly reachable domain as URL, I actually get the expected messages. Now, when I know, that the code is working, I want to tunnel messages from the ngrok server to my local VM.

$ ngrok http telegrambot.sandbox.loc:80
ngrok by @inconshreveable                                                                                                                    (Ctrl+C to quit)

Session Status                online                                                                                                                         
Session Expires               7 hours, 59 minutes                                                                                                            
Version                       2.2.8                                                                                                                          
Region                        United States (us)                                                                                                             
Web Interface                 http://127.0.0.1:4040                                                                                                          
Forwarding                    http://0955b8f3.ngrok.io -> telegrambot.sandbox.loc:80                                                                         
Forwarding                    https://0955b8f3.ngrok.io -> telegrambot.sandbox.loc:80                                                                        

Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                    
                              0       0       0.00    0.00    0.00    0.00

Well. Now I set the webhook and get a response like

{
    "ok":true,
    "result":{
        "url":"https://a200f5f7.ngrok.io",
        "has_custom_certificate":false,
        "pending_update_count":0,
        "max_connections":40
    }
}

ngrok's CLI output:

HTTP Requests                                                                                                                                                
-------------                                                                                                                                                

POST /                         502 Bad Gateway                                                                                                               
POST /                         502 Bad Gateway                                                                                                               
POST /                         502 Bad Gateway                                                                                                               
POST /                         502 Bad Gateway 

I didn't get any messages rewarded. Then I added the host name to the /etc/hosts:

127.0.0.1       telegrambot.sandbox.loc

Now I'm getting

HTTP Requests                                                                                                                                                
-------------                                                                                                                                                

POST /                         405 Not Allowed                                                                                                               
POST /                         405 Not Allowed                                                                                                               
POST /                         405 Not Allowed                                                                                                               
POST /                         405 Not Allowed                                                                                                               
POST /                         405 Not Allowed                                                                                                               
POST /                         405 Not Allowed                                                                                                               
GET  /favicon.ico              404 Not Found                                                                                                                 
GET  /                         200 OK           

How to set up and use ngrok correctly and get messages from a Telegram bot forwarded to a local system?


Additional info:

I also tried to get ngrok working on my publicly accessible server. The domain is SSL protected and I don't need this forwarding for the productive system. I just wanted to see, how/whether It would work.

ngrok http mybot.myserver.com:80

Output:

ngrok by @inconshreveable                                                                                                                                                                                                    (Ctrl+C to quit)

Session Status                online
Session Expires               7 hours, 56 minutes
Version                       2.2.8
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://22cf6597.ngrok.io -> mybot.myserver.com:80
Forwarding                    https://22cf6597.ngrok.io -> mybot.myserver.com:80

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

The result (both, with and without the /etc/hosts entry) seems to be OK:

HTTP Requests
-------------

POST /                         200 OK
POST /                         200 OK
POST /                         200 OK
POST /                         200 OK

But the defined URI seems still not to be triggered.


Solution

  • You may try this:

    ngrok http telegrambot.sandbox.loc:80 --host-header=telegrambot.sandbox.loc