const fetch = require("isomorphic-fetch")
const { imageUrl } = require("./api")

function postMessage(responseUrl, message) {
  fetch(responseUrl, {
    method: "POST",
    headers: {
      "Content-Type": "application/json;charset=UTF-8"
    },  
    body: JSON.stringify(message), 
  }).catch(error => {
    console.error("Could not send to %s", responseUrl, { message, error })
  })
}

const menuAttachment = menu => ({
  title: menu.menulinie + ": " + menu.title,
  text: menu.description,
  imageUrl: imageUrl(menu.menuimage),
  footer: "Guten Appetit!",
})

const menuIncredientAttachment = incredients => ({
  title: "Nährwerte",
  fields: incredients.map(incredient => ({
    title: incredient.name,
    value: `${incredient.amount} ${incredient.unit}`,
    short: true, 
  })),
})

/**
 * Slack Message Builder
 * 
 * @arg reponseOrURL express Response | string
 * @arg text string
 */
const message = (responseOrURL, text) => {
  let message = {
    text
  }
  return {
    attachMenu(menu) {
      var result = this.attach(menuAttachment(menu))
      if (menu.incredients) {
        result.attach(menuIncredientAttachment(menu.incredients))
      }        
      return result
    },

    attach({title, text, imageUrl, footer, fields}) {
      const attachment = {
        title, text, image_url: imageUrl, footer,
        mrkdwn_in: ["title", "text", "fields"],
      }
      if (fields) {
        attachment.fields = fields
      }
      if (message.attachments) {
        message.attachments.push(attachment)
      } else {
        message.attachments = [attachment]
      }
      return this
    },

    sendInChannel() {
      return this.send("in_channel")
    },

    send(destination) {
      if (destination) {
        message.response_type = destination
      }
      console.info(message)
      if (typeof responseOrURL === "string") {
        postMessage(responseOrURL, message)
      } else {
        responseOrURL.body = message
      }
    }
  }
}


function* checkSlackToken(next) {
  if (["Q4p9aXw3H53vy8gIirlBmITm"].indexOf(this.query.token) == -1) {
    this.throw(401, "You are not allowed to ask me anything")
  }
  if (!this.query.user_id) {
    this.throw(401, "No user_id given")
  }
  yield next
}

module.exports = {
  message,
  checkSlackToken,
}
@@ -74,24 +74,21 @@
 /api%22)%0A%0A
-function
+const
  postMes
@@ -83,32 +83,35 @@
 onst postMessage
+ = 
 (responseUrl, me
@@ -117,17 +117,20 @@
 essage) 
-%7B
+=%3E (
 %0A  fetch
@@ -381,14 +381,30 @@
  %7D)%0A
+    throw error%0A
   %7D)%0A
-%7D
+)
 %0A%0Aco
@@ -1879,16 +1879,23 @@
         
+return 
 postMess
@@ -1974,16 +1974,62 @@
 message%0A
+        return Promise.resolve(responseOrURL)%0A
       %7D%0A

Make Slack message send return Promise