Search code examples
javassltelegram-bottelegram-webhook

Telegram Javabot. Setting webhook


I've created telegram bot on Java with rubenlagus api.And now I can't setup webhook. I know these rules for webhook:

*Supports IPv4, IPv6 is currently not supported for Webhooks.

*Accepts incoming POSTs from 149.154.167.197-233 on port 443,80,88 or 8443.

*Is able to handle TLS1.0+ HTTPS-traffic.

*Provides a supported, non-wildcard, verified or self-signed certificate.

*Uses a CN or SAN that matches the domain you’ve supplied on setup.

*Supplies all intermediate certificates to complete a verification chain.

I have a domain name with verified ssl certificate.Qualys test shows A+ rank.Server Supports IPv4. 443 port is listening. And server accepts incoming POSTs from 149.154.167.197-233 on port 443. I use this rubenlagus api method for creating TelegramApi

private static TelegramBotsApi createNoSelfSignedTelegramBotsApi() throws TelegramApiException { return new TelegramBotsApi( "src/main/resources/server.jks",//path to KeyStore for the server "myPassword", //Key store password for the serve "https://example.com:443", //External url "https://localhost:443"); //Internal url }

I've obtained server.jks via these commands

  • openssl pkcs12 -export -in mydomain.net.crt -inkey mydomain.key > keypair.p12
    • keytool -importkeystore -srckeystore keypair.p12 -destkeystore server.jks -srcstoretype pkcs12

This is my code:

   ApiContextInitializer.init();
    TelegramBotsApi botsApi = new TelegramBotsApi(
                           "src/main/resources/server.jks",
                           "mypassword",
                           "https://example.com:443",
                           "https://localhost:443");
   BotHook webhookBot = new BotHook(options);
   botsApi.registerBot(webhookBot);

When i start program, i recieve this

Jul 28, 2018 3:27:59 PM org.glassfish.grizzly.http.server.NetworkListener start

INFO: Started listener bound to [localhost:443]

Jul 28, 2018 3:27:59 PM org.glassfish.grizzly.http.server.HttpServer start

INFO: [HttpServer] Started.

But bot don't work.I see this in server's logs:

2018/07/29 15:08:43 [error] 1166#1166: *453 openat() "/var/www/www->root/data/www/example.net/callback/WebhookClass failed (2: No such file or >directory), client: 149.154.167.227, server: example.net request: "POST >/callback/WebhookClass HTTP/1.1", host: "example.net"

It seems like Grizzly can't handle http request. When i'm trying to check it via this curl command

curl -X POST -i http://217.0.0.1:443/callback

I recieve this

curl: (7) Failed to connect to 217.0.0.1 port 443: Connection timed out

I checked many times all parameters passed in TelegramBotsApi constructor.


Solution

  • Seems like problem with your infrastructure, not code. TelegramBotsApi starts http Grizzly server on port 443 which handles bot related requests from Telegram. Telegram will access server by it's external URL https://example.com:443.

    Server's logs your provided looks like it is Nginx, am I right? So I assume your have Nginx server configured to accept requests for https://example.com:443. Requests from Telegram are handled by Nginx and not by Grizzly server. Nginx answers with 404 because it has no handlers configured on /callback/* path.

    You have several options to make requests from Telegram sent to example.com to be forwarded to Grizzly:

    • Run your program on server where nginx is running. You need use another port, 8443 for example. Setup your server's firewall rules to allow incoming connections on 8443.
    • Configure nginx to forward all http request matching /callback/* to your Grizzly server. Add following to server section of nginx config file:

      location /callback {
          proxy_pass https://<YOUR_TELEGRAM_BOT_SERVER>:443;
      }
      

    Where YOUR_TELEGRAM_BOT_SERVER is host name or IP of server running your program. When registering bot api make sure to use the same certificate as your nginx server.