Skip to content

Commit

Permalink
Merge pull request github#32 from Aupajo/master
Browse files Browse the repository at this point in the history
Add Dictionary
  • Loading branch information
atmos committed Oct 29, 2011
2 parents b362944 + 5982e63 commit 99dd29e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/scripts/dictionary.coffee
@@ -0,0 +1,34 @@
# Dictionary definitions with the Wordnik API. You'll need an API key from http://developer.wordnik.com/
#
# define me <word> - Grabs a dictionary definition of a word.
module.exports = (robot) ->
robot.respond /define( me)? (.*)/i, (msg) ->

if process.env.WORDNIK_API_KEY == undefined
msg.send "Missing WORDNIK_API_KEY env variable."
return

word = msg.match[2]

msg.http("http://api.wordnik.com/v4/word.json/#{escape(word)}/definitions")
.header('api_key', process.env.WORDNIK_API_KEY)
.get() (err, res, body) ->
definitions = JSON.parse(body)

if definitions.length == 0
msg.send "No definitions for \"#{word}\" found."
else
reply = "Definitions for \"#{word}\":\n"

lastSpeechType = null
definitions = definitions.forEach (def) ->
# Show the part of speech (noun, verb, etc.) when it changes
if def.partOfSpeech != lastSpeechType
reply += " (#{def.partOfSpeech})\n" if def.partOfSpeech != undefined

lastSpeechType = def.partOfSpeech

reply += " - #{def.text}\n"

msg.send reply

0 comments on commit 99dd29e

Please sign in to comment.