Skip to content

Commit

Permalink
feat: list upcoming events
Browse files Browse the repository at this point in the history
addresses part of #28
  • Loading branch information
profnandaa committed Feb 3, 2019
1 parent 785f841 commit 0a9ea7c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ GH_OAUTH_TOKEN=
MC_BASE_URL=https://xxx.api.mailchimp.com/3.0
MC_LIST_ID=xxxx
MC_API_KEY=xxxx

DEFAULT_EVENT_IMG=https://user-images.githubusercontent.com/261265/52181980-4ccf9500-2809-11e9-811f-9239fff50ed7.png
FACEBOOK_GROUP_URL=https://fb.com/groups/devcnairobi
53 changes: 53 additions & 0 deletions bot/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const isURL = require('validator/lib/isURL');
const firebase = require('../storage/firebase');

// // common functions
const utils = {
validArray(data) {
return Array.isArray(data) && data.length > 0;
},
currentTimestamp() {
return Math.floor(new Date().getTime() / 1000);
},
};

const getEventsElems = (events) => {
const elems = [];
events.forEach((event) => {
const elem = {
title: event.title,
image_url: isURL(String(event.cover_img)) ? String(event.cover_img) : process.env.DEFAULT_EVENT_IMG,
subtitle: event.description,
buttons: [
{
type: 'web_url',
url: isURL(String(event.url)) ? String(event.url) : process.env.FACEBOOK_GROUP_URL,
title: 'View Event',
},
{
type: 'postback',
title: '♡ Interested',
payload: `event:interest:${event.timestamp}`,
},
],
};
elems.push(elem);
});
return elems;
};

const upcomingEvents = (payload, chat) => {
firebase.listEvents(null, (data) => {
if (utils.validArray(data)) {
const elems = getEventsElems(data);
chat.sendGenericTemplate(elems);
}
else {
chat.say('No events to display');
}
});
};

module.exports = {
upcomingEvents,
};
13 changes: 11 additions & 2 deletions bot/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const BootBot = require('bootbot');
const createLogger = require('bunyan').createLogger;

const db = require('../storage/firebase');
const events = require('./events');
const replies = require('./replies');
const User = require('./user');
const db = require('../storage/firebase');
const createLogger = require('bunyan').createLogger;

// load env variables
const env = require('dotenv'); // https://github.com/motdotla/dotenv/issues/114
Expand Down Expand Up @@ -60,6 +62,13 @@ bot.hear([/^RSVP/], (payload, chat) => {
db.eventRSVP(payload.sender.id, 2, chat);
});

/**
* Display a list of upcoming events
*/
bot.hear([/upcoming events/i], (payload, chat) => {
events.upcomingEvents(payload, chat);
});

// listen for postback
bot.on('postback', (payload, chat) => {
// if it's the getting started CTA
Expand Down
12 changes: 12 additions & 0 deletions storage/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,16 @@ module.exports = {
}
});
},

listEvents(timestamp, callback) {
// TODO: add query to get events >= timestamp once
// it is provided
const eventsRef = fb.database().ref('/events/');
eventsRef.once('value').then(snapshot => {
const object = snapshot.val();
// convert to array
const data = Object.values(object);
callback(data);
});
},
};

0 comments on commit 0a9ea7c

Please sign in to comment.