From d65b722ca2c4753f67a3527ce286afd7bac4d8a8 Mon Sep 17 00:00:00 2001 From: yago Date: Thu, 25 Sep 2014 15:24:52 +0200 Subject: [PATCH] img command --- bot/bot.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bot/bot.lua b/bot/bot.lua index cd3bb04..f0febe4 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -1,7 +1,8 @@ http = require("socket.http") +URL = require("socket.url") json = (loadfile "./bot/JSON.lua")() -VERSION = 'v0.2.1' +VERSION = 'v0.3' function on_msg_receive (msg) @@ -105,6 +106,17 @@ function do_action(msg) return end + if string.starts(msg.text, '!img') then + text = msg.text:sub(5,-1) + url = getGoogleImage(text) + file_name = url:match("([^/]+)$") + file = "/tmp/"..file_name + sh = "curl -o '"..file.."' "..url + run_bash(sh) + send_photo(receiver, file, ok_cb, false) + return + end + if string.starts(msg.text, '!9gag') then url, title = get_9GAG() file_name = url:match("([^/]+)$") @@ -272,6 +284,14 @@ function get_fortunes_uc3m() return b end +function getGoogleImage(text) + local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=" + text = URL.escape(text) + b = http.request(api..text) + local google = json:decode(b) + return google.responseData.results[1].url +end + function get_9GAG() b = http.request("http://api-9gag.herokuapp.com/") local gag = json:decode(b)