diff --git a/.env-sample b/.env-sample index c910ffc..886b51c 100644 --- a/.env-sample +++ b/.env-sample @@ -1,2 +1,2 @@ -FLOWDOCK_FLOW_TOKEN=abcdefghijklmnopqrstuvwxyz123456 +FLOWDOCK_FLOW_TOKENS=abcdefghijklmnopqrstuvwxyz123456,abcdefghijklmnopqrstuvwxyz123456,abcdefghijklmnopqrstuvwxyz123456 ANTELL_MENU_URL=http://www.antell.fi/lounaslistat/lounaslista.html?owner=112 diff --git a/README.md b/README.md index 514426a..1e48ce5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ This script makes use of environmental variables to configure the flowdock sourc | Variable name | Value | |---------------|-------| | `ANTELL_MENU_URL` | The address of the lunch menu, e.g. http://www.antell.fi/lounaslistat/lounaslista.html?owner=112 | -| `FLOWDOCK_FLOW_TOKEN` | Comma separated array of the tokens of the flows to which you want lunch menus sent | +| `FLOWDOCK_FLOW_TOKENS` | Comma separated array of the tokens of the flows to which you want lunch menus sent | ## License antell-lunchmenu-flowdock is licensed under the MIT license. See LICENSE. diff --git a/package.json b/package.json index ed3eefe..eb9eea3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "antell-localhost-flowdock", - "version": "1.0.5", + "version": "1.1.0", "description": "Flowdock integration for the Antell lunch menu", "main": "src/index.js", "scripts": { @@ -11,6 +11,7 @@ "license": "MIT", "private": true, "dependencies": { + "bluebird": "3.5.0", "dotenv": "4.0.0", "jsdom": "11.0.0", "node-fetch": "1.7.1" diff --git a/src/config.js b/src/config.js index 99ee37a..87ba15f 100644 --- a/src/config.js +++ b/src/config.js @@ -1,5 +1,9 @@ +const tokens = process.env.FLOWDOCK_FLOW_TOKENS + ? process.env.FLOWDOCK_FLOW_TOKENS.split(',') + : [] + module.exports = { userAgent: 'Antell Lunch Menu Scraper (github.com/cxcorp/antell-lunchmenu-flowdock)', antellMenuUrl: process.env.ANTELL_MENU_URL, - flowdockFlowToken: process.env.FLOWDOCK_FLOW_TOKEN + flowdockFlowTokens: tokens } diff --git a/src/flowdock-submitter.js b/src/flowdock-submitter.js index 1f146aa..b9eedc8 100644 --- a/src/flowdock-submitter.js +++ b/src/flowdock-submitter.js @@ -1,16 +1,17 @@ +const Promise = require('bluebird') const fetch = require('node-fetch') -const { userAgent, flowdockFlowToken, antellMenuUrl } = require('./config') +const { userAgent, flowdockFlowTokens, antellMenuUrl } = require('./config') const { getTodaysWeekdayInEnglish, formatDateRfc } = require('./util') /** * @param {any} menu * @returns {Promise} flowdock server response */ -function submitToFlowdock(menu) { +function submitMenuToFlowdock(menu) { const fields = menuToFields(menu) const body = getMessageBody(fields) const payload = { - flow_token: flowdockFlowToken, + //flow_token: flowdockFlowTokens, // injected below event: 'activity', title: `Today's lunch`, body, @@ -23,6 +24,19 @@ function submitToFlowdock(menu) { }, tags: ['#lunch_menu'] } + + return Promise.all(flowdockFlowTokens.map((token, i) => { + const payloadWithToken = Object.assign( + {}, + payload, + { flow_token: token } + ) + console.log(`Submitting lunch list to flow #${i}`) + return submitBodyToFlow(payloadWithToken) + })) +} + +function submitBodyToFlow(payload) { const headers = { 'Content-Type': 'application/json', 'User-Agent': userAgent @@ -61,4 +75,4 @@ function getThreadTitle() { return `${getTodaysWeekdayInEnglish()}'s lunch menu (${formatDateRfc(new Date())})` } -module.exports = { submitToFlowdock } +module.exports = { submitMenuToFlowdock } diff --git a/src/index.js b/src/index.js index 9fd27d2..d97a97d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ require('dotenv').config() const fetch = require('node-fetch') -const { antellMenuUrl, flowdockFlowToken, userAgent } = require('./config') +const { antellMenuUrl, flowdockFlowTokens, userAgent } = require('./config') const { getTodaysWeekdayInFinnish } = require('./util') const { parseLunchMenu } = require('./lunch-menu-parser') -const { submitToFlowdock } = require('./flowdock-submitter') +const { submitMenuToFlowdock } = require('./flowdock-submitter') exitIfMissingVars() @@ -19,14 +19,14 @@ getLunchMenu().then(weekMenu => { process.exit(1) } - return submitToFlowdock(menu) + return submitMenuToFlowdock(menu) }) function exitIfMissingVars() { let exit = false - if (!flowdockFlowToken) { + if (flowdockFlowTokens.length < 1) { exit = true - console.error('FLOWDOCK_TOKEN is not set!') + console.error('FLOWDOCK_FLOW_TOKENS are not set!') } if (!antellMenuUrl) { exit = true