2017-02-07 22 views
1

我想用如下改性LUA脚本在电报-CLI来发送自动回复消息:在电报-CLI发送多个响应消息(LUA脚本)

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, 'Hi there!')) then 
     wait(1) 
     send_msg (msg.from.print_name, 'Hello', ok_cb, false) 
    else 
     --do nothing 
    end 
end 

当我跑上面的脚本,如果我一条消息“Hi there!”,脚本将等待1秒钟,然后它会发送带有“Hello”消息的回复。

当我仅设置一条回复消息时,该脚本正常工作。但是,当我修改脚本以添加下面的其他回复消息时,结果不符合我的预期。

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, 'Hi there!')) then 
     wait(1) 
     send_msg (msg.from.print_name, 'Hello', ok_cb, false) 
     wait(3)            --new command 
     send_msg (msg.from.print_name, 'World!', ok_cb, false) --new command 
    else 
     --do nothing 
    end 
end 

当我收到“Hi there!”时,我期望修改后的脚本是:消息,脚本会等待1秒,然后发送“Hello”消息,再等3秒钟,最后发送“World!”。信息。

实际发生的事情是剧本将等待3秒,然后发送“你好”和“世界!”与此同时。

有没有人有任何线索?在此先感谢

回答

0

@wakhaiha

您只需编辑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文件(这是现在很容易与“你好”的时候,用户发送一条消息,“你好”要回复内容:

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 

)。 来源:source

此外,请确保您添加与add_contact的联系人,以便从它接收消息。 您可以通过键入启动电报CLI与Lua中:

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

之前安装屏幕包。