You need to enable JavaScript to run this app. Telegram Bot无法接收单条消息携带多张图片的解决方案咨询
优惠活动
大模型
产品
解决方案
定价
更多

Telegram Bot无法接收单条消息携带多张图片的解决方案咨询

问题根因

根据Telegram的官方消息传输规则:
用户一次性发送多张图片(组成媒体组)时,Telegram不会把所有图片打包到同一条消息对象中,而是为每一张图片生成独立的Message实例,所有同组消息共享完全相同的media_group_id字段。

你现有代码存在两个核心问题,导致无法捕获同消息内的全部图片:

  • 代码通过register_next_step_handler仅捕获紧随触发动作的下一条消息,同组后续的图片消息因为触发规则不匹配,会被直接跳过,不会进入图片处理函数
  • 存储结构里upload.media是单值字段,就算拿到多图也会被后续值反复覆盖
实现方案

按照以下逻辑调整即可完整接收同消息内的所有图片:

  • 调整用户状态存储结构,支持按媒体组暂存收集到的所有图片file_id
  • 单独注册content_types=['photo']的全局消息处理器,替代原来靠next_step等待单张图的逻辑
  • 增加短延时收集机制,等同media_group_id的所有图片全部推送完成后,再触发后续业务处理,避免漏收
  • 整组图片处理完成后,及时重置用户的上传状态,避免后续误触发
调整后可正常运行的完整代码
# -*- coding: utf-8 -*-
import config
import telebot
import time
from telebot.types import InputMediaPhoto

bot = telebot.TeleBot(config.token)
main_menu_keyboard = telebot.types.ReplyKeyboardMarkup(True, True)
main_menu_keyboard.add('test1', 'test2')
welcome_message = '''Hello,
dude
'''

# 存储所有用户的上传状态
user_upload_state = {}

# 调整状态类结构,支持多图存储
class UserUpload:
    def __init__(self, name):
        self.name = name
        self.media_list = []  # 存储所有收到的图片file_id
        self.current_media_group_id = None  # 记录正在收集的媒体组ID
        self.collect_start_time = None  # 记录组收集的起始时间


def process_name_step(message):
    try:
        chat_id = message.chat.id
        name = message.text
        # 初始化用户上传状态
        user_upload_state[chat_id] = UserUpload(name)
        bot.send_message(chat_id, 'Upload your photos: ')
    except Exception as e:
        bot.reply_to(message, 'Error...')


# 新增全局图片消息处理器,替代原有的单步等待逻辑
@bot.message_handler(content_types=['photo'])
def handle_incoming_photo(message):
    chat_id = message.chat.id
    # 先判断当前用户是不是处于需要上传图片的状态
    if chat_id not in user_upload_state:
        return
    current_user = user_upload_state[chat_id]
    current_photo_file_id = message.photo[-1].file_id
    # 无media_group_id说明是单张发送的图片,直接处理
    if not message.media_group_id:
        current_user.media_list.append(current_photo_file_id)
        # 处理完成后清空状态,返回收到的所有图
        bot.send_media_group(chat_id, [InputMediaPhoto(media=mid) for mid in current_user.media_list])
        del user_upload_state[chat_id]
        return
    # 遇到新的媒体组ID,重置暂存列表
    if current_user.current_media_group_id != message.media_group_id:
        current_user.media_list = []
        current_user.current_media_group_id = message.media_group_id
    # 每次收到同组消息,刷新收集计时
    current_user.collect_start_time = time.time()
    # 把当前图片加入暂存列表
    if current_photo_file_id not in current_user.media_list:
        current_user.media_list.append(current_photo_file_id)
    # 连续2秒无新增同组消息,判定整组推送完成
    if time.time() - current_user.collect_start_time > 2:
        # 整组收齐,触发后续处理
        bot.send_message(chat_id, f'已收到 {len(current_user.media_list)} 张图片')
        bot.send_media_group(chat_id, [InputMediaPhoto(media=mid) for mid in current_user.media_list])
        # 重置状态
        del user_upload_state[chat_id]


bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, welcome_message, reply_markup=main_menu_keyboard)

@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() == 'test1':
        message = bot.send_message(message.chat.id, 'Enter your name: ')
        bot.register_next_step_handler(message, process_name_step)
    elif message.text.lower() == 'test2':
        bot.send_message(message.chat.id, 'Empty')

bot.polling(none_stop=True)
注意事项
  • 2秒的延时收集阈值是Telegram推送媒体组消息的常规间隔,绝大多数场景下可以收齐全量图片,网络较差场景可以适当调整到3-5秒
  • 不需要额外配置Bot的权限,只要bot能正常接收普通消息即可收到媒体组的所有条目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.18 12:06:03