Skip to content

BotKit Slack bot. Show free meeting rooms, book rooms (Google Calendar API) etc.

License

Notifications You must be signed in to change notification settings

gerwinkramer/office-slack-bot

 
 

Repository files navigation

Slack Bot

Slack bot for office use.

  • Send a message anonymously
  • Is there anyone at the office
  • Office temperature
  • List free meeting rooms
  • List next meeting room reservations
  • Book a meeting room
  • Cancel a booking made with the SlackBot
  • Suggest a lunch place
  • Suggest a beer place
  • Send an email to a predefined address
  • Show current bitcoin price
  • Show current channel user activity statistics
  • Automatically translate conversation to selected language
  • WebScraping

Get Started

  1. Create new configuration file (src/configuration.js)
    • Copy template from configuration file section
  2. Create a new App for your Slack workspace
    • Add App's token to configuration's botToken
  3. Run bot e.g. with forever, supervisord etc.
    • $ forever start src/app.js

Functionality

Bot sends a reply to the channel or to private chat where the command was sent from.

Sends exceptions and errors to the user defined in the configuration file (slackAdminUserId).

Anonymous message

Anonyous messages are sent to the home channel defined in the configuration file (homeChannelId)

Anonymous message

Anyone at the office

Returns Office has people or Office is empty message, depending if there is currently people at the office.

GET /api/haspeople/{id} from Sensordata API for all sensors in the configuration file. If any of the requests return true, then there is someone at the office.

Get Temperture

Returns lates sensors data for all sensors defined in the configuration file.

GET /api/data/{id} from Sensordata API.

{"name":"5krs","temperature":21.25,"humidity":23,"noise":47,"light":124,"time":"11:47 19.01."}
{"name":"6krs","temperature":22.64,"humidity":23,"noise":45,"light":571,"time":"11:47 19.01."}

Free meeting rooms, current events & book a room

Free meeting rooms shows a list of rooms that are free and duration how long they are available. Current events shows a list of next 2 events for each calendar defined in the configuration file. It will hide info from private events. Book a room makes a reservation for the next X minutes if that time is available. Bookings made with the SlackBot can also be cancelled by the same user.

Book a meeting room

Uses Google Calendar API. Meeting room calendars are defined in the configuration file. Booker info from Slack User Info.

Requirements:

  • Execute Step 1: Turn on the Google Calendar API from quicksart. Save file as client_secret.json
  • Execute npm run create_token to store authentication token to json-file. This file is not in version control
  • Both files need to be in the workspace root

Suggest a lunch or a beer place

Get list of restaurants/bars from Google Places API that are max 500m/800m from the office and return random item from that list. Office location is defined in the configuration file.

Suggest a lunch place

Requirements:

Translate service

Automatically translate the conversation to selected language.

Translate

Requirements:

translator: {
    keyPath: '/home/user123/office-slack-bot/translate_secret.json',
    dbStringPath: '/home/my-user/office-slack-bot/db_config.json',
    prefix: ':flag-england: ',
    language: 'en',            
    maxCharacters: 1000,
    // Translated channels are no longer configured here, set up a database instead
    channels: {
        aaaaa: {
            enabled: true
        },
        xxxxx: {
            enabled: true
        }
    }
}

Bot adds a channel to the channel list when translate command is issued from the channel.

Send email

Nodemailer is used to sen email. Add mailConfig to the configuration file. Will send email to defined email address and cc to sender. Add information to the configuration's emailMessage.

Bot's source code has an implementation to send an email to the maintenane company.

Requirements:

WebScraping

Uses Cheerio to scrape text from html. Selector is executed with eval it's result is shown to user. Use helpers\cheerioTester.js to verify selector.

reddit : {
    url: 'https://www.reddit.com/r/all',
    description: 'Top link from Reddit all',
    selector: `const link = $('#siteTable').find('a').first().attr('href'); link.startsWith('http') ? link : 'https://www.reddit.com' + link;`
}

