From 108eedd450b6276b66068532494f897584d54331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joona=20Heikkil=C3=A4?= Date: Thu, 29 Jun 2017 17:36:34 +0300 Subject: [PATCH] Read flow token and url from command line params --- package.json | 4 +-- src/config.js | 8 +----- src/flowdock-submitter.js | 25 ++++++------------- src/index.js | 51 ++++++++++++++++++++++----------------- 4 files changed, 40 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index eb9eea3..0d0a7f6 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,8 @@ "private": true, "dependencies": { "bluebird": "3.5.0", - "dotenv": "4.0.0", "jsdom": "11.0.0", - "node-fetch": "1.7.1" + "node-fetch": "1.7.1", + "yargs": "8.0.2" } } diff --git a/src/config.js b/src/config.js index 87ba15f..10ad58b 100644 --- a/src/config.js +++ b/src/config.js @@ -1,9 +1,3 @@ -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, - flowdockFlowTokens: tokens + userAgent: 'Antell Lunch Menu Scraper (github.com/cxcorp/antell-lunchmenu-flowdock)' } diff --git a/src/flowdock-submitter.js b/src/flowdock-submitter.js index b9eedc8..71535e1 100644 --- a/src/flowdock-submitter.js +++ b/src/flowdock-submitter.js @@ -1,39 +1,30 @@ -const Promise = require('bluebird') const fetch = require('node-fetch') -const { userAgent, flowdockFlowTokens, antellMenuUrl } = require('./config') +const { userAgent } = require('./config') const { getTodaysWeekdayInEnglish, formatDateRfc } = require('./util') /** * @param {any} menu * @returns {Promise} flowdock server response */ -function submitMenuToFlowdock(menu) { +function submitMenuToFlowdock(menu, argv) { const fields = menuToFields(menu) const body = getMessageBody(fields) const payload = { - //flow_token: flowdockFlowTokens, // injected below + flow_token: argv.flow, event: 'activity', title: `Today's lunch`, body, author: { name: 'Antell' }, - external_thread_id: getThreadId(), + external_thread_id: getThreadId(argv.url), thread: { title: getThreadTitle(), fields: fields, - external_url: antellMenuUrl + external_url: argv.url }, - tags: ['#lunch_menu'] + tags: argv.tags } - 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) - })) + return submitBodyToFlow(payload) } function submitBodyToFlow(payload) { @@ -66,7 +57,7 @@ function getMessageBody(fields) { return `${fields[0].label}${andMore}` } -function getThreadId() { +function getThreadId(antellMenuUrl) { // just generate some random ID so that the messages don't get grouped return `${antellMenuUrl}?id=${new Date().getTime()}` } diff --git a/src/index.js b/src/index.js index d97a97d..01fc1eb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,35 @@ -require('dotenv').config() +#!/usr/bin/env node const fetch = require('node-fetch') -const { antellMenuUrl, flowdockFlowTokens, userAgent } = require('./config') +const yargs = require('yargs') +const { userAgent } = require('./config') const { getTodaysWeekdayInFinnish } = require('./util') const { parseLunchMenu } = require('./lunch-menu-parser') const { submitMenuToFlowdock } = require('./flowdock-submitter') -exitIfMissingVars() +const yargv = yargs + .usage('$0 [args]') + .option('u', { + alias: 'url', + demandOption: true, + describe: 'url of the lunch list', + type: 'string' + }) + .option('f', { + alias: 'flow', + demandOption: true, + describe: 'flow source token of the target flow', + type: 'string' + }) + .option('t', { + alias: 'tags', + demandOption: false, + describe: 'tags of the message sent', + type: 'array' + }) + .help() + .argv -getLunchMenu().then(weekMenu => { +getLunchMenu(yargv).then(weekMenu => { const key = getTodaysWeekdayInFinnish() if (key === 'Lauantai' || key === 'Sunnuntai') { console.log('Weekend; not posting menu') @@ -19,26 +41,11 @@ getLunchMenu().then(weekMenu => { process.exit(1) } - return submitMenuToFlowdock(menu) + return submitMenuToFlowdock(menu, yargv) }) -function exitIfMissingVars() { - let exit = false - if (flowdockFlowTokens.length < 1) { - exit = true - console.error('FLOWDOCK_FLOW_TOKENS are not set!') - } - if (!antellMenuUrl) { - exit = true - console.error('ANTELL_MENU_URL is not set!') - } - if (exit) { - process.exit(1) - } -} - -function getLunchMenu() { - return fetch(antellMenuUrl, { headers: { 'User-Agent': userAgent } }) +function getLunchMenu(argv) { + return fetch(argv.url, { headers: { 'User-Agent': userAgent } }) .then(body => body.text()) .then(parseLunchMenu) }