You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

aiogram 3.4.0接收用户位置时出现Update未被处理的问题

解决aiogram 3.4.0中位置消息未被处理的问题

问题描述

使用aiogram 3.4.0开发Telegram机器人时,添加用户位置处理逻辑后,发送位置给机器人会触发日志提示:

INFO:aiogram.event:**Update is not handled.**Duration 2 ms by bot id=xxx

尝试过@dp.message(content_type=ContentType.LOCATION)过滤器,问题依旧。

错误原因

  • Bot实例缺失:代码中未创建Bot实例,main函数直接使用未定义的bot变量,导致Dispatcher无法正常接收和处理更新。
  • 过滤器用法不符合aiogram 3.x规范:直接传入ContentType.LOCATION作为过滤器的写法不适用于aiogram 3.x版本,需要使用新的过滤器语法。
  • 冗余导入:代码中重复导入Command等模块,属于无效冗余代码。

修复方案

步骤1:创建Bot实例

在代码中添加Bot实例的创建,填入你的机器人Token。

步骤2:使用正确的过滤器语法

aiogram 3.x中推荐使用以下两种方式过滤位置消息:

  • F.content_type == enums.ContentType.LOCATION
  • 更简洁的F.location(自动匹配包含位置内容的消息)

步骤3:清理冗余代码

删除重复导入的模块,简化代码结构。

修复后的完整代码

import asyncio
from aiogram import Bot, Dispatcher, types, F, enums
from aiogram.types import Message

# 替换为你的机器人Token
BOT_TOKEN = "你的机器人Token"

dp = Dispatcher()

# 使用F.location过滤位置消息
@dp.message(F.location)
async def handle_location(message: types.Message):
    print('function started')
    lat = message.location.latitude
    lon = message.location.longitude
    reply = f"纬度:  {lat}\n经度: {lon}"
    await message.answer(reply)

async def main():
    bot = Bot(token=BOT_TOKEN)
    await dp.start_polling(bot)

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

补充说明

  • 处理器函数必须在Dispatcher实例化之后定义,确保装饰器能正确注册处理器。
  • 确保机器人Token正确,且机器人已开启隐私模式(若需要接收用户消息)。

内容的提问来源于stack exchange,提问作者Vaelyin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 15:13:33