 
     langs = getTranslationPath(garbleAmount)
 
-    if text?.length
-
-      request getRequestURI(langs[0], langs[1], text), (error, response, body) ->
-        request getRequestURI(langs[1], langs[2], parseResponse(body)), (error, response, body) ->
-          request getRequestURI(langs[2], langs[3], parseResponse(body)), (error, response, body) ->
-            request getRequestURI(langs[3], langs[4], parseResponse(body)), (error, response, body) ->
-              request getRequestURI(langs[4], langs[5], parseResponse(body)), (error, response, body) ->
-                request getRequestURI(langs[5], langs[6], parseResponse(body)), (error, response, body) ->
-                  msg.send parseResponse(body)
-                  unless SHOW_TRANSLATION_PATH == 'false'
-                    msg.send getTranslationPathLog(langs)
+    Promise.reduce(langs, ((text, languagePair) ->
+      getTranslation(languagePair[0], languagePair[1], text).then (response) ->
+        return parseResponse(response[1])
+    ), text).then (text) ->
+      msg.send text
+      unless DISPLAY_TRANSLATION_PATH == 'false'
+        msg.send getTranslationPathLog(langs)

# an array of languages to translate text through
# wraps LOCAL_LANGUAGE around random assortment of languages
getTranslationPath = (garbleAmount) ->
  _.chain(getRandomLanguages(getLanguageCodes(), garbleAmount))
    .unshift(LOCAL_LANGUAGE)
    .push(LOCAL_LANGUAGE)
    .value()

# a string that indicates what "translation path" the text was passed through
getTranslationPathLog = (languageCodes) ->
  languageNames = _.map languageCodes, (code) -> getLanguageName(code)
  "translation path: #{languageNames.join(' → ')}"

# a URI that satisfies the Yandex API requirements

    text: text
  return "#{TRANSLATE_URI}?#{querystring.stringify(opts)}"

# returns request promise
getTranslation = (fromLang, toLang, text) ->
  request getRequestURI(fromLang, toLang, text)

# a string of translated text
parseResponse = (str) ->
  try
    return JSON.parse(str).text[0]
  catch e
    return str

module.exports = (robot) ->

