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)
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
}
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

View File

@ -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