From fb38228a97209f8bf132693feed33e247684e840 Mon Sep 17 00:00:00 2001 From: yago Date: Mon, 12 Jan 2015 22:29:59 +0100 Subject: [PATCH] GiphyAPI --- plugins/giphy.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 plugins/giphy.lua diff --git a/plugins/giphy.lua b/plugins/giphy.lua new file mode 100644 index 0000000..ccf8697 --- /dev/null +++ b/plugins/giphy.lua @@ -0,0 +1,46 @@ +-- Idea by https://github.com/asdofindia/telegram-bot/ +-- See http://api.giphy.com/ + +function get_random_top() + local api_key = "dc6zaTOxFJmzC" -- public beta key + b = http.request("http://api.giphy.com/v1/gifs/trending?api_key="..api_key) + local images = json:decode(b).data + math.randomseed(os.time()) + local i = math.random(0,#images) + return images[i].images.downsized.url +end + +function search(text) + local api_key = "dc6zaTOxFJmzC" -- public beta key + b = http.request("http://api.giphy.com/v1/gifs/search?q="..text.."&api_key="..api_key) + local images = json:decode(b).data + math.randomseed(os.time()) + local i = math.random(0,#images) + return images[i].images.downsized.url +end + +function run(msg, matches) + -- If no search data, a cat gif will be sended + -- Because everyone loves pussies + if matches[1] == "!gif" or matches[1] == "!giphy" then + gif_url = get_random_top() + else + gif_url = search(matches[1]) + end + + file = download_to_file(gif_url) + send_document(get_receiver(msg), file, ok_cb, false) + return "preparing to make you laugh" +end + +return { + description = "Sends you a laughable gif", + usage = "", + patterns = { + "^!gif$", + "^!gif (.*)", + "^!giphy (.*)", + "^!giphy$" + }, + run = run +} \ No newline at end of file