-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimg_google.lua
More file actions
35 lines (28 loc) · 878 Bytes
/
img_google.lua
File metadata and controls
35 lines (28 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
do
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)
print("Image URL: ", url)
send_photo_from_url(receiver, url)
end
return {
description = "Search image with Google API and sends it.",
usage = "!img [term]: Random search an image with Google API.",
patterns = {"^!img (.*)$"},
run = run
}
end