1

I want to send the automatic reply messages in Telegram-CLI using modified lua script as below:

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

When I ran script above, if I got a message "Hi there!", the script will wait 1 second, then it will send reply with "Hello" message.

The script works fine when I set only one reply message. However, when I modified the script to add another reply message as below, the result is not as what I expected.

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

What I expect from the modified script is, when I received "Hi there!" message, the script will wait 1 second, then send "Hello" message, wait another 3 seconds, finally send "World!" message.

What actual happended is the script will wait 3 seconds, then send both "Hello" and "World!" at the same time.

Does anyone has any clues about this? thanks in advance

wakhaiha
  • 55
  • 1
  • 6

2 Answers2

0

@wakhaiha

You only have to edit the on_msg_receive function:

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

If you're getting an error message from the Lua script, saying something like

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

you have to comment out or delete everything within Notification code{{{ .

You can expand the commands above, just edit the Lua file and the python file (it is easy now to reply with "Hi there" when the users sent a message with "Hi" as content:

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: source

Also, make sure that you added the contact with add_contact in order to receive messages from it. You can start telegram-cli with the Lua script by typing:

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

Install screen package before.

j3141592653589793238
  • 1,810
  • 2
  • 16
  • 38
0

The problem is that the command send_msg was collected in the function on_msg_receive. To solve this use a boolean var and the function cron. Add the sececond send_msg in the cron function with a boolean var.

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

function cron()
.
.
If blnSendMsgdMsg then
  send_msg .,
blnSendMsgdMsg = false
end