Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DuckDuckGo plugin #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/plugins/mocx/duckduckgo/duckduckgo.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
});
}

}
9 changes: 9 additions & 0 deletions src/plugins/mocx/duckduckgo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{

"name": "duckduckgo",
"title": "DuckDuckGo Search",
"description": "DuckDuckGo Search Module for Modubot",
"version": "1.0.0",
"author": {"name": "Alex Kavon"}

}