D
D
Doniyor Mamatkulov2021-05-09 10:53:55
Python
Doniyor Mamatkulov, 2021-05-09 10:53:55

How to get photo from user via pyTelegramBotAPI?

Hello.
I'm trying to achieve this logic from the Telegram bot (I use the desktop version of the client):

  1. The bot writes a request to send a photo
  2. The user sends a photo (both with grouping and without grouping)
  3. The bot receives all photos sent by the user
  4. Only after taking all the photos, the bot sends a message about the successful operation and passes control to the next function.


My code:
import telebot

bot_token = '1279****77:AAHJDPFJ************KJfLbiI'
bot = telebot.TeleBot(bot_token, parse_mode='html')
photo_list = []

@bot.message_handler(commands=['start'])
def send_welcome(message):
    send = bot.send_message(message.from_user.id, 'Send your pics...')
    bot.register_next_step_handler(send, get_user_pics)
    return

@bot.message_handler(content_types=['photo'])
def get_user_pics(message):
    if message.photo[-1].file_id not in photo_list:
        photo_list.append(message.photo[-1].file_id)
        if len(photo_list) == 1:
            send = bot.send_message(message.from_user.id, "Photos received...")
            bot.register_next_step_handler(send, process_messages())
            return

def process_messages():
    print(photo_list)
    return

bot.polling()


Now, the logic only works if the user poisons images with grouping.
If you send without grouping (~10 photos), then after about the 4th photo, the bot writes that it has successfully received the photo and then nothing ...
So, how to achieve the required logic, regardless of whether the photos are grouped or not?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fedor Doronin, 2021-05-09
@Fedor11

According to the idea with this code, the bot will exit after the first photo....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question