M
M
Murat500052022-02-05 21:52:22
Bots
Murat50005, 2022-02-05 21:52:22

Can aiogram and flask be friends?

Can aiogram and flask be friends?
How to make "flask" and "aiogram" friends, the web interface will be written in "flask" and the bot in "aiogram" to launch the bot with a single button click in the web interface. Is it possible to run polling in a separate thread and how to do it?
webhook not considered

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2022-02-11
@kshnkvn

You can run in a separate process, so that later, if necessary, you can stop this process:

from multiprocessing import Process

from flask import Flask
from aiogram import Bot, Dispatcher, executor, types

import config

app = Flask(import_name=__name__)

bot = Bot(token=config.BOT_TOKEN)
dispatcher = Dispatcher(bot=bot)


def bot_start_polling():
    executor.start_polling(dispatcher=dispatcher, skip_updates=True)


@dispatcher.message_handler(commands=['start'])
async def bot_handler_start(message: types.Message):
    await message.reply('Foo')


@app.get(rule='/start_bot')
def start_bot():
    bot_process = Process(target=bot_start_polling)
    bot_process.start()

    return str(bot_process.pid)


if __name__ == '__main__':
    app.run()

But it's very stupid to do this, since you are using flask, hang up a webhook and do it normally. Any solution other than that would be a very strange crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question