D
D
Denis Petrenko2021-05-31 20:37:45
Nginx
Denis Petrenko, 2021-05-31 20:37:45

How to raise ngix in conjunction with iohttp for a telegram bot?

I use aiogram library and nginx server. So far, everything is set to default. There are plans to implement a webhook for the telegram bot, but somehow everything is not going well. So nginx is running in stock. The bot code from the webhook example:

app.py code
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
from aiohttp import web


API_TOKEN = 'BOT_TOKEN_HERE'

# webhook settings
WEBHOOK_HOST = '45.45.45.45'
WEBHOOK_PATH = ''
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())

app = web.Application()
app.add_routes([web.get('/', index)])
web.run_app(app)


@routes.get('/')
async def index(request):
    return web.Response(text="Hello, world")

@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,
    )

It doesn't work, the nginx server started successfully, but aiohttp doesn't seem to be associated with it at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
ky0, 2021-05-31
@Termoslik

Nginx is configured in the standard way in proxy mode to the local port of your application.
I recommend that you immediately google the bundle and set up the url .well-knownfor confirmation when issuing an SSL certificate from Let`s Encrypt.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question