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

Telegram bot无法发送消息 运行代码无输出问题排查求助

Telegram Bot运行无输出问题修复

核心错误点

  • 直接将回调函数hello传入add_handler方法,没有使用CommandHandler封装,调度器无法识别该函数的触发规则,导致代码运行后无响应。

修复后的代码

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext


def hello(update: Update, context: CallbackContext) -> None:
    update.message.reply_text(f'Hello {update.effective_user.first_name}')


# 将引号内内容替换为你自己申请的Bot Token
updater = Updater('my_token')

# 用CommandHandler封装回调函数,指定触发命令为/hello
updater.dispatcher.add_handler(CommandHandler('hello', hello))

updater.start_polling()
updater.idle()

验证步骤

  • 运行修复后的代码,打开Telegram找到对应Bot,发送/hello命令,即可收到Bot的回复内容。
  • 如果仍然无输出,首先检查Token是否填写正确,其次确认运行代码的网络环境可以正常访问Telegram服务。

额外注意

如果你安装的是python-telegram-bot v20及以上版本,该版本已经弃用了Updater相关的同步API,你可以将库降级到v13.7版本使用上述代码,或者按照v20版本的异步规则重写代码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 02:36:05