You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram机器人能否用内置播放器直接播放外链视频?求Python示例

Telegram Bot 直接播放服务器外链视频方案及Python实现

核心要求

要让Telegram内置播放器直接播放你的服务器视频,必须满足以下前提:

  • 服务器视频链接支持HTTP Range请求(Telegram播放器依赖断点续传实现流式加载):可通过curl -I -H "Range: bytes=0-100" https://your-server/video.mp4验证,返回206 Partial Content即符合要求。
  • 视频格式为Telegram支持的类型:MP4、MKV、AVI等主流格式均可。
  • 视频链接需公开可访问(若为私有链接,需确保Telegram服务器能通过防火墙/权限验证访问)。

Python 实现示例

以下使用python-telegram-bot库提供两种版本实现(适配不同版本的库):

版本1:python-telegram-bot v13.x(同步模式,新手友好)

  1. 安装依赖
pip install python-telegram-bot==13.7
  1. 完整代码
from telegram import Bot, InputMediaVideo
from telegram.ext import Updater, CommandHandler
import logging

# 基础日志配置
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

# 替换为你的Bot Token(从@BotFather获取)
BOT_TOKEN = "YOUR_BOT_TOKEN_HERE"

def start(update, context):
    update.message.reply_text('发送 /play 命令即可加载视频!')

def play_video(update, context):
    # 替换为你的服务器视频外链
    video_url = "https://your-server-domain/path/to/your/video.mp4"
    
    try:
        # 通过send_media_group发送流媒体视频
        context.bot.send_media_group(
            chat_id=update.effective_chat.id,
            media=[InputMediaVideo(media=video_url)]
        )
        update.message.reply_text('视频已加载,点击即可播放!')
    except Exception as e:
        logger.error(f"发送视频失败: {str(e)}")
        update.message.reply_text('播放失败,请检查视频链接是否符合要求!')

def main():
    updater = Updater(BOT_TOKEN, use_context=True)
    dp = updater.dispatcher
    
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("play", play_video))
    
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

版本2:python-telegram-bot v20.x(异步模式,最新版本)

  1. 安装依赖
pip install python-telegram-bot==20.7
  1. 完整代码
from telegram import InputMediaVideo
from telegram.ext import ApplicationBuilder, CommandHandler
import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

BOT_TOKEN = "YOUR_BOT_TOKEN_HERE"

async def start(update, context):
    await update.message.reply_text('发送 /play 命令即可加载视频!')

async def play_video(update, context):
    video_url = "https://your-server-domain/path/to/your/video.mp4"
    try:
        await context.bot.send_media_group(
            chat_id=update.effective_chat.id,
            media=[InputMediaVideo(media=video_url)]
        )
        await update.message.reply_text('视频已加载,点击即可播放!')
    except Exception as e:
        logger.error(f"发送视频失败: {str(e)}")
        await update.message.reply_text('播放失败,请检查视频链接是否符合要求!')

def main():
    application = ApplicationBuilder().token(BOT_TOKEN).build()
    
    application.add_handler(CommandHandler("start", start))
    application.add_handler(CommandHandler("play", play_video))
    
    application.run_polling()

if __name__ == '__main__':
    main()

常见问题排查

  • 显示“无法播放视频”:
    1. 检查视频链接是否能在浏览器正常打开;
    2. 验证链接是否支持Range请求;
    3. 确认视频格式为Telegram兼容类型;
    4. 检查服务器防火墙是否允许Telegram IP段访问。

内容的提问来源于stack exchange,提问作者kGummyBear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:15:59