C
C
coneenoc2022-04-18 14:00:54
Python
coneenoc, 2022-04-18 14:00:54

Aiogram how to fix Unclosed connector error?

Good day

I am writing a small bot that will send goods to the telegram channel at startup. That is, I do not need to maintain the session all the time, but only send a post (picture + text) to the cart 2-3 times a day.
Now the code looks like this.

from aiogram import Bot, types
import asyncio

API_TOKEN = '00000000000000000000000000'
CHANNEL_ID = -00000000000000000

bot = Bot(token=API_TOKEN, parse_mode=types.ParseMode.HTML)


async def send_good(chanel_id: int, text: str):
    await bot.send_message(chanel_id, text)


async def start_post():
    posted = False
    while not posted:
        await send_good(CHANNEL_ID, 'Some text')
        posted = True
        break

loop = asyncio.get_event_loop()
loop.run_until_complete(start_post())


On startup I get an error although text is being sent to the channel

Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000026709284F10>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x000002670928C340>, 18442.312)]']
connector: <aiohttp.connector.TCPConnector object at 0x0000026709284D90>


As I understand it, this is due to the fact that the bot is designed for a permanent connection, and if I run the script again, the previous session remains open. I tried to write at the end of loop.close () again - a lot of errors "fall out".
Tell me how to get around this and one more thing, that I need to send a picture signed below with text in bot.send_message (chanel_id, text) to send photo 3 as an argument as text in a bitwise format, or can it be done in some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coneenoc, 2022-04-19
@coneenoc

If anyone is interested, I rewrote the bot like this

import telebot

class TelegramPost:
    TOKEN = settings.config['TOKEN']
    GROUP_ID = settings.config['GROUP_ID']
    bot = telebot.TeleBot(TOKEN, parse_mode='HTML')

    def post_channel(self, image_url, caption):
        photo = open(image_url, 'rb')
        self.bot.send_photo(self.GROUP_ID, photo=photo, caption=caption)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question