如何解决电报机器人无法在一条消息上收到多张照片
我想设置一个电报机器人,以在一条消息中接收多张图像。我的机器人只能接收第一个图像,所有其他图像都将被忽略。这是我的代码:
# -*- coding: utf-8 -*-
import config
import telebot
from telebot.types import InlineKeyboardMarkup,InlineKeyboardButton,InputMediaPhoto
bot = telebot.TeleBot(config.token)
main_menu_keyboard = telebot.types.ReplyKeyboardMarkup(True,True)
main_menu_keyboard.add('test1','test2')
welcome_message='''Hello,dude
'''
dict = {}
class nameDict:
def __init__(self,name):
self.name = name
self.media = None
def process_name_step(message):
try:
chat_id = message.chat.id
name = message.text
upload = nameDict(name)
dict[chat_id] = upload
message = bot.send_message(chat_id,'Upload your photos: ')
bot.register_next_step_handler(message,process_media_step)
except Exception as e:
bot.reply_to(message,'Error...')
def process_media_step(message):
try:
chat_id = message.chat.id
media = message.photo[-1].file_id
upload = dict[chat_id]
upload.media = media
bot.send_photo(chat_id,str(upload.media))
except Exception as e:
bot.reply_to(message,'Error...')
bot.register_next_step_handler(message,process_media_step)
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 youe name: ')
bot.register_next_step_handler(message,process_name_step)
elif message.text.lower() == 'test2':
bot.send_message(message.chat.id,'Empty')
bot.polling()
请问我能找到一种解决方案,该解决方案如何在向机器人发送的单个消息中接收多个图像? 谢谢!
解决方法
我发现一种解决方法只是添加一个照片处理程序,将其保存在本地。
@bot.message_handler(content_types=['photo'])
def get_photo(message):
fileID = message.photo[-1].file_id
file_info = bot.get_file(fileID)
downloaded_file = bot.download_file(file_info.file_path)
user_path=str(message.from_user.id)
if not os.path.exists(user_path):
os.makedirs(user_path)
with open(str(message.from_user.id) + '/' + fileID,'wb') as new_file:
new_file.write(downloaded_file)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。