envio de imagenes en 9gag

This commit is contained in:
yago 2014-08-05 14:07:25 +02:00
parent 512e2465f5
commit 0a6f071154
3 changed files with 18 additions and 10 deletions

3
.gitmodules vendored
View File

@ -0,0 +1,3 @@
[submodule "tg"]
path = tg
url = git@github.com:yagop/tg.git

View File

@ -1,11 +1,11 @@
http = require("socket.http") http = require("socket.http")
json = (loadfile "./bot/JSON.lua")() json = (loadfile "./bot/JSON.lua")()
VERSION = 'v0.0.7' VERSION = 'v0.1'
function on_msg_receive (msg) function on_msg_receive (msg)
-- vardump(msg) -- vardump(msg)
mark_read(get_receiver(msg)) -- mark_read(get_receiver(msg))
if msg.out then if msg.out then
return return
end end
@ -23,7 +23,7 @@ function on_msg_receive (msg)
msg.text = msg.text:sub(2,-1) msg.text = msg.text:sub(2,-1)
do_action(msg) do_action(msg)
end end
write_log_file(msg) -- write_log_file(msg)
end end
-- Where magic happens -- Where magic happens
@ -45,8 +45,12 @@ function do_action(msg)
send_msg(receiver, text) send_msg(receiver, text)
end end
if string.starts(msg.text, '9gag') then if string.starts(msg.text, '9gag') then
text = get_infiniGAG() url = get_infiniGAG()
send_msg(receiver, text) file_name = url:match("([^/]+)$")
file = "/tmp/"..file_name
sh = "curl -o '"..file.."' "..url
run_bash(sh)
send_photo(receiver, file)
end end
if string.starts(msg.text, 'fortune') then if string.starts(msg.text, 'fortune') then
text = run_bash('fortune') text = run_bash('fortune')
@ -101,8 +105,7 @@ function do_action(msg)
!fortune : print a random adage !fortune : print a random adage
!weather [city] : weather in that city (Madrid if not city) !weather [city] : weather in that city (Madrid if not city)
!9gag : send random url image from 9gag !9gag : send random url image from 9gag
!uc3m : fortunes from Universidad Carlos III !uc3m : fortunes from Universidad Carlos III]]
!hackers : send text to group Juankers]]
send_msg(receiver, text) send_msg(receiver, text)
end end
end end
@ -230,8 +233,9 @@ end
function get_infiniGAG() function get_infiniGAG()
b, c, h = http.request("http://infinigag-us.aws.af.cm") b, c, h = http.request("http://infinigag-us.aws.af.cm")
local gag = json:decode(b) local gag = json:decode(b)
i = math.random(table.getn(gag.data)) -- random i = math.random(#gag.data) -- random max json table size (# is an operator o.O)
local link_image = gag.data[i].images.normal local link_image = gag.data[i].images.normal
print("9gag image"..link_image)
return link_image return link_image
end end
@ -255,7 +259,7 @@ function get_weather(location)
return temp .. '\n' .. conditions return temp .. '\n' .. conditions
end end
function sanitize(txt) function sanitize_html(txt)
local replacements = { local replacements = {
['&' ] = '&', ['&' ] = '&',
['<' ] = '&lt;', ['<' ] = '&lt;',

1
tg Submodule

@ -0,0 +1 @@
Subproject commit ba9bf5ba7af11de6da399409814b5da1426ca8f5