首页 > 解决方案 > Save an image or several files what my telegram bot gets, to a specific directory

问题描述

@self.__bot.message_handler(content_types=['photo'])
def on_photo(message):
  file_id = message.photo[-1].file_id
  file_info = self.__bot.get_file(file_id)

I created a bot in the telegram and one of the commands is to send a picture to create a post from it. I want the image you send to it to be saved in the folder of my choice ... How do you do that? I use telebot in python I would love to help

标签: pythontelegramtelegram-bot

解决方案


@self.__bot.message_handler(content_types=['photo'])
def on_photo(message):
  file_id = message.photo[-1].file_id
  file_info = self.__bot.get_file(file_id)
  file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(API_TOKEN, file_info.file_path))
  with open('photo.jpg','wb') as f:
    f.write(file.content)

    

推荐阅读