0

I am expanding on the following example to communicate with my Raspberry Pi using Telegram:

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

I use the following bit of code:

#!/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

I get the following output:

Ping->Pong

Hi->Hi, how can I help you?

Hello->Hi, how can I help you?

[Anything else]->Random Text

Photo->Here you go! [photo is recieved] Random Text

So when I send any of the message within an if/ifelse other than Photo, the last else isn't reached. When I send 'Photo', it goes into that ifelse as well as the last else. Can't figure out why.

Langleson
  • 103
  • 1
  • 13
  • No need to call `math.randomseed` before `math.random`. See http://stackoverflow.com/a/35455929/107090. – lhf Feb 29 '16 at 00:05
  • Interesting. When I tested it in a basic example that generated a random number then printed it. It gave me the same number every time I called the script until I added randomseed. But you are correct, in this case I removed it and it works fine. – Langleson Feb 29 '16 at 00:13
  • 1
    FYI this line does not seem like it would work. Maybe something else is off around there too in your real version? `os.execute[["scriptToTakePhoto]]` – Adam B Mar 01 '16 at 04:10
  • It looks like I accidentally deleted the closing double quote and technically it's "pathToTakePhoto.sh" The '[[' syntax was required because there was a space in my pathname. I learned a valuable lesson about spaces in pathnames. – Langleson Mar 02 '16 at 11:15

0 Answers0