Troubles sending images

This commit is contained in:
yago 2015-02-22 22:34:10 +01:00
parent e7bc20af3e
commit 6b49398098
2 changed files with 33 additions and 28 deletions

View File

@ -1,17 +1,20 @@
do
function run(msg, matches) function run(msg, matches)
file = download_to_file(matches[1]) local url = matches[1]
print("I will send the image " .. file) local receiver = get_receiver(msg)
send_photo(get_receiver(msg), file, ok_cb, false) send_photo_from_url(receiver, url)
end end
return { return {
description = "When user sends image URL (ends with png, jpg, jpeg) download and send it to origin.", description = "When user sends image URL (ends with png, jpg, jpeg) download and send it to origin.",
usage = "", usage = "",
patterns = { patterns = {
"(https?://[%w-_%.%?%.:/%+=&]+.png)$", "(https?://[%w-_%.%?%.:/%+=&]+.png)$",
"(https?://[%w-_%.%?%.:/%+=&]+.jpg)$", "(https?://[%w-_%.%?%.:/%+=&]+.jpg)$",
"(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$", "(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$",
}, },
run = run run = run
} }
end

View File

@ -1,27 +1,28 @@
do
function getGoogleImage(text)
text = URL.escape(text)
for i = 1, 5, 1 do -- Try 5 times
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
b = http.request(api..text)
local google = json:decode(b)
if (google.responseStatus == 200) then -- OK function getGoogleImage(text)
math.randomseed(os.time()) local text = URL.escape(text)
i = math.random(#google.responseData.results) -- Random image from results local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
return google.responseData.results[i].url local res, code = http.request(api..text)
end if code ~= 200 then return nil end
local google = json:decode(res)
if google.responseStatus ~= 200 then
return nil
end end
-- Google response Ok
local i = math.random(#google.responseData.results) -- Random image from results
return google.responseData.results[i].url
end end
function run(msg, matches) function run(msg, matches)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
local text = msg.text:sub(6,-1) local text = msg.text:sub(6,-1)
local url = getGoogleImage(text) local url = getGoogleImage(text)
local file_path = download_to_file(url) print("Image URL: ", url)
print(file_path) send_photo_from_url(receiver, url)
send_photo(receiver, file_path, ok_cb, false)
return nil
end end
return { return {
@ -31,3 +32,4 @@ return {
run = run run = run
} }
end