在telegram-cli中发送多条响应消息(使用Lua脚本)

我想使用以下修改后的Lua脚本,在Telegram-CLI中发送自动回复消息:

function ok_cb(extra, success, result)
end

function wait(seconds)
    local start = os.time()
    repeat until os.time() > start + seconds
end

function on_msg_receive (msg)
    if msg.out then
        return
    end
    if (string.find(msg.text, '你好')) then
        wait(1)
        send_msg (msg.from.print_name, '你好啊', ok_cb, false)
    else
        --do nothing
    end
end

当我运行上面的脚本时,如果我收到一个消息“你好”,该脚本将等待1秒钟,然后发送“你好啊”回复消息。

当我修改脚本以添加另一个回复消息时,结果与我预期的不同。

function ok_cb(extra, success, result)
end

function wait(seconds)
    local start = os.time()
    repeat until os.time() > start + seconds
end

function on_msg_receive (msg)
    if msg.out then
        return
    end
    if (string.find(msg.text, '你好')) then
        wait(1)
        send_msg (msg.from.print_name, '你好啊', ok_cb, false)
        wait(3)                                                --new command
        send_msg (msg.from.print_name, '世界!', ok_cb, false) --new command
    else
        --do nothing
    end
end

我期望修改后的脚本在收到“你好”消息时,将等待1秒钟,然后发送“你好啊”消息,再等待3秒钟,最后发送“世界!”消息。

现实发生的是,该脚本将等待3秒钟,然后同时发送“你好啊”和“世界!”消息。

有没有人有关于这个问题的线索?提前谢谢。

点赞
用户7973906
用户7973906

你只需编辑 on_msg_receive 函数:

function on_msg_receive(msg)
    if started == 0 then
        return
    end
    if msg.out then
        return
    end

    if msg.text then
         mark_read(msg.from.print_name, ok_cb, false)
    end

    -- Optional: Only allow messages from one number
    if msg.from.print_name ~= 'Prename_surname' then
        os.execute('*path_to_your_send_script*' ..msg.from.print_name.." 'Not allowed'")
        return
    end
    if (string.lower(msg.text) == 'uptime') then
        local handle = io.popen("sudo python *path_to_your_python* uptime")
        local res = handle:read("*a")
        handle:close()
        os.execute("*path_to_your_send_script* "..msg.from.print_name.." '"..res.."' ")
        return
    end

如果 Lua 脚本出现以下错误消息:

namespace.lua:149: Typelib file for namespace 'Notify' (any version) not found

那么您需要注释或删除“Notification code{{{"中的所有内容。

您可以扩展上面的命令,只需编辑 Lua 文件和 Python 文件即可(现在当用户发送内容为“Hi”时,轻松回复“嗨,怎么了?”):

if (string.lower(msg.text) == 'hi there') then
    os.execute('*path_to_your_send_script*' ..msg.from.print_name.." 'Hey, what's up?'")
    return
end

来源:

此外,请确保通过 add_contact 添加联系人以接收来自其的消息。

您可以通过输入以下命令在 Lua 脚本中启动 telegram-cli:

screen -dmS TelegramCLI ~/tg/bin/telegram-cli -s ~/tg/test.lua

在此之前,请安装 screen 包。

2017-08-13 09:31:47
用户15246322
用户15246322

问题在于 send_msg 命令被包含在 on_msg_receive 函数中。要解决这个问题,使用布尔变量和函数 cron。在 cron 函数中增加第二个 send_msg,并使用布尔变量。

function on_msg_receive(msg)
.
.
blnSendMsgdMsg = true
.
.

function cron()
.
.
if blnSendMsgdMsg then
  send_msg .,
blnSendMsgdMsg = false
end
2021-02-20 02:39:03