Ошибка при выполнении кода (aiogram)
Пишу код погодного тг бота и при его выполнении выдается ошибка:
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0000024D22D6C4D0>
Вот сам код:
from aiogram import Bot, Dispatcher, executor, types
import python_weather
# bot init
bot = Bot(token="тут был токен")
lang = 'ru'
dp = Dispatcher(bot)
client = python_weather.Client(format=python_weather.IMPERIAL)
# echo
@dp.message_handler()
async def echo(message: types.Message):
weather = await client.find(message.text)
celsius = (weather.current.temperature - 32) / 1.8
resp_msg = weather.location_name + "\n"
resp_msg += f"Текущая температура: {celsius}\n"
resp_msg += f"Состояние погоды:{weather.current.sky_text}"
await message.answer(resp_msg)
# run long-polling
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)
Ответы (1 шт):
Автор решения: Creeper Hack
→ Ссылка
вы начали polling прямо в обработчик.
а чтобы обработчик работал надо сначала начать polling.
start_polling должен быть в конце коде и ВНЕ функциях.
вы сейчас должны просто убрать один таб в if __name__..
from aiogram import Bot, Dispatcher, executor, types
import python_weather
# bot init
bot = Bot(token="тут был токен")
lang = 'ru'
dp = Dispatcher(bot)
client = python_weather.Client(format=python_weather.IMPERIAL)
# echo
@dp.message_handler()
async def echo(message: types.Message):
weather = await client.find(message.text)
celsius = (weather.current.temperature - 32) / 1.8
resp_msg = weather.location_name + "\n"
resp_msg += f"Текущая температура: {celsius}\n"
resp_msg += f"Состояние погоды:{weather.current.sky_text}"
await message.answer(resp_msg)
# run long-polling
# ВОТ ЗДЕСЬ
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)