2016-07-17 13:22:27 +02:00
|
|
|
local bImages = {}
|
|
|
|
|
|
|
|
function bImages:init(config)
|
|
|
|
if not cred_data.bing_search_key then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: bing_search_key.')
|
|
|
|
print('bImages.lua wird nicht aktiviert.')
|
2016-07-17 13:22:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
bImages.triggers = {"^/nil$"}
|
|
|
|
bImages.inline_triggers = {
|
2016-07-18 18:37:29 +02:00
|
|
|
"^[Ii][Mm][Gg] (.*)"
|
2016-07-17 13:22:27 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
local apikey = cred_data.bing_search_key
|
|
|
|
local BASE_URL = 'https://api.cognitive.microsoft.com/bing/v5.0'
|
|
|
|
|
|
|
|
function bImages:getImages(query)
|
|
|
|
local url = BASE_URL..'/images/search?q='..URL.escape(query)..'&count=50&mkt=de-de'
|
|
|
|
local response_body = {}
|
|
|
|
local request_constructor = {
|
|
|
|
url = url,
|
|
|
|
method = "GET",
|
|
|
|
sink = ltn12.sink.table(response_body),
|
|
|
|
redirect = false,
|
|
|
|
headers = {
|
|
|
|
["Ocp-Apim-Subscription-Key"] = apikey
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 21:07:27 +02:00
|
|
|
local ok, response_code, response_headers = https.request(request_constructor)
|
2016-07-17 13:22:27 +02:00
|
|
|
if not ok then return end
|
2016-08-01 21:07:27 +02:00
|
|
|
local images = json.decode(table.concat(response_body)).value
|
2016-07-17 13:22:27 +02:00
|
|
|
if not images[1] then return end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local results = '['
|
2016-08-15 23:14:28 +02:00
|
|
|
local id = 300
|
2016-07-17 13:22:27 +02:00
|
|
|
for n in pairs(images) do
|
|
|
|
if images[n].encodingFormat == 'jpeg' then -- Inline-Querys MUST use JPEG photos!
|
|
|
|
local photo_url = images[n].contentUrl
|
|
|
|
local thumb_url = images[n].thumbnailUrl
|
2016-08-15 23:14:28 +02:00
|
|
|
results = results..'{"type":"photo","id":"'..id..'","photo_url":"'..photo_url..'","thumb_url":"'..thumb_url..'","photo_width":'..images[n].width..',"photo_height":'..images[n].height..',"reply_markup":{"inline_keyboard":[[{"text":"Bing aufrufen","url":"'..images[n].webSearchUrl..'"},{"text":"Bild öffnen","url":"'..photo_url..'"}]]}},'
|
|
|
|
id = id+1
|
2016-07-17 13:22:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local results = results:sub(0, -2)
|
|
|
|
local results = results..']'
|
|
|
|
cache_data('bImages', string.lower(query), results, 1209600, 'key')
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
|
|
|
function bImages:inline_callback(inline_query, config, matches)
|
|
|
|
local query = matches[1]
|
|
|
|
local results = redis:get('telegram:cache:bImages:'..string.lower(query))
|
|
|
|
if not results then
|
|
|
|
results = bImages:getImages(query)
|
|
|
|
end
|
|
|
|
|
2016-08-15 23:14:28 +02:00
|
|
|
if not results then utilities.answer_inline_query(self, inline_query) return end
|
2016-07-17 13:22:27 +02:00
|
|
|
utilities.answer_inline_query(self, inline_query, results, 3600)
|
|
|
|
end
|
|
|
|
|
|
|
|
function bImages:action()
|
|
|
|
end
|
|
|
|
|
|
|
|
return bImages
|