Lua脚本 - 使用Telegram与Raspberry Pi进行通信。 if,elseif,else statment在特定情况下错误地击中了其他

时间:2016-02-29 00:01:34

标签: lua raspberry-pi telegram

我正在扩展以下示例,使用Telegram与我的Raspberry Pi进行通信:

http://www.instructables.com/id/Raspberry-remote-control-with-Telegram/

我使用以下代码:

#!/usr/local/bin/lua
function on_msg_receive (msg)
    if msg.out then
        return
    end
    if (msg.text=='Ping') then
        send_msg (msg.from.print_name, 'pong', ok_cb, false)
    elseif (msg.text=='Photo') then
        os.execute[["scriptToTakePhoto]]
        send_photo (msg.from.print_name, '/pathToPhoto.jpg', ok_cb, false)
        send_msg (msg.from.print_name, 'Here you go!', ok_cb, false)
    elseif (msg.text=='Hello'or msg.text =='Hi') then
        send_msg (msg.from.print_name, 'Hi, how can I help you?', ok_cb, false)
    else
        math.randomseed(os.time())
        randNum = math.random(0,5)
        if randNum == 0 then
            messageText = "Random Text"
        elseif randNum == 1 then
            messageText = "Random Text"
        elseif randNum == 2 then
            messageText = "Random Text"
        elseif randNum == 3 then
            messageText = "Random Text"
        elseif randNum == 4 then
            messageText = "Random Text"
        elseif randNum == 5 then
            messageText = "Random Text"
        end
        send_msg (msg.from.print_name, messageText, ok_cb, false)
    end
end

我得到以下输出:

刘炳章>傍

嗨 - >嗨,我该怎么帮你?

您好 - >您好,我该如何帮助您?

[其他] - >随机文字

照片 - >在这里你去! [收到的照片] 随机文字

因此,当我在Photo之外的if / ifelse中发送任何消息时,最后的其他消息都没有到达。当我发送' Photo'时,它会进入ifelse以及最后一个。无法弄清楚原因。

0 个答案:

没有答案