D
D
Dmitry2021-01-16 04:49:59
Python
Dmitry, 2021-01-16 04:49:59

How to send the user a photo from a folder in the project when clicking on the Inline button?

Good afternoon! I have such a code - please tell me how to implement so that when you click on the Inline button: get a photo - the bot sends a random photo from the project folder to the user?
python - telegrambotapi

here is my code:

@bot.message_handler(commands=['photo'])
def keyboard():
    start_keyboard = types.InlineKeyboardMarkup()
    menu = types.InlineKeyboardButton(text="фото", callback_data='men')
    start_keyboard.add(menu)
    bot.send_message(message.chat.id, "получить фото", reply_markup=start_keyboard)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alekssamos, 2021-01-16
@pagedimka

Get

a random photo from the project folder ?
maybe so
import glob, os, random
files = []
for ext in ["png", "jpg", "jpeg"]:
  [files.append(file) for file in glob.glob(f"*.{ext}")]

random_file = files[random.randint(0, len(files)-1)]

You can send it like this:
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
  if call.data == 'men':
    with open(random_file, 'rb') as f:
      bot.send_media_group(call.message.chat.id, [InputMediaPhoto(f)])

S
SashaN69, 2021-01-16
@SashaN69

Make the name of the photo a number, say 1.jpg 2.jpg. Then randomly select a number and add the file extension, then send the photo to the user. Or you can store the name of the photo in the database, and randomly select the entry.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question