Acciones HTTP llevadas a plugins

This commit is contained in:
yago 2014-11-04 22:39:14 +01:00
parent 7d8abde1d1
commit 50d88007a0
4 changed files with 75 additions and 74 deletions

View File

@ -30,20 +30,8 @@
if msg_valid(msg) == false then if msg_valid(msg) == false then
return return
end end
-- Check if command starts with ! eg !echo
if msg.text:sub(0,1) == '!' then
do_action(msg) do_action(msg)
else
if is_image_url(msg.text) then
send_image_from_url (msg)
elseif get_youtube_code(msg.text) then
send_youtube_thumbnail(msg)
else
if is_file_url(msg.text) then
send_file_from_url(msg)
end
end
end
mark_read(get_receiver(msg), ok_cb, false) mark_read(get_receiver(msg), ok_cb, false)
-- write_log_file(msg) -- write_log_file(msg)
@ -53,6 +41,9 @@
end end
function msg_valid(msg) function msg_valid(msg)
if msg.text == nil then
return false
end
if msg.from.id == our_id then if msg.from.id == our_id then
return true return true
end end
@ -70,56 +61,6 @@
end end
end end
function send_file_from_url (msg)
last = string.get_last_word(msg.text)
file = download_to_file(last)
send_document(get_receiver(msg), file, ok_cb, false)
end
function send_image_from_url (msg)
last = string.get_last_word(msg.text)
file = download_to_file(last)
print("I will send the image " .. file)
send_photo(get_receiver(msg), file, ok_cb, false)
end
function is_image_url(text)
last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) -- TODO: Change it please
if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then
return true
end
return false
end
function get_youtube_code( text )
local yt_normal = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]+)")
if yt_normal then
return yt_normal
end
yt_short = string.match(text, "youtu.be/([A-Za-z0-9-]+)")
if yt_short then
return yt_short
end
return false
end
function send_youtube_thumbnail(msg)
yt_code = get_youtube_code(msg.text)
yt_thumbnail = "http://img.youtube.com/vi/".. yt_code .."/hqdefault.jpg"
file = download_to_file(yt_thumbnail)
send_photo(get_receiver(msg), file, ok_cb, false)
end
function is_file_url(text)
last = string.get_last_word(text)
extension = string.get_extension_from_filename(last)
if extension == 'gif' then
return true
end
return false
end
-- Where magic happens -- Where magic happens
function do_action(msg) function do_action(msg)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)

18
plugins/gifs.lua Normal file
View File

@ -0,0 +1,18 @@
function send_file_from_url (msg, url)
last = string.get_last_word(ul)
file = download_to_file(last)
send_document(get_receiver(msg), file, ok_cb, false)
end
function run(msg, matches)
send_file_from_url(msg, matches[1])
end
return {
description = "from gif URL downloads it and sends to origin",
usage = "",
patterns = {
"(https?://[%w-_%.%?%.:/%+=&]+.gif)$"
},
run = run
}

22
plugins/images.lua Normal file
View File

@ -0,0 +1,22 @@
function send_image_from_url (msg)
last = string.get_last_word(msg.text)
file = download_to_file(last)
print("I will send the image " .. file)
send_photo(get_receiver(msg), file, ok_cb, false)
end
function run(msg, matches)
send_image_from_url(msg)
end
return {
description = "from image URL downloads it and sends to origin",
usage = "",
patterns = {
"(https?://[%w-_%.%?%.:/%+=&]+.png)$",
"(https?://[%w-_%.%?%.:/%+=&]+.jpg)$",
"(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$",
},
run = run
}

20
plugins/youtube.lua Normal file
View File

@ -0,0 +1,20 @@
function send_youtube_thumbnail(msg, yt_code)
yt_thumbnail = "http://img.youtube.com/vi/".. yt_code .."/hqdefault.jpg"
file = download_to_file(yt_thumbnail)
send_photo(get_receiver(msg), file, ok_cb, false)
end
function run(msg, matches)
send_youtube_thumbnail(msg, matches[1])
end
return {
description = "sends YouTube image",
usage = "",
patterns = {
"youtu.be/([A-Za-z0-9-]+)",
"youtube.com/watch%?v=([A-Za-z0-9-]+)",
},
run = run
}