An open API service indexing awesome lists of open source software.

https://github.com/thundergolfer/library-management-slack-bot

:books: Slack bot that helps facilitate tracking of books and borrowers in your office/home library
https://github.com/thundergolfer/library-management-slack-bot

aws-lambda jsonnet slack-bot typescript

Last synced: 11 months ago
JSON representation

:books: Slack bot that helps facilitate tracking of books and borrowers in your office/home library

Awesome Lists containing this project

README

          

# Library Management Slack Bot [![Build Status](https://travis-ci.com/thundergolfer/library-management-slack-bot.svg?branch=master)](https://travis-ci.com/thundergolfer/library-management-slack-bot)

Slack bot that helps facilitate tracking of books and borrowers in your office/home library. This bot was built to support the office library @ [Canva Sydney](https://www.canva.com/careers/), but is general in nature and with simple hosting on [AWS Lambda](https://aws.amazon.com/lambda/) and [Google Sheets](https://www.twilio.com/blog/2017/03/google-spreadsheets-and-javascriptnode-js.html) can solve the same problems in your office.

-----

## How It Works

This bot exists to manage an index of physical books in an office library, and
also track borrows and returns of the library users. Via simple Slack interactions,
users can search for available books, record their borrow, and record their return.

### Commands

> *Note:* This is currently an MVP. In future, the bot will *not* require the user manually inputting an ISBN.

##### **[@librarybot](/README.md) (with a scanned image of the book's barcode)** - Borrow, add, or return a book without having to manually type its ISBN.
##### **[@librarybot](/README.md) ``** - Borrow, add, or return a book with the ISBN of value ``
##### **[@librarybot](/README.md) `list`** - List all books registered in the library database
##### **[@librarybot](/README.md) `borrow`** - List all books _you_ have borrowed.
##### **[@librarybot](/README.md) `search ""`** - Search books

## Development

### Prerequisites

- **Access to API Gateway and Lambda on the AWS Account where this bot is deployed**
- **Jsonnet** ([`brew install jsonnet`](https://formulae.brew.sh/formula/jsonnet))

### Installation

`npm install`

### Configuration

Copy `.env.default` to `.env` and fill in the slack tokens and google sheet id.

### Testing

#### Unit

All unit tests are contained in [`tests/`](tests/). Run them with:

`npm run test`

#### Integration: `serverless` Local Testing

Using `serverless invoke local -f -p ` we can test the Lambda locally
before deploying to AWS.

Currently the function name is `hello` and the JSON payloads live in [`tests/data/`](/tests/data). The payloads
are generated by _Jsonnet_ (see [**Generating Mock Event Payloads**](#generating-mock-event-payloads)).

For example this command will execute a borrow against the [development Google Sheets DB](https://docs.google.com/spreadsheets/d/1Vbvys2uiSyJWPKsFWjMyHeZ-1mTWDTZCyeFfYCkemuQ/edit#gid=0):

`npx serverless invoke local -f hello -p tests/data/book_message.json`

It won't actually send a message to Slack. It will just `console.log` what would be sent 😊.

#### Generating Mock Event Payloads

From repository root, run `jsonnet -m tests/data tests/data/jsonnet/api_gateway_base.jsonnet`. Valid AWS API Gateway payloads
*containing* Slack Event API payloads as strings (`event.body`) will be generated in `test/data`.

## Running Locally

To start the server locally run:

`npx serverless offline`

To create an external URL accessible from slack run:

```
npm install -f localtunnel
lt --port 3000 --subdomain
```

1) [Create a Slack app](https://api.slack.com/apps)
2) Enable event subscriptions, with the URL from localtunnel
3) Create a bot user
4) Install the app with the `chat:write:user` scope
5) In the slack workspace you installed to, message the bot with `@ list`

## Deployment

Currently deployment involves manually uploading the `.zip` artifact for the
function at [https://console.aws.amazon.com/lambda/](https://console.aws.amazon.com/lambda/).
This can be automated using **Serverless**, and will be soon. For the moment:

1. `npm run package` (creates `library-management-slack-bot.zip` in `artifact/`)
2. Go to AWS Lambda console and upload `.zip` for `slack-library`