Как использовать параметр миниатюры в send_document pytelegrambotapiPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как использовать параметр миниатюры в send_document pytelegrambotapi

Сообщение Anonymous »

Я не программист, но мне удалось создать бота для Telegram на Python, и он работает хорошо, за исключением параметра миниатюры: бот просит пользователя отправить изображение, чтобы установить его в качестве миниатюры для документа, затем бот отправляет документ вернулся пользователю с предварительным просмотром миниатюрного изображения, но он не изменился.
Вот мой сценарий:

Код: Выделить всё

import telebot
from telebot import types
from decouple import config
import os

BOT_TOKEN = config('BOT_TOKEN')
bot = telebot.TeleBot(BOT_TOKEN)

# Define the handle_new_name function outside of the welcome function
def handle_new_name(message):
# Your existing logic for handle_new_name
pass

@bot.message_handler(commands=["start", "help"])
def welcome(message):
markup = types.InlineKeyboardMarkup()
change_name_button = types.InlineKeyboardButton("Change Name", callback_data="change_name")
markup.add(change_name_button)
bot.send_message(message.chat.id, "Welcome!", reply_markup=markup)

@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
if call.data == "change_name":
# Step 1: Ask the user to send a file
bot.send_message(call.message.chat.id, "Please send a file to change its name.")
bot.register_next_step_handler(call.message, get_new_name)

def get_new_name(message):
# Step 2: Handle the received file
if message.document:
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)

original_file_name = message.document.file_name
file_extension = os.path.splitext(original_file_name)[-1]
new_file_name = f"new_file{file_extension}"

with open(new_file_name, 'wb') as new_file:
new_file.write(downloaded_file)

# Step 3: Ask the user for the new name
bot.send_message(message.chat.id, "Please enter the desired new name for the file:")

# Step 4: Handle the new name
@bot.message_handler(func=lambda msg: True)
def handle_new_name(msg):
new_name = msg.text.strip()  # Get the user's input
os.rename(new_file_name, new_name + file_extension)

# Step 5: Ask the user to send an image for the thumbnail
bot.send_message(msg.chat.id, "Now, please send an image to use as the thumbnail:")

# Step 6: Handle the received thumbnail image
@bot.message_handler(content_types=['photo'])
def handle_thumbnail_photo(thumbnail_message):
thumbnail_file_id = thumbnail_message.photo[-1].file_id
with open(new_name + file_extension, 'rb') as renamed_file:
# Send the renamed file back to the user with the thumbnail
bot.send_document(msg.chat.id, renamed_file, caption=f"Renamed File: {new_name}", thumbnail=thumbnail_file_id)

# Note: No need to remove_listener; the handlers will be called once
# when the user provides the new name and thumbnail.

# Your existing handlers and bot.polling() below

bot.polling()

Любая помощь приветствуется.
Спасибо!

Подробнее здесь: https://stackoverflow.com/questions/785 ... grambotapi
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»