InlineQuerys für QR

This commit is contained in:
Andreas Bielawski 2016-07-14 20:34:47 +02:00
parent a30e35b853
commit 44e4d37b4e

View File

@ -9,6 +9,9 @@ function qr:init(config)
'^/qr "(%w+)" "(%w+)" (.+)$', '^/qr "(%w+)" "(%w+)" (.+)$',
"^/qr (.+)$" "^/qr (.+)$"
} }
qr.inline_triggers = {
"^qr (https?://[%w-_%.%?%.:/%+=&]+)"
}
qr.doc = [[* qr.doc = [[*
]]..config.cmd_pat..[[qr* _<Text>_: Sendet QR-Code mit diesem Text ]]..config.cmd_pat..[[qr* _<Text>_: Sendet QR-Code mit diesem Text
*]]..config.cmd_pat..[[qr* _"[Hintergrundfarbe]"_ _"[Datenfarbe]"_ _[Text]_ *]]..config.cmd_pat..[[qr* _"[Hintergrundfarbe]"_ _"[Datenfarbe]"_ _[Text]_
@ -40,12 +43,16 @@ function qr:get_hex(str)
return str return str
end end
function qr:qr(text, color, bgcolor) function qr:qr(text, color, bgcolor, img_format)
local url = "http://api.qrserver.com/v1/create-qr-code/?" local url = "http://api.qrserver.com/v1/create-qr-code/?"
.."size=600x600" --fixed size otherways it's low detailed .."size=600x600" --fixed size otherways it's low detailed
.."&data="..URL.escape(utilities.trim(text)) .."&data="..URL.escape(utilities.trim(text))
if img_format then
url = url..'&format='..img_format
end
if color then if color then
url = url.."&color="..qr:get_hex(color) url = url.."&color="..qr:get_hex(color)
end end
@ -66,6 +73,30 @@ function qr:qr(text, color, bgcolor)
return nil return nil
end end
function qr:inline_callback(inline_query, config, matches)
local text = matches[1]
if string.len(text) > 200 then return end
local image_url = qr:qr(text, nil, nil, 'jpg')
if not image_url then return end
local results = '[{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..image_url..'","thumb_url":"'..image_url..'","photo_width":600,"photo_height":600,"caption":"'..text..'"},'
local i = 0
while i < 29 do
i = i+1
local color = math.random(255)
local bgcolor = math.random(255)
local image_url = qr:qr(text, color, bgcolor, 'jpg')
results = results..'{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..image_url..'","thumb_url":"'..image_url..'","photo_width":600,"photo_height":600,"caption":"'..text..'"}'
if i < 29 then
results = results..','
end
end
local results = results..']'
utilities.answer_inline_query(self, inline_query, results, 10000)
end
function qr:action(msg, config, matches) function qr:action(msg, config, matches)
local text = matches[1] local text = matches[1]
local color local color