如何将图像转发到与电报机器人 python api 的另一个聊天?

How to forward image to another chat with telegram bot python api?

用户将图像发送到电报机器人。 是否可以在不保存的情况下转发图片或将其发送到另一个聊天室?

我的代码

def reg_text(message):
global text
text = message.text
if len(text.split()) < 2:
    bot.send_message(message.from_user.id, "Too short message.")
    bot.register_next_step_handler(message, reg_text)

else:
    bot.send_message(message.from_user.id, "Perfect! Send an image")
    bot.register_next_step_handler(message, reg_photo)

def reg_photo(消息): #get image id 并将其发送到另一个聊天室(这是常量)

我有一个工作机器人,在众多功能中,有一个正是您想要实现的。此 def 用于识别用户发送的是 image 还是 document。如果它是一个文档(if 循环的一部分),机器人将文档发送到 Telegram 上的一个频道;如果它是图像(else 循环的一部分),机器人将图像发送到同一频道。

def file_handler (update, context):

    if update.message['photo'] == []:
        fileID = update.message['document']['file_id']
        fileName = update.message['document']['file_name']
        context.bot.sendDocument(chat_id = channel_chat_id,
                                 caption = 'image caption',
                                 document = fileID)

    else:
        fileID = update.message['photo'][-1]['file_id']
        context.bot.sendPhoto(chat_id = channel_chat_id,
                              caption = 'image caption',
                              photo = fileID)

请记住,在 def main() 上,您必须有一个 MessageHandler 来处理 def file_handler

dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.document | Filters.photo, file_handler))

让我知道这是否适合您!