2015-01-12 22:29:59 +01:00
|
|
|
-- Idea by https://github.com/asdofindia/telegram-bot/
|
|
|
|
-- See http://api.giphy.com/
|
|
|
|
|
2015-02-23 00:11:31 +01:00
|
|
|
do
|
|
|
|
|
|
|
|
local BASE_URL = 'http://api.giphy.com/v1'
|
|
|
|
local API_KEY = 'dc6zaTOxFJmzC' -- public beta key
|
|
|
|
|
2015-04-17 16:29:30 +02:00
|
|
|
local function get_image(response)
|
2015-02-28 12:30:58 +01:00
|
|
|
local images = json:decode(response).data
|
|
|
|
if #images == 0 then return nil end -- No images
|
2015-04-09 21:57:54 +02:00
|
|
|
local i = math.random(#images)
|
2015-02-28 12:30:58 +01:00
|
|
|
local image = images[i] -- A random one
|
|
|
|
|
|
|
|
if image.images.downsized then
|
|
|
|
return image.images.downsized.url
|
|
|
|
end
|
|
|
|
|
|
|
|
if image.images.original then
|
2016-01-11 19:39:19 +01:00
|
|
|
return image.original.mp4
|
2015-02-28 12:30:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2015-04-17 16:29:30 +02:00
|
|
|
local function get_random_top()
|
2015-02-28 12:30:58 +01:00
|
|
|
local url = BASE_URL.."/gifs/trending?api_key="..API_KEY
|
|
|
|
local response, code = http.request(url)
|
2015-02-23 00:11:31 +01:00
|
|
|
if code ~= 200 then return nil end
|
2015-02-28 12:30:58 +01:00
|
|
|
return get_image(response)
|
2015-01-12 22:29:59 +01:00
|
|
|
end
|
|
|
|
|
2015-04-17 16:29:30 +02:00
|
|
|
local function search(text)
|
2015-02-27 21:31:14 +01:00
|
|
|
text = URL.escape(text)
|
|
|
|
local url = BASE_URL.."/gifs/search?q="..text.."&api_key="..API_KEY
|
2015-02-28 12:30:58 +01:00
|
|
|
local response, code = http.request(url)
|
2015-02-27 21:23:23 +01:00
|
|
|
if code ~= 200 then return nil end
|
2015-02-28 12:30:58 +01:00
|
|
|
return get_image(response)
|
2015-01-12 22:29:59 +01:00
|
|
|
end
|
|
|
|
|
2015-04-17 16:29:30 +02:00
|
|
|
local function run(msg, matches)
|
2015-02-28 12:30:58 +01:00
|
|
|
local gif_url = nil
|
2015-05-28 16:47:30 +02:00
|
|
|
|
|
|
|
-- If no search data, a random trending GIF will be sent
|
2015-04-17 16:29:30 +02:00
|
|
|
if matches[1] == "/gif" or matches[1] == "/giphy" then
|
2015-01-12 22:29:59 +01:00
|
|
|
gif_url = get_random_top()
|
|
|
|
else
|
|
|
|
gif_url = search(matches[1])
|
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
if not gif_url then
|
|
|
|
return 'Keine GIF gefunden!'
|
2015-02-19 21:50:13 +01:00
|
|
|
end
|
|
|
|
|
2015-02-15 14:33:48 +01:00
|
|
|
local receiver = get_receiver(msg)
|
2015-05-28 16:47:30 +02:00
|
|
|
print("GIF URL: "..gif_url)
|
|
|
|
send_document_from_url(receiver, gif_url)
|
2015-01-12 22:29:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-04-14 20:21:23 +02:00
|
|
|
description = "Sucht und sendet ein GIF von Giphy",
|
2016-06-22 13:59:06 +02:00
|
|
|
usage = {"#gif [Begriff]","#giphy [Begriff]"},
|
|
|
|
patterns = {"^#gif$","^#gif (.*)","^#giphy (.*)","^#giphy$"},
|
2015-01-12 22:29:59 +01:00
|
|
|
run = run
|
2015-02-23 00:11:31 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 02:08:08 +02:00
|
|
|
end
|