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

如何创建支持用户提问、管理员回复的Telegram机器人?已创建但收不到消息怎么办?

Telegram机器人消息接收与双向回复问题解决方案

基础配置排查

  • 验证机器人Token:确保使用@BotFather生成的正确Token,格式为123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11,无空格或错误字符。
  • 关闭隐私模式:给@BotFather发送/setprivacy,选中目标机器人,设置为Disabled,让机器人能接收群内成员或私人消息。
  • 检查机器人状态:直接给机器人发送测试消息,若无法发送,说明机器人被限制,需联系@BotFather申请解封。

核心功能实现逻辑(以Python为例)

使用python-telegram-bot库实现双向消息转发,代码示例如下:

import logging
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes

# 配置日志
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

# 替换为管理员的Telegram ID(通过@userinfobot获取)
ADMIN_ID = 123456789

async def forward_user_question(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user_msg = update.message
    # 排除管理员自身的消息,避免循环转发
    if user_msg.from_user.id != ADMIN_ID:
        # 转发消息给管理员
        await context.bot.send_message(
            chat_id=ADMIN_ID,
            text=f"用户 {user_msg.from_user.username or user_msg.from_user.first_name} (ID: {user_msg.from_user.id}) 的问题:\n{user_msg.text}"
        )
        # 回复用户已收到问题
        await user_msg.reply_text("你的问题已提交给管理员,我们会尽快回复。")

async def admin_reply_user(update: Update, context: ContextTypes.DEFAULT_TYPE):
    admin_msg = update.message
    # 仅处理管理员的回复命令
    if admin_msg.from_user.id == ADMIN_ID:
        # 管理员回复格式:/reply 用户ID 回复内容
        cmd_parts = admin_msg.text.split(maxsplit=2)
        if len(cmd_parts) >= 3:
            try:
                target_user_id = int(cmd_parts[1])
                reply_content = cmd_parts[2]
                # 发送回复给用户
                await context.bot.send_message(
                    chat_id=target_user_id,
                    text=f"管理员回复:\n{reply_content}"
                )
                await admin_msg.reply_text("回复已发送给用户。")
            except ValueError:
                await admin_msg.reply_text("格式错误,请使用:/reply 用户ID 回复内容")

if __name__ == '__main__':
    # 替换为你的机器人Token
    app = ApplicationBuilder().token("YOUR_BOT_TOKEN").build()
    
    # 监听用户的非命令消息(问题)
    app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, forward_user_question))
    # 监听管理员的回复命令
    app.add_handler(CommandHandler("reply", admin_reply_user))
    
    # 启动机器人
    app.run_polling()

调试与问题排查

  • 查看日志输出:运行代码后,通过日志确认是否有API请求错误、权限问题等。
  • 分步测试:
    1. 让成员单独给机器人发消息,检查管理员是否收到转发。
    2. 管理员用/reply 用户ID 测试内容回复,检查成员是否收到消息。
  • 网络验证:如果部署在服务器,确保服务器能访问Telegram API;本地测试需保证网络可正常连接Telegram。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 20:56:25