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/plugins/giphy.lua
topkecleon a1a4978a1b config, locale in lua
personality.lua -> interactions.lua
innumerable improvements
2015-07-15 02:15:23 -04:00

54 lines
1.3 KiB
Lua

local PLUGIN = {}
PLUGIN.doc = [[
/giphy [query]
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.
]]
PLUGIN.triggers = {
'^/giphy',
'^/gifnsfw'
}
function PLUGIN.action(msg)
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
local result_url = ''
if string.match(msg.text, '^/giphynsfw') then
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
return send_msg(msg, config.locale.errors.connection)
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
return send_msg(msg, config.locale.errors.connection)
end
local jdat = JSON.decode(jstr)
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