@szjyakgf

Как отправить стикер в телеграм с компьютера?

был код
photo=open("photos/photo.webp", "rb")
await bot.send_sticker(message.chat.id, photo)

все работало, сейчас перешел на последнюю версию aiogram и получаю
pydantic_core._pydantic_core.ValidationError: 2 validation errors for SendSticker
sticker.is-instance[InputFile]
Input should be an instance of InputFile [type=is_instance_of, input_value=<_io.BufferedReader name='skins/cats/cat.webp'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.3/v/is_instance_of
sticker.str
Input should be a valid string [type=string_type, input_value=<_io.BufferedReader name='skins/cats/cat.webp'>, input_type=BufferedReader]
For further information visit https://errors.pydantic.dev/2.3/v/string_type

Пробовал передавать только путь
photo="photos/photo.webp"
await bot.send_sticker(message.chat.id, photo)

venv\Lib\site-packages\aiogram\client\session\base.py", line 120, in check_response
raise TelegramBadRequest(method=method, message=description)
aiogram.exceptions.TelegramBadRequest: Telegram server says - Bad Request: wrong HTTP URL specified
  • Вопрос задан
  • 283 просмотра
Решения вопроса 1
@codingoleg
Отправка файлов и стикеров есть в документации https://core.telegram.org/bots/api#inputfile и https://core.telegram.org/bots/api#sendsticker. Если вы собираетесь больше 1 раза отправлять отдельный стикер, я бы добавил этот стикер в стикерпак и отправлял бы пользователю по ID стикера. Помимо BufferedInputFile можете глянуть соседние FSInputFile и URLInputFile. Вот вам 2 варианта на выбор:
from aiogram import Bot, Dispatcher, F
from aiogram import types
from aiogram.types.input_file import BufferedInputFile
import asyncio

token = 'ВАШ_ТОКЕН'
bot = Bot(token=token)
dp = Dispatcher()

# Открываем файл с компьютера
with open('photos/photo.webp', 'rb') as file:
    input_file = BufferedInputFile(file.read(), 'any_filename')
# Или устанавливаем ID стикера
sticker_id = 'CAACAgIAAxkBAAEKdDVlHm_tbwKnOGandpJwjTBEUXy2zAAC3ggAAgi3GQLYQTVG1h5WQDAE'

@dp.message(F.text == '/start')
async def send_file(message: types.Message):
    # Загрузка файла с комьютера
    await bot.send_sticker(message.chat.id, input_file)
    # Или отправляем по ID стикера
    await bot.send_sticker(message.chat.id, sticker_id)

asyncio.run(dp.start_polling(bot))
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы