我来帮你再增加对X(Twitter)的短视频下载支持。由于X的视频下载需要处理其特定的API或网页结构,我将使用 `yt-dlp`(youtube-dl的增强版)来实现,因为它支持X平台,并且比原生的Twitter API更简单直接。

以下是添加X支持后的完整代码:
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import yt_dlp # 替换youtube_dl为yt-dlp,支持更多平台
from instaloader import Instaloader
import requests
import os
from TikTokApi import TikTokApi

# Telegram Bot Token (从BotFather获取)
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN'

# 初始化Telegram Bot
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher

# 下载YouTube视频
def download_youtube(url):
try:
ydl_opts = {
'format': 'best',
'outtmpl': 'downloads/%(title)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
return filename
except Exception as e:
return str(e)

# 下载Instagram视频
def download_instagram(url):
try:
loader = Instaloader()
post = loader.check_profile_id(url.split('/')[-2])
loader.download_post(post, target='downloads')
for file in os.listdir('downloads'):
if file.endswith('.mp4'):
return os.path.join('downloads', file)
return "No video found"
except Exception as e:
return str(e)

# 下载Facebook视频
def download_facebook(url):
try:
# 使用yt-dlp替代简单请求
ydl_opts = {
'format': 'best',
'outtmpl': 'downloads/facebook_%(id)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
return filename
except Exception as e:
return str(e)

# 下载TikTok视频
def download_tiktok(url):
try:
api = TikTokApi()
video_data = api.get_Video_By_Url(url)
filename = 'downloads/tiktok_video.mp4'
with open(filename, 'wb') as f:
f.write(video_data)
return filename
except Exception as e:
return str(e)

# 下载X (Twitter) 视频
def download_x(url):
try:
ydl_opts = {
'format': 'best',
'outtmpl': 'downloads/x_%(id)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
return filename
except Exception as e:
return str(e)

# 处理/start命令
def start(update, context):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="欢迎使用视频下载Bot!\n请发送YouTube、Facebook、Instagram、TikTok或X视频链接给我。"
)

# 处理接收到的消息
def handle_message(update, context):
url = update.message.text
chat_id = update.effective_chat.id

context.bot.send_message(chat_id=chat_id, text="正在处理你的请求...")

# 判断URL类型并下载
if 'youtube.com' in url or 'youtu.be' in url:
result = download_youtube(url)
elif 'instagram.com' in url:
result = download_instagram(url)
elif 'facebook.com' in url:
result = download_facebook(url)
elif 'tiktok.com' in url:
result = download_tiktok(url)
elif 'twitter.com' in url or 'x.com' in url:
result = download_x(url)
else:
context.bot.send_message(chat_id=chat_id, text="不支持的链接格式")
return

# 检查下载结果并发送文件
if os.path.exists(result):
with open(result, 'rb') as video:
context.bot.send_video(chat_id=chat_id, video=video)
os.remove(result) # 删除临时文件
else:
context.bot.send_message(chat_id=chat_id, text=f"下载失败: {result}")

# 设置命令处理器
start_handler = CommandHandler('start', start)
message_handler = MessageHandler(Filters.text & ~Filters.command, handle_message)

dispatcher.add_handler(start_handler)
dispatcher.add_handler(message_handler)

# 创建下载目录
if not os.path.exists('downloads'):
os.makedirs('downloads')

# 启动Bot
updater.start_polling()
print("Bot已启动...")
updater.idle()

### 添加X支持的说明:

1. **替换youtube_dl为yt-dlp**: