from telegram import Update, Bot from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
# 替换为您的 Bot Token BOT_TOKEN = ‘YOUR_BOT_TOKEN’ # 源群组的 Chat ID SOURCE_CHAT_ID = -1001234567890 # 目标群组的 Chat ID TARGET_CHAT_ID = -1009876543210
def media_forwarder(update: Update, context: CallbackContext): message = update.message # 检查消息是否来自指定的源群组 if message.chat_id == SOURCE_CHAT_ID: # 检查消息中是否有媒体文件 if message.photo: context.bot.send_photo(chat_id=TARGET_CHAT_ID, photo=message.photo[-1].file_id, caption=message.caption) elif message.video: context.bot.send_video(chat_id=TARGET_CHAT_ID, video=message.video.file_id, caption=message.caption) elif message.document: context.bot.send_document(chat_id=TARGET_CHAT_ID, document=message.document.file_id, caption=message.caption) elif message.audio: context.bot.send_audio(chat_id=TARGET_CHAT_ID, audio=message.audio.file_id, caption=message.caption) elif message.voice: context.bot.send_voice(chat_id=TARGET_CHAT_ID, voice=message.voice.file_id, caption=message.caption) elif message.animation: context.bot.send_animation(chat_id=TARGET_CHAT_ID, animation=message.animation.file_id, caption=message.caption)
def main(): updater = Updater(token=BOT_TOKEN, use_context=True) dispatcher = updater.dispatcher
# 添加处理媒体文件消息的处理器 media_handler = MessageHandler(Filters.chat(chat_id=SOURCE_CHAT_ID) & Filters.media, media_forwarder) dispatcher.add_handler(media_handler)
# 启动机器人 updater.start_polling() updater.idle()
if __name__ == ‘__main__’: main() |