
本文详解 Telegram Bot 使用 Aiogram 框架时因 __name__ == '__mane__' 拼写错误导致程序未启动、bot 完全无响应的典型故障,提供快速定位、修正及验证方法。
本文详解 telegram bot 使用 aiogram 框架时因 `__name__ == '__mane__'` 拼写错误导致程序未启动、bot 完全无响应的典型故障,提供快速定位、修正及验证方法。
当你运行 python main.py 后 Telegram Bot 完全静默——不回复 /start、不响应任何消息、终端也无日志输出——这往往并非网络、Token 或权限问题,而是一个极易被忽略却致命的 Python 入口检查拼写错误。
核心问题就藏在这行代码中:
if __name__ == '__mane__':
✅ 正确写法应为:
使用 OpenAI Codex CLI 处理编码任务。触发词:codex、code review、fix CI、refactor code、implement feature、coding agent、gpt-5-codex。Clawdbot 可将编码工作委托给 Codex CLI 作为子代理或直接工具。
if __name__ == '__main__':
'__mane__' 是典型的打字错误(将 main 误输为 mane),导致 Python 解释器永远不会执行 asyncio.run(main()),整个 bot 的轮询(dp.start_polling(bot))根本不会启动。因此 bot 实际处于“未运行”状态,自然对任何消息都毫无反应。
✅ 修正后的完整可运行代码如下:
import asyncio
from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram.filters import CommandStart, Command
# 替换为你自己的 Bot Token(务必保密,勿上传至 GitHub)
bot = Bot(token='YOUR_BOT_TOKEN_HERE')
dp = Dispatcher()
@dp.message(CommandStart())
async def cmd_start(message: Message):
await message.answer('Привет!')
await message.reply('Как дела?')
@dp.message(Command('help'))
async def cmd_help(message: Message):
await message.answer('Вы нажали на кнопку помощи')
@dp.message(F.text == 'У меня всё хорошо')
async def nice(message: Message):
await message.answer('Я очень рад')
async def main():
print("? Bot starting...")
await dp.start_polling(bot)
if __name__ == '__main__': # ← 关键修正:此处必须是 '__main__'
try:
asyncio.run(main())
except KeyboardInterrupt:
print('❌ Бот выключен')
? 验证是否修复成功:
- 保存文件后,在终端重新运行:
python main.py
- 观察终端输出:应立即看到 ? Bot starting... 日志,并持续显示 polling 状态(如 Bot started on ...);
- 打开 Telegram,向你的 bot 发送 /start —— 此时应立刻收到两条回复。
⚠️ 其他注意事项:
- Token 安全:切勿在代码中硬编码真实 Token,建议使用环境变量(如 os.getenv("BOT_TOKEN"));
- 网络与权限:确保 bot 已通过 @BotFather 设置为 privacy mode: disabled(若需响应所有消息),且你已开启 bot 对话;
- 依赖版本:确认已安装兼容版本(推荐 aiogram>=3.0),运行 pip install aiogram 升级;
- VS Code 终端:确保你在正确工作目录下执行命令,且 Python 解释器路径配置无误(可通过 which python 或 py -c "import sys; print(sys.executable)" 验证)。
一个字母的拼写差异,足以让整个 bot “隐身”。养成仔细核对 if __name__ == '__main__': 的习惯,是每个 Aiogram 开发者的第一道防线。










