Check if image not found

This commit is contained in:
yago 2015-04-07 22:58:33 +02:00
parent cac8fe7999
commit f4ac6057ba

View File

@ -11,16 +11,30 @@ function getGoogleImage(text)
return nil return nil
end end
-- Google response Ok local data = google.responseData
local i = math.random(#google.responseData.results) -- Random image from results
return google.responseData.results[i].url
if not data or not data.results then
return nil
end
if #data.results == 0 then
return nil
end
-- Random image from table
local i = math.random(#data.results)
return data.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 = matches[1]
local url = getGoogleImage(text) local url = getGoogleImage(text)
if not url then
return "Error: Image not found"
end
print("Image URL: ", url) print("Image URL: ", url)
send_photo_from_url(receiver, url) send_photo_from_url(receiver, url)
end end