(Last Updated: October 14, 2020)

HTTP 400 error with Telegram



  • Hi all. Looking for some guidance as ever

    This

    luup.inet.wget('https://api.telegram.org/bot1225075966:AAHS5rwhCpOx0hwq3mOnNjtCSKiDeAS6B4/sendMessage?chat_id=@Coalport58&text=House mode is home')
    

    Works fine

    This

    luup.inet.wget('https://api.telegram.org/bot1225075966:AAHS5rwhCpOx0hwq3mOnNjtCSKiDeAS6B4/sendMessage?chat_id=@Coalport58&text=House mode is Home')
    

    Throws a 400 error:

    openLuup.client:: WGET error status: 400
    

    I have munged the bot key to post here, but just changing the 'h' to 'H' breaks it. Any ideas why?

    C



  • To other readers, I think CatmanV2 is talking about that changing the word "home" to "Home" in the Telegram message is causing problems?

    While this may not be the answer, I would suggest you at least URL encode your message before sending it off:

    local function urlEncode(str)
        if (str) then
            str = string.gsub (str, "\n", "\r\n")
            str = string.gsub (str, "([^%w %-%_%.%~])",
                function (c) return string.format ("%%%02X", string.byte(c)) end)
            str = string.gsub (str, " ", "+")
        end
        return str
    end
    
    -- eg URL encode msg
    msg = urlEncode(msg)
    


  • There also a url 'encode' function in the socket library

    local url = require "socket.url"
    local encoded = url.escape(msg)
    


  • Nice to know.



  • I usually get this error when using the internal lua libs, while curl is ok.

    It seems lua is sending an utf7 encoded url, instead of utf8 and sometime telegram doesn’t like it.



  • Thanks. Yes, just changing "home" to "Home" breaks it

    No idea how to use the URL encoding :). I try to keep things simple!

    C




Log in to reply