Skip to content

Commit

Permalink
Tidy up repo, write proper README
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Davies committed Feb 2, 2017
1 parent 9a3a9a8 commit b558b54
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 16 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules
dist
data
.env
.serverless
.DS_Store
151 changes: 148 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,156 @@
# CanteenBot

Accessing the canteen menu for The News Building is hard. This repo contains a scraper and Slack / Alexa integrations to make life a little easier.
Accessing the canteen menu for The News Building is hard. This repo contains a scraper and various service integrations to make life a little easier.

CanteenBot is built as a series of [Serverless](https://serverless.com/framework/docs/) functions and hosted on AWS Lambda. Each function has at minimum a `handler.js` file, which is Serverless’ entry point.


## Core

The core server accepts requests from service integrations in the format `http://[coreURL]?message_type=TYPE&message_param=PARAM` and returns data accordingly.

Valid message types are:

- `MENU` for requesting a given day’s menu
- `INGREDIENT` for requesting a list of days on which a given ingredient is being served

### Menus

Valid message parameters for the `MENU` type are:

`today` | `tomorrow` | `monday` | `tuesday` | `wednesday` | `thursday` | `friday` | `saturday` | `sunday`

For example:

http://[coreURL]?message_type=MENU&message_param=monday


The response will be JSON in the following format:

```
{
"data": {
"url": "http://5438cpa251hgt.co.uk/canteen-thursday",
"timestamp": "Thu Feb 2 11:00:01 2017",
"day": "Thursday",
"locations": [
{
"menu": "Malayasian market lamb kurma malaysian indian lamb curry with nasi biriyani and asian slaw add on: roti canai\n",
"location": "Global Kitchen"
},
...
]
}
}
```

It contains the following fields:

- `url`: the relevant page of the canteen’s website
- `timestamp`: the timestamp at which the menu was scraped
- `day`: the day of the week the menu is for
- `locations`: an array of `menu` and `location`, which represent the different options available in the canteen that day


### Ingredients

The message parameter for the `INGREDIENT` type must not be blank. If multiple words are passed, only the first will be considered.

For example:

http://[coreURL]?message_type=INGREDIENT&message_param=potato

The current version of CanteenBot does a simple string match, so for example `burgers` may return no results if the menu actually says `burger`.

The response will be JSON in the following format:

```
{
"data": [
"Tuesday"
]
}
```

The `data` field will be an array of all the days on which the given ingredient appears in a menu.


### Errors

Error messages will be JSON in the following format:

```
{
"error": "Both message_type and message_param must be provided."
}
```


## Scraper

The scraper function scrapes the canteen’s website on a regular basis, converts the results to JSON and places the resulting files on a server.

TODO: rewrite this as a Serverless function.


## Slack

TODO
There are currently two Slack functions.

TODO: rewrite these as a single Slack app.


### Slash commands

The `slack` function listens for Slack [slash commands](https://api.slack.com/slash-commands) via POST. It will then query the core server for data accordingly, and return a message formatted for Slack.

All commands must begin with `/canteen`.

Valid menu commands are:

`/canteen today` | `/canteen tomorrow` | `/canteen monday` | `/canteen tuesday` | `/canteen wednesday` | `/canteen thursday` | `/canteen friday` | `/canteen saturday` | `/canteen sunday`

Running `/canteen` alone will default to `/canteen today`.

Valid ingredient commands look like `/canteen ingredient burger`.

You can also run `/canteen help`. The implementation of help commands is delegated to the service integrations rather than being handled by the core server.


### Slack notifier

The `slack-notifier` function posts the current day’s menu to a Slack [incoming webhook](https://api.slack.com/incoming-webhooks) on a schedule.


## Alexa

Find the Alexa skill [here](http://alexa.amazon.co.uk/spa/index.html?#skills/dp/B01M4IGA2S) on the skill store.
The Alexa function makes CanteenBot available via a Amazon Alexa skill.

The prototype Alexa skill can be found [here](http://alexa.amazon.co.uk/spa/index.html?#skills/dp/B01M4IGA2S) on the skill store.

TODO: rewrite this as a Serverless function.


## Serverless

The Serverless configuration is stored in `serverless.yml`.


# Development

While working:

npm start

Build for production:

npm run build

Deployment:

serverless deploy


# Contributing

Issues and pull requests are welcome!
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "canteenbot",
"version": "1.0.0",
"description": "CanteenBot",
"version": "2.0.0",
"description": "Serverless functions to fetch the menu for The News Building’s canteen",
"files": [
"core/handler.js",
"slack/handler.js"
"slack/handler.js",
"slack-notifier/handler.js"
],
"scripts": {
"start": "webpack --watch",
Expand All @@ -13,10 +14,13 @@
"keywords": [
"alexa",
"skill",
"fact"
"slack",
"webhook",
"canteen",
"menu"
],
"author": "Amazon.com",
"license": "Apache-2.0",
"author": "Elliot Davies",
"license": "MIT",
"dependencies": {
"node-fetch": "^1.6.3"
},
Expand Down
11 changes: 6 additions & 5 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ provider:
runtime: nodejs4.3
region: eu-west-1
profile: elliot
# stage: dev
stage: prod

## Service-wide environment variables
environment:
CORE_URL: https://8049upo37k.execute-api.eu-west-1.amazonaws.com/dev/core
# CORE_URL: https://8049upo37k.execute-api.eu-west-1.amazonaws.com/dev/core
CORE_URL: https://3wt5txpgb1.execute-api.eu-west-1.amazonaws.com/prod/core

## Individual parts of the service
functions:
Expand All @@ -36,16 +37,16 @@ functions:
path: /slack
method: post
environment:
# TIMES_TEAM: TqKr8LRrC7yP9eHiC54dznHS
TIMES_TEAM: uK8HKof9vicacg1yD5hiKori
TIMES_TEAM: TqKr8LRrC7yP9eHiC54dznHS
# TIMES_TEAM: uK8HKof9vicacg1yD5hiKori

## Post to Slack on a schedule
slack-notifier:
handler: dist/slack-notifier.handler
events:
- schedule: cron(15 12 * * ? *)
environment:
WEBHOOK_URL: https://hooks.slack.com/services/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
WEBHOOK_URL: https://hooks.slack.com/services/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

## Bundling options
package:
Expand Down
2 changes: 1 addition & 1 deletion slack/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports.handler = (event, context, callback) => {
// Parse arguments
const { token, command, text } = parseCommand(event.body);

if (command !== '/canteen-t1000') { // For testing only, should be /canteen
if (command !== '/canteen') {
sendErrorResponse(callback, `Sorry, the service ${command} isn't supported.`);
return;
}
Expand Down

0 comments on commit b558b54

Please sign in to comment.