Skip to content

Commit

Permalink
fix: support for multiple channels and companies (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvogt authored Aug 14, 2019
1 parent 56ae463 commit d1fa95c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
24 changes: 17 additions & 7 deletions actions/announce-meal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const buildSlackResponse = require('../utils/build-slack-response');
const isToday = require('../utils/is-today');
const logger = require('../logger');

let todaysLunch = {};
const todaysLunch = {};

const announceMeal = async ({
appState: {
Expand All @@ -14,27 +14,37 @@ const announceMeal = async ({
},
message: { channel: channelId }
}) => {
logger.debug('Action ANNOUNCE_MEAL called.');
logger.debug(`Action ANNOUNCE_MEAL called in channel ${channelId}.`);

const { ZEROCATER_MEALS_URL: mealURLTemplate } = endpoints;
const { name: channelName } = subscribedChannels[channelId];
const { companyId } = sites[channelName];

if (!todaysLunch.meal || (!todaysLunch.timestamp && !isToday(todaysLunch.timestamp))) {
logger.debug('Meal data missing or stale. Fetching new meal data.');
Object.keys(subscribedChannels).forEach(channel => {
if (!todaysLunch[channel]) {
todaysLunch[channel] = {};
}
});

if (!todaysLunch[channelId].meal || !todaysLunch[channelId].timestamp || !isToday(todaysLunch[channelId].timestamp)) {
logger.debug('Missing or stale meal data. Fetching new meal data.');

let meal;

try {
meal = await today(companyId);
logger.debug('Fetched and found meal for today.', meal);
} catch (error) {
logger.debug('Failed to find a meal for today.', {
error
});
}

if (!meal) {
web.chat.postMessage({
as_user: true,
channel: channelId,
text: 'Sorry, I couldn\'t find a meal for today.'
});
return {
action: 'ANNOUNCE_MEAL',
result: 'failure',
Expand All @@ -43,7 +53,7 @@ const announceMeal = async ({
}

const timestamp = new Date();
todaysLunch = meal && {
todaysLunch[channelId] = {
meal,
timestamp
};
Expand All @@ -55,7 +65,7 @@ const announceMeal = async ({
vendor_description: vendorDescription,
vendor_image_url: vendorImageURL,
vendor_name: vendorName
} = todaysLunch.meal;
} = todaysLunch[channelId].meal;

const mealURL = mealURLTemplate.replace('{companyId}', companyId);
const mealsHyperlinkURL = `${mealURL}/${id}`;
Expand Down
11 changes: 9 additions & 2 deletions listeners/query-todays-lunch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ module.exports = ({
const { clients: { rtm } } = appState;
rtm.on('message', async message => {
const {
bot_id: botId,
channel,
text
text,
type,
user
} = message;

const isNotBotUser = Boolean(message.subtype !== 'bot_message');
Expand All @@ -18,6 +21,7 @@ module.exports = ({
const isTrue = result => result === true;
const isInteraction = [
/!lunch/ig.test(text),
/lunch is here/ig.test(text),
/what.{1}s for lunch/ig.test(text),
/what is for lunch/ig.test(text),
/what is today.{1}s lunch/ig.test(text),
Expand All @@ -35,9 +39,12 @@ module.exports = ({
}

tracker.track('New interaction detected.', {
botId,
channel,
listener: 'QUERY_CHANNEL_LUNCH',
text
text,
type,
user
});

const result = await action({ appState, message });
Expand Down
7 changes: 6 additions & 1 deletion listeners/query-todays-lunch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ const getAppState = () => {
};

const mockValidMessage = {
bot_id: 'A0005',
channel: channelFixture.id,
subtype: 'derp',
text: '!lunch',
type: 'message',
user: '10001'
};

Expand Down Expand Up @@ -89,9 +91,12 @@ test.serial('it fires a tracking event for valid interaction messages', async t
[
'New interaction detected.',
{
botId: mockValidMessage.bot_id,
channel: mockValidMessage.channel,
listener: 'QUERY_CHANNEL_LUNCH',
text: mockValidMessage.text
text: mockValidMessage.text,
type: mockValidMessage.type,
user: mockValidMessage.user
}
]
]);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"name": "@lunchly/slack-bot",
"version": "0.3.0",
"description": "A Slack bot that announces meals for your company.",
"version": "0.4.0",
"description": "A Slack bot that posts catered meal announcements.",
"keywords": [
"bots",
"chatbots",
"slack"
"slack",
"zerocater",
"lunch",
"meal",
"announcements"
],
"author": {
"name": "Chris Vogt",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ To run Lunchly, ensure the configuration steps above are completed, and do the f

Lunchly listens for interactions in all channels it is a member of. If a meal is found for that day then the bot will post a message containing details about the meal.

Triggers*: `!lunch`, `what's for lunch`, `what is for lunch`, `what is today's lunch`, `what's for lunch`
Triggers*: `!lunch`, `lunch is here`, `what's for lunch`, `what is for lunch`, `what is today's lunch`, `what's for lunch`

_Apostrophe optional. Case insensitive._

Expand Down

0 comments on commit d1fa95c

Please sign in to comment.