This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/otouto/plugins/imgur.lua
Andreas Bielawski fb9d3652f3 Upstream:
- self muss nicht mehr überall übergeben werden
- alle Plugins wurden angepasst

Eigene Updates:
- abort_inline_query() hinzugefügt
- Mehr Plugins zum Standard-Set hinzugefügt
- Eventuell noch etwas, was ich vergessen hab
2016-08-24 15:38:29 +02:00

57 lines
1.4 KiB
Lua

local imgur = {}
function imgur:init(config)
if not cred_data.imgur_client_id then
print('Missing config value: imgur_client_id.')
print('imgur.lua will not be enabled.')
return
end
imgur.triggers = {
"imgur.com/([A-Za-z0-9]+).gifv",
"https?://imgur.com/([A-Za-z0-9]+)"
}
end
local client_id = cred_data.imgur_client_id
local BASE_URL = 'https://api.imgur.com/3'
function imgur:get_imgur_data(imgur_code)
local response_body = {}
local request_constructor = {
url = BASE_URL..'/image/'..imgur_code,
method = "GET",
sink = ltn12.sink.table(response_body),
headers = {
Authorization = 'Client-ID '..client_id
}
}
local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
if not ok then
return nil
end
local response_body = json.decode(table.concat(response_body))
if response_body.status ~= 200 then return nil end
return response_body.data.link
end
function imgur:action(msg)
local imgur_code = matches[1]
if imgur_code == "login" then return nil end
utilities.send_typing(msg.chat.id, 'upload_photo')
local link = imgur:get_imgur_data(imgur_code)
if link then
local file = download_to_file(link)
if string.ends(link, ".gif") then
utilities.send_document(msg.chat.id, file, nil, msg.message_id)
else
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
end
end
end
return imgur