2016-06-11 14:46:41 +02:00
|
|
|
local ninegag = {}
|
|
|
|
|
|
|
|
ninegag.command = '9gag'
|
|
|
|
|
|
|
|
function ninegag:init(config)
|
|
|
|
ninegag.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('9gag', true):t('9fag', true).table
|
2016-07-14 11:48:48 +02:00
|
|
|
ninegag.inline_triggers = {
|
|
|
|
"^9gag"
|
|
|
|
}
|
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-14 11:48:48 +02:00
|
|
|
local url = "http://api-9gag.herokuapp.com/"
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function ninegag:get_9GAG()
|
2016-07-31 21:29:44 +02:00
|
|
|
local b,c = http.request(url)
|
2016-06-11 14:46:41 +02:00
|
|
|
if c ~= 200 then return nil end
|
2016-07-31 21:29:44 +02:00
|
|
|
local gag = json.decode(b)
|
2016-06-11 14:46:41 +02:00
|
|
|
-- random max json table size
|
2016-07-14 11:48:48 +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-05 15:25:08 +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-14 11:48:48 +02:00
|
|
|
function ninegag:inline_callback(inline_query, config)
|
2016-07-31 21:29:44 +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-07-31 21:29:44 +02:00
|
|
|
local gag = json.decode(res)
|
2016-07-14 11:48:48 +02:00
|
|
|
|
|
|
|
local results = '['
|
2016-08-07 21:20:48 +02:00
|
|
|
local id = 50
|
2016-07-14 11:48:48 +02:00
|
|
|
for n in pairs(gag) do
|
|
|
|
local title = gag[n].title:gsub('"', '\\"')
|
2016-08-07 21:20:48 +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-14 11:48:48 +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-14 11:48:48 +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-05 15:25:08 +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
|
|
|
|
|
2016-10-03 20:19:20 +02:00
|
|
|
utilities.send_photo(msg.chat.id, url, title, msg.message_id, '{"inline_keyboard":[[{"text":"Post aufrufen","url":"'..post_url..'"}]]}')
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return ninegag
|