diff --git a/plugins/images.lua b/plugins/images.lua index c39d725..bf342f6 100644 --- a/plugins/images.lua +++ b/plugins/images.lua @@ -1,17 +1,20 @@ +do function run(msg, matches) - file = download_to_file(matches[1]) - print("I will send the image " .. file) - send_photo(get_receiver(msg), file, ok_cb, false) + local url = matches[1] + local receiver = get_receiver(msg) + send_photo_from_url(receiver, url) end return { - description = "When user sends image URL (ends with png, jpg, jpeg) download and send it to origin.", - usage = "", - patterns = { - "(https?://[%w-_%.%?%.:/%+=&]+.png)$", - "(https?://[%w-_%.%?%.:/%+=&]+.jpg)$", - "(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$", - }, - run = run -} \ No newline at end of file + description = "When user sends image URL (ends with png, jpg, jpeg) download and send it to origin.", + usage = "", + patterns = { + "(https?://[%w-_%.%?%.:/%+=&]+.png)$", + "(https?://[%w-_%.%?%.:/%+=&]+.jpg)$", + "(https?://[%w-_%.%?%.:/%+=&]+.jpeg)$", + }, + run = run +} + +end \ No newline at end of file diff --git a/plugins/img_google.lua b/plugins/img_google.lua index 456a247..5335082 100644 --- a/plugins/img_google.lua +++ b/plugins/img_google.lua @@ -1,27 +1,28 @@ - -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) +do - if (google.responseStatus == 200) then -- OK - math.randomseed(os.time()) - i = math.random(#google.responseData.results) -- Random image from results - return google.responseData.results[i].url - end +function getGoogleImage(text) + local text = URL.escape(text) + local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q=" + local res, code = http.request(api..text) + if code ~= 200 then return nil end + local google = json:decode(res) + + if google.responseStatus ~= 200 then + return nil end + + -- Google response Ok + local i = math.random(#google.responseData.results) -- Random image from results + return google.responseData.results[i].url + end function run(msg, matches) local receiver = get_receiver(msg) local text = msg.text:sub(6,-1) local url = getGoogleImage(text) - local file_path = download_to_file(url) - print(file_path) - send_photo(receiver, file_path, ok_cb, false) - return nil + print("Image URL: ", url) + send_photo_from_url(receiver, url) end return { @@ -31,3 +32,4 @@ return { run = run } +end \ No newline at end of file