img command

This commit is contained in:
yago 2014-09-25 15:24:52 +02:00
parent 1d54ff40d0
commit d65b722ca2

View File

@ -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)