rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

Heroku Port issue #106

Closed consoleassert closed 8 years ago

consoleassert commented 8 years ago

After deployment my bot with TelegramBots java sdk on heroku, app work fine, but after 90 second it's crashed with log: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. How i can switch listener like this (js telegram sdk): .listen(process.env.PORT || 5000) I can get port from env in java like System.getenv("PORT") but can't find where to put it in TelegramBots sdk.

rubenlagus commented 8 years ago

I really doubt I can do anything with this, it will imply to be checking the default port every time to see if it changed and restart the bot if so... not sure that the best approach.

zsrinivas commented 8 years ago

Hi,

solution is to dynamically construct the internal url given to the TelegramBotsApi contructor, i.e make a TelegramBotsApi object as follows

new TelegramBotsApi(
    BuildVars.pathToCertificateStore, 
    BuildVars.certificateStorePassword, 
    "https://mycoolapp.herokuapp.com:80", 
    "https://127.0.0.1:" + (System.getenv("PORT") != null ? System.getenv("PORT") : "5000"),
    BuildVars.pathToCertificate);

Based on the official heroku examples, if the heroku wishes to change the port, It will definitely restart your app.

rubenlagus commented 8 years ago

@consoleassert Does the solution works for you? If so, can you close the issue?

drunckoder commented 8 years ago

@consoleassert As long as I understand, you can't use webhook bots on heroku. A quote from https://core.telegram.org/bots/webhooks :

You'll need a server that:

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.

As you've said, heroku specifies the port for your app by itself.

So you have to use a longpolling bot.

Longpolling bot is not a WEB app on heroku, as it doesn't listen to the incoming connections! It manually asks telegram servers wether they have a message for your bot.

So all you have to do is: 1) Change your bot type to longpolling 2) Change your heroku app type to worker (inside the procfile, specify it like "worker: _command_tolaunch" instead of "web: _somecommand") 3) Push changes to heroku 4) Launch your bot: "heroku ps:scale worker=1"

Hope this helps you!

rubenlagus commented 8 years ago

@consoleassert Did @drunckoder solution work for you? If so, can you close the issue?