2016-06-11 14:46:41 +02:00
|
|
|
local ninegag = {}
|
|
|
|
|
|
|
|
ninegag.command = '9gag'
|
|
|
|
|
|
|
|
function ninegag:init(config)
|
2016-07-18 18:37:29 +02:00
|
|
|
ninegag.triggers = {
|
|
|
|
"^/9[Gg][Aa][Gg]$",
|
|
|
|
"^/9[Ff][Aa][Gg]$"
|
|
|
|
}
|
2016-07-17 13:47:15 +02:00
|
|
|
ninegag.inline_triggers = {
|
2016-07-18 18:37:29 +02:00
|
|
|
"^9[Gg][Aa][Gg]"
|
2016-07-17 13:47:15 +02:00
|
|
|
}
|
2016-06-11 14:46:41 +02:00
|
|
|
ninegag.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[9gag*: Gibt ein zufälliges Bild von den momentan populärsten 9GAG-Posts aus]]
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:47:15 +02:00
|
|
|
local url = "http://api-9gag.herokuapp.com/"
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function ninegag:get_9GAG()
|
2016-08-01 21:07:27 +02:00
|
|
|
local b,c = http.request(url)
|
2016-06-11 14:46:41 +02:00
|
|
|
if c ~= 200 then return nil end
|
2016-08-01 21:07:27 +02:00
|
|
|
local gag = json.decode(b)
|
2016-06-11 14:46:41 +02:00
|
|
|
-- random max json table size
|
2016-07-17 13:47:15 +02:00
|
|
|
local i = math.random(#gag)
|
|
|
|
|
|
|
|
local link_image = gag[i].src
|
2016-06-11 14:46:41 +02:00
|
|
|
local title = gag[i].title
|
2016-07-17 13:22:27 +02:00
|
|
|
local post_url = gag[i].url
|
2016-06-11 14:46:41 +02:00
|
|
|
return link_image, title, post_url
|
|
|
|
end
|
|
|
|
|
2016-07-17 13:47:15 +02:00
|
|
|
function ninegag:inline_callback(inline_query, config)
|
2016-08-01 21:07:27 +02:00
|
|
|
local res, code = http.request(url)
|
2016-08-24 15:38:29 +02:00
|
|
|
if code ~= 200 then abort_inline_query(inline_query) return end
|
2016-08-01 21:07:27 +02:00
|
|
|
local gag = json.decode(res)
|
2016-07-17 13:47:15 +02:00
|
|
|
|
|
|
|
local results = '['
|
2016-08-15 23:14:28 +02:00
|
|
|
local id = 50
|
2016-07-17 13:47:15 +02:00
|
|
|
for n in pairs(gag) do
|
|
|
|
local title = gag[n].title:gsub('"', '\\"')
|
2016-08-15 23:14:28 +02:00
|
|
|
results = results..'{"type":"photo","id":"'..id..'","photo_url":"'..gag[n].src..'","thumb_url":"'..gag[n].src..'","caption":"'..title..'","reply_markup":{"inline_keyboard":[[{"text":"9GAG aufrufen","url":"'..gag[n].url..'"}]]}}'
|
|
|
|
id = id+1
|
2016-07-17 13:47:15 +02:00
|
|
|
if n < #gag then
|
|
|
|
results = results..','
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local results = results..']'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_inline_query(inline_query, results, 300)
|
2016-07-17 13:47:15 +02:00
|
|
|
end
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function ninegag:action(msg, config)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-07-17 13:22:27 +02:00
|
|
|
local url, title, post_url = ninegag:get_9GAG()
|
2016-06-11 14:46:41 +02:00
|
|
|
if not url then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection)
|
2016-06-11 14:46:41 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local file = download_to_file(url)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_photo(msg.chat.id, file, title, msg.message_id, '{"inline_keyboard":[[{"text":"Post aufrufen","url":"'..post_url..'"}]]}')
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
return ninegag
|