A
A
Andrey Begin2021-08-13 16:27:12
Python
Andrey Begin, 2021-08-13 16:27:12

Error 403 // Telegram Bot on webhooks?

Hello. I am writing a telegram bot on a pooling, I decided to upload it to the server and at the same time transfer it to webhooks. I rummaged through the Internet, from the source I found this code:

import logging

from aiogram import Bot, types, executor
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 aiogram.types import ParseMode

API_TOKEN = '000000:000000'

WEBHOOK_HOST = f'https://my domain'
WEBHOOK_PATH = f'/index.py'
WEBHOOK_URL = f'{WEBHOOK_HOST}{WEBHOOK_PATH}'

# webserver settings
WEBAPP_HOST = 'my domain.net'  # or ip   pip install --upgrade pip
WEBAPP_PORT = 8443

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):
    print('echo enter')
    return SendMessage(message.chat.id, message.text)


async def on_startup(dp):

    await bot.delete_webhook()
    await bot.set_webhook(url = WEBHOOK_URL)

    # Пытаюсь отправить сразу на свой айдишник сообщение
    return SendMessage(867690422, 'test')

async def on_shutdown(dp):
    await bot.delete_webhook()

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

    logging.warning('Bye!')


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


Further, as I understand, I must perform the following steps (Correct me if I'm wrong somewhere)
  1. I upload the script above at "my.domain.net/index.py" to my hosting (REG.RU took it for a month)
  2. Then I set the WEBHOOK_PATH variable to '/index.py', and in the WEBHOOK_URL I specify the full path to index.py, and of course I substitute my token from the bot.
  3. Then I register my webhook at my url 'my.domain.net/index.py' with the line below:
    await bot.set_webhook(url = WEBHOOK_URL)Displays a message that the webhooks have been successfully registered.
  4. I start the bot and send messages to it, on the hosting in the log from the cart, requests begin to arrive
    91.108.6.91 - - [13/Aug/2021:15:38:40 +0300] "POST /index.py HTTP/1.0" 403 299327 "- ""-"
    Which means that access to this page is denied by the hosting. And accordingly, the bot itself does not give an answer in the cart.


This begs the question, am I doing everything right? Do I really need to specify the path to my script in WEBHOOK_URL? If everything is OK, then how can I open access for this link?

I tried to accept requests from a cart in a PHP script and everything works with a bang. But the same thing cannot be done in python with the code above, since the hosting blocks any py scripts. Although in the domain settings there is a daw for allowing CGI scripts + Python 3.9

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Altry, 2021-08-13
@MoVVe

Once there was a similar topic.
I advise you to raise the bot on the localhost on the port of your choice, and then in the web server settings (nginx or apache) proxy all requests for a specific url to the localhost:port of your bot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question