I want to build a telegram botSaying start in this code creates a two private telegram channel that only you can access and tells the userDisplay a gui called upload or openDo not send the private cha
Here is a basic code outline for a Telegram bot that implements the functionality you described:
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Define the private channels
private_channel1 = 'channel1'
private_channel2 = 'channel2'
# Define a dictionary to store the names of uploaded files
file_names = {}
# Define a function to handle the /start command
def start(update, context):
# Create the private channels
context.bot.create_chat(private_channel1, is_private=True)
context.bot.create_chat(private_channel2, is_private=True)
# Send the user a message and display the GUI
update.message.reply_text('Welcome! Please choose an option:')
# TODO: Display the GUI with buttons for 'Upload' and 'Open'
# Define a function to handle file uploads
def upload_file(update, context):
# Check if a file was sent
if update.message.document:
# Save the file to a temporary location
file = update.message.document.get_file()
file.download('temp_file')
# Ask the user for a name for the file
update.message.reply_text('Please enter a name for this file:')
# Store the file ID in a dictionary with an empty name
file_names[file.file_id] = ''
# Define a function to handle file name inputs
def name_file(update, context):
# Get the name from the user
name = update.message.text
# Get the file ID from the message
file_id = update.message.reply_to_message.document.file_id
# Store the name in the dictionary
file_names[file_id] = name
# Send the file to the private channel1 with the name
context.bot.send_document(private_channel1, file_id, caption=name)
# Send the name to the private channel2
context.bot.send_message(private_channel2, name)
# Reply to the user and ask if they want to upload another file
update.message.reply_text('File uploaded! Do you want to upload another file?')
# Define a function to handle the 'Open' button
def open_file(update, context):
# Get the list of file names from the dictionary
names = list(file_names.values())
# Send the list of names to the user through the GUI
# TODO: Use the context parameter to send the names to the GUI
# Define a function to handle the 'End' button
def end_upload(update, context):
# Reply to the user and thank them for uploading files
update.message.reply_text('Thank you for uploading files!')
# Create the Telegram bot and add the handlers
bot_token = 'your_bot_token_here'
updater = Updater(bot_token, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(MessageHandler(Filters.document, upload_file))
updater.dispatcher.add_handler(MessageHandler(Filters.reply & Filters.text, name_file))
updater.dispatcher.add_handler(CommandHandler('open', open_file))
updater.dispatcher.add_handler(CommandHandler('end', end_upload))
# Start the bot
updater.start_polling()
updater.idle()
Note that this is just a basic outline and you will need to fill in some details, such as how to display the GUI and send the file contents to the user through the GUI. Also, this code assumes that the user will always enter a valid name for each file, so you may want to add some error handling if necessary
原文地址: http://www.cveoy.top/t/topic/ebSI 著作权归作者所有。请勿转载和采集!