skip to Main Content

I try to send picture in aiogram but got error Failed to send bytes into the underlying connection Connection
To fix it I try to use proxy but still have the same error

I run this code


API_TOKEN = 'token'
session = AiohttpSession(proxy='https://<user>:<pass>@<proxy>:<port>')

bot = Bot(token=API_TOKEN, session=session)
dp = Dispatcher(storage=MemoryStorage())

def make_picture():
    # Твоя логика обработки сообщения
    # Например, создаем картинку (здесь просто как пример черного изображения)
    img = BytesIO()
    img.name = 'result.png'
    # Генерируем картинку как черное изображение
    from PIL import Image
    image = Image.new('RGB', (200, 200), color='black')
    image.save(img, 'PNG')
    img.seek(0)
    return img

router = Router()


@router.message(Command("start"))
async def start_handler(msg: Message):
    await msg.answer("Hi!")


@router.message()
async def message_handler(message: Message):
    img = make_picture()
    await message.answer_photo(BufferedInputFile(file ='test',filename='result.png'))

async def main():
    dp.include_router(router)
    await bot.delete_webhook(drop_pending_updates=True)
    await dp.start_polling(bot)



if __name__ == "__main__":
    await main()

It works well when I press start command in telegram
But when I write any message and bot should send a picture i gon an error
aiogram.exceptions.TelegramNetworkError: HTTP Client says - ClientConnectionError: Failed to send bytes into the underlying connection Connection<ConnectionKey(host='api.telegram.org', port=443, is_ssl=True, ssl=True, proxy=None, proxy_auth=None, proxy_headers_hash=7572682526898458434)>
It seems that Bot don’t use proxy

So I have two questions:

  1. How to configure aiogram bot with usage of proxy
  2. Is there any other way to send picture without proxy

2

Answers


  1. try this,
    send image solution:

    img = make_picture()
    await message.answer_photo(BufferedInputFile(file=img, filename='result.png'))
    

    explane:
    In the make_picture() function you create an image in BytesIO format, but in message_handler you pass the string ‘test’ to the file parameter, although you should pass an img object. You need to pass the created image from the make_picture() function

    about the proxy issue, i don’t know.
    try to run bot without session like:

    API_TOKEN = 'token'
    #session = AiohttpSession(proxy='https://<user>:<pass>@<proxy>:<port>')
    
    bot = Bot(token=API_TOKEN) # no session 
    dp = Dispatcher(storage=MemoryStorage())
    ...
    
    Login or Signup to reply.
  2. Try this:

    @router.message()
    async def message_handler(message: Message):
        img = make_picture()
        await message.answer_photo(BufferedInputFile(file=img.getvalue(), filename='result.png'))
    

    In the BufferedInputFile function, in the file parameter, you must pass bytes, instead of a BytesIO object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search