在 aiogram 3 中获取位置

问题描述 投票:0回答:1

我使用 aiogram 3.4.0 构建一个机器人,我想为用户位置添加一个处理程序。 当我向机器人发送位置时,以下代码(我删除了一些其他处理程序和导入)引发“INFO:aiogram.event:**未处理更新。**机器人 id = xxx 的持续时间 2 毫秒”。

import asyncio
from aiogram import Bot, Dispatcher, types, F, enums
from aiogram.filters.command import Command
from aiogram.types import Message, location 
from aiogram.filters import Command
from aiogram.types import ContentType


dp = Dispatcher()


async def main():
    await dp.start_polling(bot)

if __name__ == "__main__":
    asyncio.run(main())


@dp.message(ContentType.LOCATION)
async def handle_location(message: types.Message):
    print('function started')
    lat = message.location.latitude
    lon = message.location.longitude
    reply = f"latitude:  {lat}\nlongitude: {lon}"
    await message.answer(reply)`

我还尝试了 @dp.message(content_type=ContentType.LOCATION) 作为过滤器。

telegram aiogram
1个回答
0
投票

尝试使用 MagicFilters (F) 注册处理程序并访问其中的 content_type。

像这样:

from aiogram import F

@dp.message(F.content_type == ContentType.PHOTO)
async def handle_location(message: types.Message):
    print('function started')
    lat = message.location.latitude
    lon = message.location.longitude
    reply = f"latitude:  {lat}\nlongitude: {lon}"
    await message.answer(reply)`
© www.soinside.com 2019 - 2024. All rights reserved.