Page 1 of 1

telegram http-response, gives error when using it

Posted: Thursday 07 November 2019 21:30
by hoeby
I want to make something with the message_id from telegram.
I get the value out of the answer. But when using it, the script gives me a error.

This one

Code: Select all

2019-11-07 21:22:38.066 Status: dzVents: Error (2.4.19): ...ticz/scripts/dzVents/generated_scripts/test telegram.lua:16: attempt to call local 'onstate' (a number value)
I know why it gives it. Because it sees it as number value and not as plain text.
But don't know how to solve. Somebody has an idea?

The script which gives this error

Code: Select all

return {
    on = { 
       devices         = {'Trigger device'},
       httpResponses   = { "Telegram_Callback"}
         },

    execute = function(dz, item)
        
    local teleTok   = '12345:0987654321'
    local chatId    = '123456789'
            
        if item.isHTTPResponse then
            if (item.ok) then
                local onstate   = item.json.result.message_id
                print (onstate)
                os.execute('curl -s -X POST "https://api.telegram.org/'..teleTok..'/deleteMessage?chat_id='..chatId..'&message_id='..onstate'"').afterSec(10)
            else
                print ('niet goed')
            end
        else
            dz.openURL(
                {
                url         = "https://api.telegram.org/bot12345:0987654321/sendMessage?chat_id=123456789&text=Isstil",
                method      = "GET",
                callback    = "Telegram_Callback", 
                })
        end
    end
}


Re: telegram http-response, gives error when using it

Posted: Thursday 07 November 2019 22:54
by waaren
hoeby wrote: Thursday 07 November 2019 21:30

Code: Select all

2019-11-07 21:22:38.066 Status: dzVents: Error (2.4.19): ...ticz/scripts/dzVents/generated_scripts/test telegram.lua:16: attempt to call local 'onstate' (a number value)
I know why it gives it. Because it sees it as number value and not as plain text.
Not really...
afterSec is not available for os.execute and you missed a ..

Can you try again after changing line 16 to

Code: Select all

os.execute('curl -s -X POST "https://api.telegram.org/'..teleTok..'/deleteMessage?chat_id='..chatId..'&message_id='.. onstate .. '"')