Indentaion Error
Я хочу создать бота в Telegram, но не могу по сколько присутствует такая ошибка.
@dp.message_handler(commands=["start"])
^
IndentationError: expected an indented block after 'with' statement on line 10
А вот и сам код.
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import python_weather
bot = Bot(token="5946210024:AAGkNfgBnFcDBNQLZIEoKVtlZCikxe7VNis")
dp = Dispatcher(bot)
async with python_weather.Client(format=python_weather.IMPERIAL)as client:
@dp.message_handler(commands=["start"])
async def process_start_command(message):
weather = await client.get(message.text)
celsius = (weather.current.temperature - 32) * 5 / 9
resp_msg = weather.location_name + "\n"
resp_msg += f"Текущая температура:{celsius}\n"
await message.answer(message.text)
@dp.message_handler()
async def echo_message(msg: types.Message):
await bot.send_message(msg.from_user.id, msg.text)
if __name__ == '__main__':
executor.start_polling(dp)
Ответы (1 шт):
Автор решения: diduk001
→ Ссылка
Дело в том, что вы не сделали отступ после объявления функции:
async def process_start_command(message):
и сделали перед ней.
Чтобы код работал, вам нужно изменить его следующим образом:
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
import python_weather
bot = Bot(token="TELEGRAM_BOT_TOKEN")
dp = Dispatcher(bot)
@dp.message_handler(commands=["start"])
async def process_start_command(message):
async with python_weather.Client(format=python_weather.IMPERIAL) as client:
weather = await client.get(message.text)
celsius = (weather.current.temperature - 32) * 5 / 9
resp_msg = weather.location_name + "\n"
resp_msg += f"Текущая температура:{celsius}\n"
#resp_msg +=
await message.answer(message.text)
#await client.close()
@dp.message_handler()
async def echo_message(msg: types.Message):
await bot.send_message(msg.from_user.id, msg.text)
if __name__ == '__main__':
executor.start_polling(dp)
UPD: Также была проблема, связанная с неправильным использованием контекстного менеджера, нужно было перенести строчку:
async with python_weather.Client(format=python_weather.IMPERIAL) as client:
в тело функции, перед получением результата в weather.