JOSHUA HOLBROOK github : twitter

ddgbot (posted 23 Oct 2012)

Daang, it’s been a while, hasn’t it? Last blog post is over 6 months old! This one’s less interesting, but it does change the scenery a bit.

I got the itch to make another irc bot the other day, after installing one of my old ones to my Linode. I’d always wanted a bot for querying duckduckgo—or, at least, as long as I’ve known about duckduckgo. I’m a little bit of a fanboy.

It turns out their “api” is actually really easy to use! It’s pretty much the same as a normal search querystring, except you hit a different endpoint and you get a few extra params for choosing output style (why you wouldn’t pick JSON I don’t know):

function ddg(query, cb) {
  var url = '' + qs.stringify({
    q: query,
    format: 'json',
    no_redirect: '1',
    no_html: '1'
  request(url, function (err, res, body) {
    if (err) {
      return cb(err);

    try {
      body = JSON.parse(body);
    catch (err) {
      return cb(err);

    cb(null, body, url);

Even better, duckduckgo supports these !bang commandshundreds of them, and all I have to do to make them work is pass the received irc message in directly to ddg:

ddg(msg, function (err, data, url) {
  if (err) {
    client.say(to, 'ddg error: ' + err.message);
    return log.error(err.stack);
  }'ddg: `' + msg + '` => `' + JSON.stringify(data) + '`');

The entire bot is 101 lines long, and most of it deals with pretty-printing the received data in IRC. I find it amusing that this bot has more valid commands than lines of code. Here are some examples I like; there are many more.

11:55 <@jesusabdullah> !erowid klonopin
11:55 < _ddgbot>
11:55 <@jesusabdullah> they added erowid cause I asked them to on twitter \m/

12:14 <@jesusabdullah> !google how to write an irc bot
12:14 < _ddgbot>

11:54 <@jesusabdullah> !npm ncp
11:54 < _ddgbot>

11:57 <@jesusabdullah> !urban gnar
11:57 < _ddgbot>

Sauce is here. Have fun!