initial commit
This commit is contained in:
1
Python/telegram/.env
Normal file
1
Python/telegram/.env
Normal file
@ -0,0 +1 @@
|
||||
TG_TOKEN=5684427592:AAEwHcoWyLCaGi7uaT7Eso_BDzpFusWE6jY
|
39
Python/telegram/main.py
Normal file
39
Python/telegram/main.py
Normal file
@ -0,0 +1,39 @@
|
||||
from io import BytesIO
|
||||
from os import environ
|
||||
|
||||
import dotenv
|
||||
from aiogram import Bot, Dispatcher, executor, types
|
||||
from httpx import AsyncClient
|
||||
|
||||
dotenv.load_dotenv()
|
||||
bot = Bot(token=environ["TG_TOKEN"])
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
@dp.message_handler(commands=["start"])
|
||||
async def start(mes: types.Message) -> None:
|
||||
await mes.reply(
|
||||
f"Привет {mes.from_user.get_mention()}",
|
||||
parse_mode="MarkdownV2",
|
||||
)
|
||||
|
||||
|
||||
@dp.message_handler(commands=["send_pic"])
|
||||
async def send_pic(mes: types.Message) -> None:
|
||||
async with AsyncClient() as client:
|
||||
response = await client.get(
|
||||
"https://loremflickr.com/320/240/dog",
|
||||
follow_redirects=True,
|
||||
)
|
||||
photo = BytesIO(response.content)
|
||||
photo.name = "dog.jpg"
|
||||
await mes.reply_photo(photo, "Here you go")
|
||||
|
||||
|
||||
@dp.message_handler(content_types=types.ContentTypes.TEXT)
|
||||
async def message_handler(mes: types.Message) -> None:
|
||||
await bot.send_message(mes.chat.id, mes.text)
|
||||
print(mes.text)
|
||||
|
||||
|
||||
executor.start_polling(dp)
|
27
Python/telegram/task1.py
Normal file
27
Python/telegram/task1.py
Normal file
@ -0,0 +1,27 @@
|
||||
import asyncio
|
||||
from typing import Never
|
||||
|
||||
|
||||
async def print_nums() -> Never:
|
||||
num = 1
|
||||
while True:
|
||||
num += 1
|
||||
print(num)
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def print_time() -> Never:
|
||||
count = 0
|
||||
while True:
|
||||
if not count % 3:
|
||||
print(f"Time {count}")
|
||||
count += 1
|
||||
await asyncio.sleep(1)
|
||||
|
||||
|
||||
async def main() -> Never:
|
||||
await asyncio.gather(print_nums(), print_time())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
37
Python/telegram/task2.py
Normal file
37
Python/telegram/task2.py
Normal file
@ -0,0 +1,37 @@
|
||||
import asyncio
|
||||
import pathlib
|
||||
from time import perf_counter
|
||||
|
||||
import aiofiles
|
||||
from httpx import AsyncClient, Response
|
||||
|
||||
|
||||
async def get_file(client: AsyncClient, url: str) -> Response:
|
||||
return await client.get(url, follow_redirects=True)
|
||||
|
||||
|
||||
async def write_file(response: Response) -> None:
|
||||
file_name = response.url.path.rsplit("/", 1)[1]
|
||||
print(file_name)
|
||||
async with aiofiles.open(f"photos/{file_name}", "wb") as f:
|
||||
await f.write(response.content)
|
||||
|
||||
|
||||
async def download_file(client: AsyncClient, url: str) -> None:
|
||||
await write_file(await get_file(client, url))
|
||||
|
||||
|
||||
async def main():
|
||||
pathlib.Path("photos").mkdir(exist_ok=True)
|
||||
async with AsyncClient() as client:
|
||||
await asyncio.gather(
|
||||
*(
|
||||
download_file(client, "https://loremflickr.com/320/240")
|
||||
for _ in range(10)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
start = perf_counter()
|
||||
asyncio.run(main())
|
||||
print(perf_counter() - start)
|
Reference in New Issue
Block a user