I am not a coder, but i managed to create a telegram bot in python and it’s working well, except the thumbnail parameter, the bot ask user to send an image to set as thumbnail for the document, then the bot send the document back to user with thumbnail image preview but it didn’t change.
Here is my script:
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()
Any help appreciated.
Thanks!
New contributor
ali90i is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.