Skip to content

Commit

Permalink
Support sending to multiple flows, v1.0.5->v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cxcorp committed Jun 29, 2017
1 parent e31d02d commit 0352149
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env-sample
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 18 additions & 4 deletions src/flowdock-submitter.js
Original file line number Diff line number Diff line change
@@ -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<string>} 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,
Expand All @@ -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
Expand Down Expand Up @@ -61,4 +75,4 @@ function getThreadTitle() {
return `${getTodaysWeekdayInEnglish()}'s lunch menu (${formatDateRfc(new Date())})`
}

module.exports = { submitToFlowdock }
module.exports = { submitMenuToFlowdock }
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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
Expand Down

0 comments on commit 0352149

Please sign in to comment.