2017-02-07 21 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 

之前安裝屏幕包。