D
D
Denis Petrenko2021-05-31 07:57:40
Python
Denis Petrenko, 2021-05-31 07:57:40

How to organize a webhook for telegrams (aiogram)?

There is such an example from the off site of the aiogram:

spoiler
import logging

from aiogram import Bot, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import SendMessage
from aiogram.utils.executor import start_webhook


API_TOKEN = 'BOT_TOKEN_HERE'

# webhook settings
WEBHOOK_HOST = 'https://your.domain'
WEBHOOK_PATH = '/path/to/api'
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"

# webserver settings
WEBAPP_HOST = 'localhost'  # or ip
WEBAPP_PORT = 3001

logging.basicConfig(level=logging.INFO)

bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
dp.middleware.setup(LoggingMiddleware())


@dp.message_handler()
async def echo(message: types.Message):
    # Regular request
    # await bot.send_message(message.chat.id, message.text)

    # or reply INTO webhook
    return SendMessage(message.chat.id, message.text)


async def on_startup(dp):
    await bot.set_webhook(WEBHOOK_URL)
    # insert code here to run it after start


async def on_shutdown(dp):
    logging.warning('Shutting down..')

    # insert code here to run it before shutdown

    # Remove webhook (not acceptable in some cases)
    await bot.delete_webhook()

    # Close DB connection (if used)
    await dp.storage.close()
    await dp.storage.wait_closed()

    logging.warning('Bye!')


if __name__ == '__main__':
    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,
    )


And I don't understand how to make it work. The bot working on the pooling was fine.
What parameters to set Host, Path, Url, etc.? I tried to find answers in the docks, but apparently I didn’t search well. If there are resources related to this, then reset.
server ip conditionally 45.45.45.45

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Altry, 2021-05-31
@Termoslik

To make aiogram work on webhooks, you will need your own server on nginx or apache (nginx is better in my opinion). You will also need a self-signed certificate, or a domain name with SSL.

WEBHOOK_HOST = 'Ip вашего сервера'
WEBHOOK_PATH = 'путь к апи где слушает ваш бот' #или пустое значение, если он слушает на стартовой странице.
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"

It works like this. AioHTTP raises its own server on localhost and listens on a port that you previously opened. NGINX listens on 443 (or one of the ports that Telegram supports for HTTPS) and forwards all requests to your bot app.
Regarding materials for study - a good article on setting up NGINX
And, in fact, a guide from the developers - tyk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question