1

I have tried this script which is similar to aiogram official example for webhook deployment.

import os
import logging

from aiogram import Bot, Dispatcher, types, executor

PROJECT_NAME = os.environ.get("PROJ")
TOKEN = os.environ.get("TOKEN")

WEBHOOK_HOST = f"https://{PROJECT_NAME}.herokuapp.com"
WEBHOOK_PATH = "/webhook/" + TOKEN
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"

WEBAPP_HOST = "localhost"
WEBAPP_PORT = 8443

bot = Bot(TOKEN)
dp = Dispatcher(bot)

logging.basicConfig(level=logging.INFO)


# Example handler
@dp.message_handler(commands="start")
async def start_handler(message: types.Message):
    await bot.send_message(message.chat.id, text="hi")


# Run after startup
async def on_startup():
    await bot.delete_webhook()
    await bot.set_webhook(WEBHOOK_URL)


# Run before shutdown
async def on_shutdown():
    logging.warning("Shutting down..")
    await bot.delete_webhook()
    await dp.storage.close()
    await dp.storage.wait_closed()
    logging.warning("Bot down")


if __name__ == "__main__":
    if "HEROKU" in list(os.environ.keys()):
        executor.start_webhook(
            dispatcher=dp,
            webhook_path=WEBHOOK_PATH,
            on_startup=on_startup,
            on_shutdown=on_shutdown,
            skip_updates=True,
            host=WEBAPP_HOST,
            port=WEBAPP_PORT,
        )
    else:
        executor.start_polling(dp)

Polling worked fine for a few seconds and then crashed.

I have set the environment variables as follows:

TOKEN ( token from BotFather ) , PROJ ( name of Heroku project )

AND HEROKU ( valueless )

After deploying,

