aiogram / aiogram

aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
https://aiogram.dev
MIT License
4.75k stars 835 forks source link

Add telegram test server #1542

Closed mrafieefard closed 4 months ago

mrafieefard commented 4 months ago

aiogram version

3.x

Problem

I want to use webapp with localhost and as you know we can create account in test server and use localhost links for webapp

Possible solution

idk

Alternatives

No response

Code example

No response

Additional information

No response

Vadim-Khristenko commented 4 months ago

Here is the solution to your problem:

from aiogram.client.telegram import TelegramAPIServer
from aiogram.client.session.aiohttp import AiohttpSession 
# Or you can import: from aiogram.client.telegram import TEST

test_servers = AiohttpSession(
    api=TelegramAPIServer(
        base="https://api.telegram.org/bot{token}/test/{method}",
        file="https://api.telegram.org/file/bot{token}/test/{path}",
    )
)

bot = Bot(
        token=os.getenv('TEST_TOKEN'), # This token must be created on the Telegram test server in BotFather
        session=test_servers, # Or you can insert TEST variable here
        default=DefaultBotProperties(parse_mode=ParseMode.HTML
)

I also want to say that now all newly created bots on the Test servers do not connect to the Telegram Bot API (At the time: 07/22/2024 5:30 UTC). Q: What does this mean? A: This means that if you try to execute absolutely any request with a newly created bot, you will receive a 401 unauthorized error. At the moment, only bots created earlier on July 10-12 are working. (I mean tokens, if you reset a token from a bot that was created before July 10-12, then most likely you will disconnect it from the API)

If this helped you, then close the issue.

Vadim-Khristenko commented 4 months ago

token=os.getenv('TEST_TOKEN'), # This token must be created on the Telegram test server in BotFather

This line of code uses getenv, but if you want, you can use the usual string.

mrafieefard commented 4 months ago

Thanks for your help ❤