Answer the question
In order to leave comments, you need to log in
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
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)]
@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)])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question