Aller au contenu

Messages recommandés

Posté(e) (modifié)

Telegram HC3

 

 

Sur Quick APP de de la HC3

origine https://forum.fibaro.com/topic/52109-lua-telegram-change-hc2-to-hc3-where-am-i-going-wrong/

 

---change ton BOT KEY

local BOT_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==

function QuickApp:onInit()
    self:debug("onInit")
end

function dump(o)
    if type(o) == 'table' then
       local s = '{ '
       for k,v in pairs(o) do
          if type(k) ~= 'number' then k = '"'..k..'"' end
          s = s .. '['..k..'] = ' .. dump(v) .. ','
       end
       return s .. '} '
    else
       return tostring(o)
    end
 end

function QuickApp:sendPhoto(fileName, image, chat_id)
    function base64dec(data)
        local b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" -- You will need this for encoding/decoding
        data = string.gsub(data, "[^" .. b .. "=]", "")
        return (data:gsub(
            ".",
            function(x)
                if (x == "=") then
                    return ""
                end
                local r, f = "", (b:find(x) - 1)
                for i = 6, 1, -1 do
                    r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0")
                end
                return r
            end
        ):gsub(
            "%d%d%d?%d?%d?%d?%d?%d?",
            function(x)
                if (#x ~= 8) then
                    return ""
                end
                local c = 0
                for i = 1, 8 do
                    c = c + (x:sub(i, i) == "1" and 2 ^ (8 - i) or 0)
                end
                return string.char(c)
            end
        ))
    end

    function getFileExtension(fileName)
        return fileName:match("^.+(%..+)$")
    end

    if tonumber(chat_id) == nil then
        self:error("chat_id is null or empty!")
        return
    end

    if not fileName or fileName and fileName == "" then
        self:error("fileName of picture is null or empty!")
        return
    end

    if not image or image and image == "" then
        self:error("image message is null or empty!")
        return
    end

    local img = base64dec(image)
    local file_ext = getFileExtension(fileName)
    if string.lower(file_ext) == ".png" then
        file_ext = "png"
    else
        -- as JPG
        file_ext = "jpeg"
    end

    -- image/jpeg	.jpg, .jpeg, .jfif, .pjpeg, .pjp
    -- image/png	.png

    print("Sending photo to ", chat_id)

    local headers = {
        ["Accept"] = "application/json",
        ["Cache-Control"] = "no-cache"
    }

    headers["Content-Type"] = "multipart/form-data; boundary=----WebKitFormBoundaryX3bY6PBMcxB1vCan"
    local bodyhead = ""

    bodyhead = bodyhead .. '------WebKitFormBoundaryX3bY6PBMcxB1vCan'
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. "Content-Disposition: form-data; name=\"chat_id\"" .. "\r\n" .. "\r\n"
    bodyhead = bodyhead .. chat_id
    bodyhead = bodyhead .. '\r\n'

    bodyhead = bodyhead .. '------WebKitFormBoundaryX3bY6PBMcxB1vCan'
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. [[Content-Disposition: form-data; name="photo"; filename=fileName]]
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. 'Content-Type: image/'..file_ext
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. img
    bodyhead = bodyhead .. '\r\n'
    bodyhead = bodyhead .. '------WebKitFormBoundaryX3bY6PBMcxB1vCan--'
    local body   = bodyhead

    local url = "https://api.telegram.org/bot".. BOT_KEY .."/sendPhoto"
    local http = net.HTTPClient()
    http:request(
        url,
        {
            options = {
                method = "POST",
                headers = headers,
                data = body
            },
            success = function(response)
                if (response.status >= 200) then
                    if func then
                        func(json.decode(response.data))
                    else
                        print(response.data)
                    end
                else
                    self:error("Wrong status '" .. response.status .. "' in response!")
                end
            end,
            error = function(message)
                self:error("Connection error: " .. message)
            end
        }
    )
end

function QuickApp:sendMessage(text, chat_id)
    if tonumber(chat_id) == nil then
        self:error("chat_id is null or empty!")
        return
    end

    if not text or text and text == "" then
        self:error("text message is null or empty!")
        return
    end

    local body = {chat_id = chat_id, text = text}

    local headers = {
        ["Accept"] = "application/json",
        ["Content-Type"] = 'application/json',
        ["Cache-Control"] = "no-cache"
    }

    local url = "https://api.telegram.org/bot" .. BOT_KEY .. "/sendMessage"
    local http = net.HTTPClient()
    http:request(
        url,
        {
            options = {
                method = "POST",
                headers = headers,
                data = json.encode(body)
            },
            success = function(response)
                if (response.status >= 200) then
                    if func then
                        func(json.decode(response.data))
                    else
                        print(response.data)
                    end
                else
                    self:error("Wrong status '" .. response.status .. "' in response!")
                end
            end,
            error = function(message)
                self:error("Connection error: " .. message)
            end
        }
    )
end

 

 

dans une scéne 

 

local id =  150 -- Id du Quick APP
local chat_id = 1111111   ---chat ID de votre telegram

local msg = "coucou"
print("message telegram = " .. msg)
fibaro.call(id, "sendMessage", msg , chat_id)

 

 

 

 

 

 

Modifié par 971jmd
  • Like 2
  • 2 ans après...
  • 10 mois après...
Posté(e)
Le 30/11/2022 à 17:27, Kage a dit :

thx for QA but how to send a picture

 

 

hello,

did you find how to send picture (wich comand line).

i know, 1 year later :)

 

thanks

Posté(e)
Le 05/10/2020 à 13:50, pepite a dit :

Merci pour le partage !!

Salut Pepite,

peux tu m'aiguiller sur la syntaxe de l'envoi d'une photo avec ce Qapp stp ?

telegram fonctionne avec mes scènes mais j'ai beau essayé, je n'arrive pas à joindre une photo avec un message.

 

Merci

Karl

×
×
  • Créer...