S
S
stulevtoday2021-10-30 08:37:11
Python
stulevtoday, 2021-10-30 08:37:11

The variable is not saved and returned from the aiogram function. What to do?

'user location request'

@dp.message_handler(content_types = ['location'])
    async def handle_location(message: types.Message):
        lat = round(message.location.latitude, 3)
        lon = round(message.location.longitude, 3)
        location = []
        location.append(lat), location.append(lon)
        reply = "Широта:  {}\nДолгота: {}".format(lat, lon)
        await message.answer(reply, reply_markup = types.ReplyKeyboardRemove())
        await bot.send_message(message.from_user.id,
                               'Теперь мы видим, где ты находишься) \n'
                               'Выбери действие и узнаешь погоду в своем городе ',
                               reply_markup = get_weather_keyboard())
        return location
    
'не возвращает location' 

    @dp.message_handler(commands = ['locate_me'])
    async def cmd_locate_me(message: types.Message):
        reply = "Нажмите здесь, чтобы поделиться геопозицией"
        await message.answer(reply, reply_markup = get_location_keyboard())
    
'вызов функции с погодой'
'возвращает None, т.к. функция handle_location не возвращает location'

    @dp.message_handler(content_types = ['text'])
    async def weather(message: types.Message):
        if message.text == 'Погода сейчас':
            reply_current = current_weather(handle_location)
            await message.answer(message.from_user.id, reply_current)
        if message.text == 'Погода на сегодня':
            reply_today = today_weather(handle_location)
            await message.answer(message.from_user.id, reply_today)
        if message.text == 'Погода на завтра':
            reply_tomorrow = tomorrow_weather(handle_location)
            await message.answer(message.from_user.id, reply_tomorrow)
        if message.text == 'Погода на 4 дня':
            reply_week = week_weather(handle_location)
            await message.answer(message.from_user.id, reply_week)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vabots, 2021-10-30
@stulevtoday

I would advise you to write the user's location to the database by type user_id, location
user_id - message.chat.id
And at the moment where you break through weather, again contact the database, find the user's ID and location using the filter. And take the location.
Or advice, try to do it through State, if you don’t know, I can tell you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question