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

Telegram CLI控制树莓派3播放视频Lua脚本报错求助

问题分析与解决方案

你遇到的lua:: attempt to call a nil value错误和视频不播放问题,都是Lua脚本里的语法错误、逻辑漏洞,加上脚本配置细节问题导致的,我们一步步来修复:


1. 先解决Lua的nil调用错误

看你actions.lua里的这段代码:

send_msg (msg.from.print_name, 'Okay! This might take a second..', ok_c$, ok_cb, false)

这里的ok_c$是明显的拼写错误,应该是ok_cb(如果不需要回调逻辑,也可以直接传nil)。这个未定义的变量就是触发attempt to call a nil value错误的直接原因。


2. 修正视频播放命令的构造逻辑

你的命令拼接方式存在两个问题:

  • 用[[]]包裹的长字符串里,Lua的拼接运算符..会被当成普通字符,根本起不到拼接作用
  • 相对路径../ytplayvlc可能因Telegram CLI的运行目录导致找不到脚本,建议改用绝对路径

修正后的命令构造代码:

local link = string.match(message, "^PLAY-(.*)$") -- 用^和$确保只匹配以PLAY-开头的完整消息
if link then -- 先判断是否匹配到有效链接,避免空值报错
    send_msg(msg.from.print_name, 'Okay! This might take a second..', ok_cb, false)
    -- 把../ytplayvlc替换成你的脚本绝对路径,比如/home/pi/ytplayvlc
    local command = "../ytplayvlc '" .. link .. "' 18"
    -- 添加调试输出,方便排查问题
    print("Executing command: " .. command)
    local exit_code = os.execute(command)
    print("Command exit status: " .. exit_code)
end

3. 确保ytplayvlc脚本本身可用

你脚本的shebang写错了,当前的#!bin/bash应该改成#!/bin/bash(缺少开头的/),这会导致脚本无法被系统识别执行。另外还要给脚本添加执行权限:

chmod +x ytplayvlc

先单独测试脚本是否能正常运行:

./ytplayvlc "https://www.youtube.com/watch?v=你的视频ID" 18

确认能播放后再整合到Telegram Bot里。


4. 处理无桌面环境的播放问题

如果你的树莓派是通过SSH远程运行、没有桌面环境,VLC需要额外配置才能播放,修改ytplayvlc里的命令:

youtube-dl -U -f $2 -o - "$1" | vlc --no-xlib -

修正后的完整actions.lua

function on_msg_receive (msg)
    if msg.out then return end

    -- 处理Ping-Pong逻辑
    if msg.text == 'ping' then
        send_msg(msg.from.print_name, 'pong', ok_cb, false)
        return
    end

    -- 处理PLAY命令逻辑
    local message = msg.text
    local link = string.match(message, "^PLAY-(.*)$")
    if link then
        send_msg(msg.from.print_name, 'Okay! This might take a second..', ok_cb, false)
        -- 建议替换成ytplayvlc的绝对路径
        local command = "../ytplayvlc '" .. link .. "' 18"
        -- 调试输出,方便排查命令执行问题
        print("Running command: " .. command)
        local result = os.execute(command)
        print("Command result: " .. tostring(result))
    end
end

-- 以下空函数保持原样即可
function on_our_id(id) end
function on_secret_chat_update(user, what_changed) end
function on_user_update(user_what_changed) end
function on_chat_update(user, what_changed) end
function on_get_difference_end() end
function on_binlog_replay_end() end

调试建议

  1. 运行Telegram CLI时不要后台运行,保持终端打开,这样能看到print输出的调试信息,确认命令是否正确执行
  2. 如果命令执行失败,检查脚本路径、权限,以及youtube-dl是否正常(可以执行sudo youtube-dl -U更新到最新版本)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:52:59