2016-05-29 19:08:39 +02:00
|
|
|
-- Credit to Juan (tg:JuanPotato; gh:JuanPotato) for this plugin.
|
|
|
|
-- Or rather, the seven lines that actually mean anything.
|
|
|
|
|
|
|
|
local bing = {}
|
|
|
|
|
|
|
|
local URL = require('socket.url')
|
|
|
|
local JSON = require('dkjson')
|
|
|
|
local mime = require('mime')
|
|
|
|
local https = require('ssl.https')
|
|
|
|
local ltn12 = require('ltn12')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-05-29 19:08:39 +02:00
|
|
|
|
|
|
|
bing.command = 'bing <query>'
|
|
|
|
bing.doc = [[```
|
|
|
|
/bing <query>
|
|
|
|
Returns the top web search results from Bing.
|
|
|
|
Aliases: /g, /google
|
|
|
|
```]]
|
|
|
|
|
|
|
|
bing.search_url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=\'%s\'&$format=json'
|
|
|
|
|
2016-06-07 08:22:01 +02:00
|
|
|
function bing:init(config)
|
|
|
|
if not config.bing_api_key then
|
2016-05-29 19:08:39 +02:00
|
|
|
print('Missing config value: bing_api_key.')
|
|
|
|
print('bing.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
2016-06-07 08:22:01 +02:00
|
|
|
bing.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('bing', true):t('g', true):t('google', true).table
|
2016-05-29 19:08:39 +02:00
|
|
|
end
|
|
|
|
|
2016-06-07 09:02:05 +02:00
|
|
|
function bing:action(msg, config)
|
2016-05-29 19:08:39 +02:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
|
|
|
if msg.reply_to_message and msg.reply_to_message.text ~= '' then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
|
|
|
utilities.send_reply(self, msg, bing.doc, true)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local url = bing.search_url:format(URL.escape(input))
|
|
|
|
local resbody = {}
|
|
|
|
local _,b,_ = https.request{
|
|
|
|
url = url,
|
2016-06-07 09:02:05 +02:00
|
|
|
headers = { ["Authorization"] = "Basic " .. mime.b64(":" .. config.bing_api_key) },
|
2016-05-29 19:08:39 +02:00
|
|
|
sink = ltn12.sink.table(resbody),
|
|
|
|
}
|
|
|
|
if b ~= 200 then
|
2016-06-07 09:02:05 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2016-05-29 19:08:39 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
local dat = JSON.decode(table.concat(resbody))
|
|
|
|
local limit = 4
|
|
|
|
if msg.chat.type == 'private' then
|
|
|
|
limit = 8
|
|
|
|
end
|
|
|
|
if limit > #dat.d.results then
|
|
|
|
limit = #dat.d.results
|
|
|
|
end
|
|
|
|
local reslist = {}
|
|
|
|
for i = 1, limit do
|
|
|
|
local result = dat.d.results[i]
|
|
|
|
local s = '• [' .. result.Title:gsub('%]', '\\]') .. '](' .. result.Url:gsub('%)', '\\)') .. ')'
|
|
|
|
table.insert(reslist, s)
|
|
|
|
end
|
|
|
|
local output = '*Search results for* _' .. utilities.md_escape(input) .. '_ *:*\n' .. table.concat(reslist, '\n')
|
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
return bing
|