Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 81 lines (73 sloc) 2.803 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
# Description:
# Return the word of the day.
#
# Dependencies:
# "xml2js": "0.1.14"
#
# Configuration:
# WOTD_PROVIDER - to 'wordnik' or 'dictionary'
# WORDNIK_API_KEY - API key from http://developer.wordnik.com/ or http://developer.dictionary.com/
#
# Commands:
# hubot wotd (me) (short) - Returns the word of the day.
# hubot word of the day (me) (short) - Returns the word of the day.
#
# Notes:
# FIXME This should be merged with wordnik.coffee
#
# Author:
# tapichu

# FIXME use JSON, to avoid a dependency
Parser = require("xml2js").Parser

module.exports = (robot) ->

  robot.respond /(word of the day|wotd)\s?(me)?\s?(short)?(.*)$/i, (msg) ->
    # FIXME prefix WOTD_PROVIDER and DICTIONARY_API_KEY with HUBOT_ for consistency
    if process.env.WOTD_PROVIDER is "wordnik" and process.env.WORDNIK_API_KEY?
      wotd_wordnik msg, msg.match[3]?
    else if process.env.WOTD_PROVIDER is "dictionary" and process.env.DICTIONARY_API_KEY?
      wotd_dictionary msg, msg.match[3]?
    else
      msg.send "Missing WOTD_PROVIDER, WORDNIK_API_KEY or DICTIONARY_API_KEY env variable"

wotd_wordnik = (msg, short_response) ->
  msg.http("http://api.wordnik.com/v4/words.json/wordOfTheDay")
    .header("api_key", process.env.WORDNIK_API_KEY)
    .get() (err, res, body) ->
      if err?
        lookup_error msg, err
      else
        wotd = JSON.parse(body)
        if wotd.word?
          msg.send "Word of the day: #{wotd.word}"
        if wotd.definitions?
          for def in wotd.definitions
            msg.send "Definition: #{def.text}"
        if not short_response
          if wotd.examples?
            for example in wotd.examples
              msg.send "Example: #{example.text}"
          if wotd.note?
            msg.send "Note: #{wotd.note}"

wotd_dictionary = (msg, short_response) ->
  msg.http("http://api-pub.dictionary.com/v001")
    .query(vid: process.env.DICTIONARY_API_KEY, type: "wotd")
    .get() (err, res, body) ->
      if err?
        lookup_error msg, err
      else
        parser = new Parser
        parser.parseString body, (error, doc) ->
          wotd = doc.entry
          msg.send "Word of the day: #{wotd.word}"
          msg.send "Pronunciation: #{wotd.pronunciation}"
          msg.send "Audio: #{wotd.audio["@"].audioUrl}"
          if short_response
            msg.send "Definition: (#{wotd.partofspeech}) #{wotd.shortdefinition}"
          else
            for def in wotd.definitions.definition
              msg.send "Definition: (#{def.partofspeech}) #{def.data}"
            for example in wotd.examples.example
              msg.send "Example: #{example.quote}"
            msg.send "Note: #{wotd.footernotes}"

lookup_error = (msg, err) ->
  console.log err
  msg.reply "Sorry, there was an error looking up the word of the day"

Something went wrong with that request. Please try again.