Skip to content

Instantly share code, notes, and snippets.

@TiagoDanin
Created May 4, 2016 18:27
Show Gist options
  • Save TiagoDanin/ded96061a86bf9ccf9ef0de69316e1e3 to your computer and use it in GitHub Desktop.
Save TiagoDanin/ded96061a86bf9ccf9ef0de69316e1e3 to your computer and use it in GitHub Desktop.
DuckDuckGo for otouto
local command = 'DuckDuckGo'
local doc = [[
DuckDuckGo <3
]]
local triggers = {
'^/duckduckgo[@'..bot.username..']*',
}
local action = function(msg)
local input = msg.text:input()
if not input then
if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text
else
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
return
end
end
local url = 'http://api.duckduckgo.com/?q=' .. URL.escape(input) .. '&format=json&pretty=1&no_html=1&skip_disambig=1'
if msg.from.id == msg.chat.id then
ns = 11
else
ns = 6
end
local jstr, res = HTTP.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if not jdat.RelatedTopics[1] then
sendReply(msg, config.errors.results)
return
end
local output = '🔍 *DuckDuckGo results for* _' .. input .. '_ *:*\n'
for i,v in ipairs(jdat.RelatedTopics) do
if jdat.RelatedTopics[i].Result then
if i < ns then
local title = jdat.RelatedTopics[i].Text:gsub('%[.+%]', ''):gsub('&amp;', '&')
if title:len() > 41 then
title = title:sub(1, 38) .. '...'
end
local url = jdat.RelatedTopics[i].FirstURL
if url:find('%)') then
--output = output .. '• ' .. title .. '\n' .. url:gsub('_', '\\_') .. '\n'
else
output = output .. '• [' .. title .. '](' .. url .. ')\n'
end
end
end
end
if jdat.AbstractText and jdat.AbstractURL then
if jdat.AbstractText ~= '' and jdat.AbstractURL ~= '' then
local text = jdat.AbstractText
if text:len() > 69 then
text = text:sub(1, 66) .. '...'
end
output = output .. '\n📖 Wiki: ' .. text.. ' [Open Wiki]('.. jdat.AbstractURL ..')'
end
end
sendMessage(msg.chat.id, output, true, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment