O
O
oiaya2022-01-08 15:06:01
Python
oiaya, 2022-01-08 15:06:01

What is the problem with the code, how can I fix it?

import requests
import datetime
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor


bot = Bot(token=tg_bot_token)
dp = Dispatcher(bot)


@dp.message_handler(commands=["weather"])
async def start_command(message: types.Message):
    await message.reply("Введите название города")


@dp.message_handler()
async def get_weather(message: types.Message):
    try:
        r = requests.get(
                f"http://api.openweathermap.org/data/2.5/weather?q={message.text}&appid={open_weather_token}&units=metric"
        )

        data = r.json()

        city = data["name"]
        cur_weather = data["main"]["temp"]
        humidity = data["main"]["humidity"]
        pressure = data["main"]["pressure"]
        wind = data["wind"]["speed"]
        sunrise_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
        sunset_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
        length_of_the_day = datetime.datetime.fromtimestamp(data["sys"]["sunset"]) - datetime.datetime.fromtimestamp(data["sys"]["sunrise"])

        await message.reply(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
          f"Погода в городе: {city}\nТемпература: {cur_weather}C°\n"
          f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\n"
          f"Ветер: {wind} м/с\nВосход солнца: {sunrise_timestamp}\n"
          f"Закат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n***Хорошего дня!***")
    except:
        await message.reply("Проверьте название города")



@dp.message_handler(commands=["ip"])
async def start_command(message: types.Message):
    await message.reply("Введите IP")


@dp.message_handler()
async def get_ip(message: types.Message):
    try:
        r = requests.get(
                f"http://ip-api.com/json/{message.text}"
        )

        data = r.json()

        city = data["city"]
        country = data["country"]
        org = data["org"]
        query = data["query"]
        timezone = data["timezone"]

        await message.reply(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
                            f"Запрос: {query}\nСтрана: {country}\nОрганизация: {org}\n"
                            f"Город: {city}\nЧасовой пояс: {timezone}\n***Хорошего дня!***"
                            )
    except:
        await message.reply("Проверьте айпи адресс")



if __name__ == "__main__":
    executor.start_polling(dp)


There is a code that takes data from the site and sends it to Telegram by the bot, but for some reason, because of the try-except block, the bot does not want to receive anything except the /weather command, it answers all the entered data
await message.reply("Введите название города")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lord Legitov, 2022-01-10
@lordcodes

Finite State Machine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question