Files

  • src/app.js
    • Slack Botkit related communication
  • src/consoleApp.js
    • Console application for testig
  • src/bot.js
    • Logic for executing correct functionality
  • src/configuration.js
    • Required configuration for the application. Not in version control.
    • Add this file manually
  • src/calendarServices.js
    • Google Calendar integration
  • src/emailSender.js
    • Email sending functionality
  • src/googlePlacesService.js
    • Google Places API integration

ConsoleApp for testing

Console app wraps the same functionality as BotKit, so it works with same commands and returns same responsens.

$ npm run console

Configuration file

configuration.js containts tokens, passwords, locations, sensors etc.

If allowGuestsToUse is set to false, then restricted users will get an error messages when trying to send messages to the bot.

configuration.js:

'use strict';

module.exports = {
  botToken: 'xxxx',
  homeChannelId: 'xxxx',
  slackAdminUserId: 'xxxx',
  allowGuestsToUse: false,
  apiUserName: 'xxxx',
  apiPassword: 'xxxx',
  apiUrl: 'xxxx',
  locationApiKey: 'xxxx',
  office: { lat: 60.17, lon: 24.94 },
  sensors: [
    { id: 'xxx', name: 'xx' },
    { id: 'xxx', name: 'xx' }
  ],
  meetingRooms: [
    { name: 'xxxx', id: 'xxxx' }
  ],
  translator: {
    keyPath: '/home/my-user/office-slack-bot/translate_secret.json',
    dbStringPath: '/home/my-user/office-slack-bot/db_config.json',
    prefix: ':flag-england: ',
    language: 'en',
    maxCharacters: 1000,
    // Translated channels are no longer configured here, set up a database instead
    channels: { 
      AAAA: {
        enabled: true
      },
      BBBB: {
        enabled: true
      }
    }
  },
  webScraperOptions: {
    reddit: {
      url: 'https://www.reddit.com/r/all',
      description: 'Top link from Reddit all',
      selector: `const link = $('#siteTable').find('a').first().attr('href'); link.startsWith('http') ? link : 'https://www.reddit.com' + link;`
    },
    vincit_kurssi: {
      url: 'https://www.kauppalehti.fi/5/i/porssi/porssikurssit/osake/index.jsp?klid=2073',
      description: 'Vincit stock value',
      selector: `$('.stock_number').text() + "€";`
    },
    hs_top: {
      url: 'https://www.hs.fi/',
      description: 'Most read link from Helsingin Sanomat',
      selector: `'https://www.hs.fi/' + $('.is-most-read-articles-list').find('a').first().attr('href');`
    }
  },
  emailConfig: {
    service: "gmail",
    host: "smtp.gmail.com",
    auth: {
      user: "xxx@gmail.com",
      pass: "xxxxx"
    }
  },
  emailMessage: {
    receiver: 'test@test.com',
    subject: 'Maintenance request',
    template: `
Hi,

{content}

Br,
{senderName}
`
  }
};

MongoDB Database

MongoDB database needs to be set up. Free MongoDB hosting for example mLab will do.

  1. Set up a cluster
  2. Create a new database and a collection. Bot expects both the database and the collection to be named translate_channels.
  3. Create a database user and whitelist your bot's IP address
  4. Get a connection string and insert it into your dbStringPath file e.g. :
{
  "connectString": "mongodb+srv://<username>:<password>@<rest-of-connect-string>"
}

Deploying

This readme will cover deploying the bot in Google Cloud in a Docker container.

  1. Create a new project in Google Cloud
  2. Enable Container Registry for the Docker image
  3. Enable Compute Engine for running the image
  4. Set up Container Registry, build a docker image and push it to Container Registry
  5. Set up VM Instance (with Container Optimized OS) and use your Docker image as a Container image

Tests

Test folder contains tests. Some tests require correct Google API keys in the configuration file.

$ npm test

Run test matchin the pattern:

$ npm run test:g [pattern]

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Licensed under the MIT License.

About

BotKit Slack bot. Show free meeting rooms, book rooms (Google Calendar API) etc.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 99.1%
  • Dockerfile 0.9%