S
S
SashaN692021-04-24 10:35:18
Flask
SashaN69, 2021-04-24 10:35:18

Webhook Flask + aiogram on pythonanywhere?

I decided to try to make a bot with a webhook, but there was a problem when I started everything on my PC using ngrok, everything worked fine, then I decided to throw it on pythonanywhere. these requests in a bot?

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_PATH = ""
WEBHOOK_URL =  "https://Dimon0825.pythonanywhere.com"

# webserver settings
WEBAPP_HOST = '127.0.0.1'  # or ip
WEBAPP_PORT = 5003

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


#webhook
from flask import Flask

app = Flask(__name__)

@app.route('/', methods=['POST'])
def hello_world():
    return 'Hello from Flask!'

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=5003)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Sofonov, 2021-06-05
@sofcom

in the word "nasal", there should be two letters "s"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question