2020-11-21T16:06:33.939710+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-21T16:06:38.339135+00:00 heroku[web.1]: Starting process with command `python bot.py`
2020-11-21T16:06:42.115941+00:00 heroku[web.1]: Process exited with status 1
2020-11-21T16:06:42.169072+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-21T16:06:41.819141+00:00 app[web.1]: INFO:aiogram:Bot: AahnikTester [@aahniks_tester_bot]
2020-11-21T16:06:41.987742+00:00 app[web.1]: WARNING:aiogram:Updates were skipped successfully.
2020-11-21T16:06:41.990985+00:00 app[web.1]: ERROR:asyncio:unhandled exception during asyncio.run() shutdown
2020-11-21T16:06:41.990986+00:00 app[web.1]: task: <Task finished name='Task-6' coro=<_run_app() done, defined at /app/.heroku/python/lib/python3.8/site-packages/aiohttp/web.py:287> exception=TypeError('on_startup() takes 0 positional arguments but 1 was given')>
2020-11-21T16:06:41.990986+00:00 app[web.1]: Traceback (most recent call last):
2020-11-21T16:06:41.990987+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web.py", line 508, in run_app
2020-11-21T16:06:41.990987+00:00 app[web.1]:     loop.run_until_complete(main_task)
2020-11-21T16:06:41.990988+00:00 app[web.1]:   File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
2020-11-21T16:06:41.990988+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web.py", line 319, in _run_app
2020-11-21T16:06:41.990989+00:00 app[web.1]:     await runner.setup()
2020-11-21T16:06:41.990990+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_runner.py", line 275, in setup
2020-11-21T16:06:41.990990+00:00 app[web.1]:     self._server = await self._make_server()
2020-11-21T16:06:41.990990+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_runner.py", line 375, in _make_server
2020-11-21T16:06:41.990991+00:00 app[web.1]:     await self._app.startup()
2020-11-21T16:06:41.990991+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_app.py", line 416, in startup
2020-11-21T16:06:41.990992+00:00 app[web.1]:     await self.on_startup.send(self)
2020-11-21T16:06:41.990992+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/signals.py", line 34, in send
2020-11-21T16:06:41.990993+00:00 app[web.1]:     await receiver(*args, **kwargs)  # type: ignore
2020-11-21T16:06:41.990993+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiogram/utils/executor.py", line 250, in _wrap_callback
2020-11-21T16:06:41.990993+00:00 app[web.1]:     return await cb(self.dispatcher)
2020-11-21T16:06:41.990994+00:00 app[web.1]: TypeError: on_startup() takes 0 positional arguments but 1 was given
2020-11-21T16:06:41.991678+00:00 app[web.1]: Traceback (most recent call last):
2020-11-21T16:06:41.991741+00:00 app[web.1]:   File "bot.py", line 45, in <module>
2020-11-21T16:06:41.992057+00:00 app[web.1]:     executor.start_webhook(
2020-11-21T16:06:41.992112+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiogram/utils/executor.py", line 98, in start_webhook
2020-11-21T16:06:41.992386+00:00 app[web.1]:     executor.run_app(**kwargs)
2020-11-21T16:06:41.992389+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiogram/utils/executor.py", line 282, in run_app
2020-11-21T16:06:41.992703+00:00 app[web.1]:     web.run_app(self._web_app, **kwargs)
2020-11-21T16:06:41.992706+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web.py", line 508, in run_app
2020-11-21T16:06:41.993089+00:00 app[web.1]:     loop.run_until_complete(main_task)
2020-11-21T16:06:41.993090+00:00 app[web.1]:   File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
2020-11-21T16:06:41.993309+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web.py", line 319, in _run_app
2020-11-21T16:06:41.993604+00:00 app[web.1]:     await runner.setup()
2020-11-21T16:06:41.993607+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_runner.py", line 275, in setup
2020-11-21T16:06:41.993862+00:00 app[web.1]:     self._server = await self._make_server()
2020-11-21T16:06:41.993865+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_runner.py", line 375, in _make_server
2020-11-21T16:06:41.994180+00:00 app[web.1]:     await self._app.startup()
2020-11-21T16:06:41.994186+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/web_app.py", line 416, in startup
2020-11-21T16:06:41.994510+00:00 app[web.1]:     await self.on_startup.send(self)
2020-11-21T16:06:41.994534+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiohttp/signals.py", line 34, in send
2020-11-21T16:06:41.994724+00:00 app[web.1]:     await receiver(*args, **kwargs)  # type: ignore
2020-11-21T16:06:41.994724+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.8/site-packages/aiogram/utils/executor.py", line 250, in _wrap_callback
2020-11-21T16:06:41.994977+00:00 app[web.1]:     return await cb(self.dispatcher)
2020-11-21T16:06:41.995020+00:00 app[web.1]: TypeError: on_startup() takes 0 positional arguments but 1 was given

I am looking for an example of aiogram's deployment to heroku using webhook.

With some tweaks to above code, this came

2020-11-26T09:32:27.283413+00:00 app[web.1]: WARNING:aiogram:Updates were skipped successfully.
2020-11-26T09:32:27.283425+00:00 app[web.1]: WARNING:root:Starting webhook
2020-11-26T09:32:27.455462+00:00 app[web.1]: WARNING:root:Webhook set
2020-11-26T09:32:27.457534+00:00 app[web.1]: ======== Running on http://localhost:8443 ========
2020-11-26T09:32:27.457535+00:00 app[web.1]: (Press CTRL+C to quit)
2020-11-26T09:33:23.679946+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

how to fix this ? ( in heroku )

with some other tweaks

the webhook is being set correctly because telegram is sending updates. but they are not being handled. thats why another error is coming.

2020-11-26T09:52:48.395734+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/webhook/secret_key..." host=radiant-caverns-48683.herokuapp.com

1 Answer 1

1

Thanks @evgfilim1 and @AsyncAwait who helped me on aiogram's telegram group.

Important points:

  1. At async def on_startup(): and async def on_shutdown(): need dispatcher: Dispatcher

Example:

from aiogram import Dispatcher

async def on_startup(dispatcher: Dispatcher) -> None:
  1. Use int(os.getenv("PORT")) instead of 8443

  2. Try binding to 0.0.0.0 instead of localhost

Here is a complete example for deployment of an aiogram bot that uses webhooks to fetch updates to Heroku

https://github.com/aahnik/webhook-aiogram-heroku

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.