2015-07-03 00:15:52 +02:00
|
|
|
local PLUGIN = {}
|
|
|
|
|
|
|
|
PLUGIN.doc = [[
|
2015-07-08 09:38:04 +02:00
|
|
|
/giphy [query]
|
2015-07-09 02:11:33 +02:00
|
|
|
Returns a random or search-resulted GIF from giphy.com. Results are limited to PG-13 by default; use '/gifnsfw' to get potentially NSFW results.
|
2015-07-21 01:29:40 +02:00
|
|
|
Want GIFs sent directly to chat? Try @ImageBot.
|
2015-07-03 00:15:52 +02:00
|
|
|
]]
|
|
|
|
|
|
|
|
PLUGIN.triggers = {
|
2015-07-08 09:38:04 +02:00
|
|
|
'^/giphy',
|
|
|
|
'^/gifnsfw'
|
2015-07-03 00:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function PLUGIN.action(msg)
|
|
|
|
|
2015-07-15 08:15:23 +02:00
|
|
|
local search_url = 'http://api.giphy.com/v1/gifs/search?limit=10&api_key=' .. config.giphy_api_key
|
|
|
|
local random_url = 'http://tv.giphy.com/v1/gifs/random?api_key=' .. config.giphy_api_key
|
2015-07-03 00:15:52 +02:00
|
|
|
local result_url = ''
|
|
|
|
|
2015-07-08 09:38:04 +02:00
|
|
|
if string.match(msg.text, '^/giphynsfw') then
|
2015-07-03 00:15:52 +02:00
|
|
|
search_url = search_url .. '&rating=r&q='
|
|
|
|
random_url = random_url .. '&rating=r'
|
|
|
|
else
|
|
|
|
search_url = search_url .. '&rating=pg-13&q='
|
|
|
|
random_url = random_url .. '&rating=pg-13'
|
|
|
|
end
|
|
|
|
|
|
|
|
local input = get_input(msg.text)
|
|
|
|
|
|
|
|
if not input then
|
|
|
|
|
|
|
|
local jstr, res = HTTP.request(random_url)
|
|
|
|
if res ~= 200 then
|
2015-07-15 08:15:23 +02:00
|
|
|
return send_msg(msg, config.locale.errors.connection)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
result_url = jdat.data.image_url
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
local jstr, res = HTTP.request(search_url .. input)
|
|
|
|
if res ~= 200 then
|
2015-07-15 08:15:23 +02:00
|
|
|
return send_msg(msg, config.locale.errors.connection)
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
local jdat = JSON.decode(jstr)
|
2015-08-29 08:15:01 +02:00
|
|
|
if #jdat.data == 0 then
|
|
|
|
return send_msg(msg, config.locale.errors.results)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
result_url = jdat.data[math.random(#jdat.data)].images.original.url
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
send_message(msg.chat.id, result_url, false, msg.message_id)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return PLUGIN
|