diff --git a/src/plugins/mocx/duckduckgo/duckduckgo.ts b/src/plugins/mocx/duckduckgo/duckduckgo.ts new file mode 100644 index 0000000..3912b48 --- /dev/null +++ b/src/plugins/mocx/duckduckgo/duckduckgo.ts @@ -0,0 +1,70 @@ +export class Plugin { + bot:any; + database:any; + client:any; + commands:any; + request:any; + + constructor(bot:any) { + this.bot = bot; + this.database = bot.database; + this.client = bot.client; + this.commands = { + 's': 'onCommandDuckDuckGo', + 'ddg': 'onCommandDuckDuckGo' + }; + this.request = require('request'); + } + + onCommandDuckDuckGo(from:string, to:string, message:string, args:any) { + var query = args.splice(1).join(' '); + var _this = this; + this.request({ + url: 'http://api.duckduckgo.com/', + qs: { + q: query, + format: 'json', + no_html: 1, + no_redirect: 1 + } + }, function(err, response, body) { + if(err) { + _this.bot.reply(from, to, err, 'notice'); + return; + } + + if(response.statusCode == 200) { + try { + var results = JSON.parse(body); + + if(results.Redirect.length > 0) { + _this.bot.reply(from, to, results.Redirect); + } else if(results.RelatedTopics && results.RelatedTopics.length > 0) { + var max; + + if (results.RelatedTopics.length >= 10) { + max = 10; + } else { + max = results.RelatedTopics.length; + } + + for (var i = 0; i < max; i++) { + var result = results.RelatedTopics[i]; + + if (i == 0) { + _this.bot.reply(from, to, result.Text + ' - ' + result.FirstURL); + } + + _this.bot.reply(from, to, result.Text + ' - ' + result.FirstURL, 'notice'); + } + } else { + _this.bot.reply(from, to, 'no results.'); + } + } catch (e) { + _this.bot.reply(from, to, 'error: ' + e.message, 'notice'); + } + } + }); + } + +} diff --git a/src/plugins/mocx/duckduckgo/package.json b/src/plugins/mocx/duckduckgo/package.json new file mode 100644 index 0000000..d0bb8a5 --- /dev/null +++ b/src/plugins/mocx/duckduckgo/package.json @@ -0,0 +1,9 @@ +{ + + "name": "duckduckgo", + "title": "DuckDuckGo Search", + "description": "DuckDuckGo Search Module for Modubot", + "version": "1.0.0", + "author": {"name": "Alex Kavon"} + +}