Skip to content

Commit

Permalink
Use env variables (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Jan 2, 2020
1 parent c14c396 commit c5f99ac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CHANNEL='#test-bot'
SCHEDULE='*/15 * * * * *'
SLACK_BOT_TOKEN=
TZ=Europe/Paris
REDIS_URL=''
42 changes: 22 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ npm install

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/Narno/leo-bot)

### Slack
#### Token
## Configuration

You need to set environment variables. See `.env.dist`.

You need to set an environment variable:
```bash
export SLACK_BOT_TOKEN="xoxb-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX"
export CHANNEL='#leo'
export SCHEDULE='0 45 17 * * 1-5'
export SLACK_BOT_TOKEN=xoxb-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX
```
> You'll find `SLACK_BOT_TOKEN` at https://my.slack.com/apps/A0F7YS25R-bots.
#### Channel

The bot daily message is sent to:
- `#test-bot` channel on **debug** mode
- `#leo` channel on **production** mode

See `config.json`.

### Reminder

The bot daily message is scheduled at:
- `*/15 * * * * *` on **debug** mode: every 15 seconds
- `0 45 17 * * 1-5` on **production** mode: every opendays at 5:45pm
The cron-style scheduling format consists of:
```
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
```

See `config.json`.
> You'll find `SLACK_BOT_TOKEN` at https://my.slack.com/apps/A0F7YS25R-bots.
## Usage

Expand All @@ -49,7 +49,7 @@ npm run lint
npm run start
```

### Supported commands
### Supported Slack commands

Just ask `help` to `@leo-bot`.

Expand All @@ -60,6 +60,8 @@ Just ask `help` to `@leo-bot`.
```bash
heroku create --buildpack https://github.com/heroku/heroku-buildpack-nodejs.git
heroku addons:create heroku-redis:hobby-dev
heroku config:set CHANNEL='#leo'
heroku config:set SCHEDULE='0 45 17 * * 1-5'
heroku config:set SLACK_BOT_TOKEN=xoxb-XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXX
heroku config:set TZ=Europe/Paris
heroku ps:scale web=0 worker=1
Expand Down
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
}
],
"env": {
"CHANNEL": {
"description": "Slack channel.",
"value": "#leo",
"required": true
},
"SCHEDULE": {
"description": "The cron-style scheduling format.",
"value": "0 45 17 * * 1-5",
"required": true
},
"SLACK_BOT_TOKEN": {
"description": "You'll find it at https://my.slack.com/apps/A0F7YS25R-bots.",
"value": "",
Expand Down
12 changes: 0 additions & 12 deletions config.json

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

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

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "leo-bot",
"version": "1.1.1",
"version": "1.3.0",
"description": "A simple reminder Slack bot.",
"keywords": [
"bot",
Expand All @@ -16,15 +16,15 @@
"email": "[email protected]",
"url": "https://narno.org"
},
"main": "index.js",
"main": "worker.js",
"repository": {
"type": "git",
"url": "https://github.com/Narno/leo-bot.git"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon ./src/index.js --exec 'npm run lint && node'",
"start": "NODE_ENV=production node ./src/index.js",
"dev": "nodemon -r dotenv/config ./src/worker.js --exec 'npm run lint && node'",
"start": "NODE_ENV=production node ./src/worker.js",
"lint": "./node_modules/eslint/bin/eslint.js ./src/"
},
"dependencies": {
Expand All @@ -34,6 +34,7 @@
},
"devDependencies": {
"babel-eslint": "^10.0.3",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-html": "^6.0.0",
Expand Down
41 changes: 21 additions & 20 deletions src/index.js → src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
* @author Arnaud Ligny <[email protected]>
*/

const config = require('../config.json')[process.env.NODE_ENV || 'development']
const schedule = require('node-schedule')
const Schedule = require('node-schedule')
const Redis = require('redis').createClient(process.env.REDIS_URL)
const Botkit = require('botkit')

const debug = process.env.NODE_ENV !== 'production'
const token = process.env.SLACK_BOT_TOKEN
const channel = process.env.CHANNEL
const schedule = process.env.SCHEDULE

// messages
const messageReminder = 'Attention <!channel>, il est temps de s\'organiser pour récupérer Léo !'
const messageHelp = 'Hello, je ne sers que de rappel journalier ! :kissing_heart:\n' +
'- `annule` pour annuler les rappels\n' +
Expand All @@ -15,16 +22,13 @@ const messageReminderCancelConfirm = 'Les rappels ont été annulés :no_bell:'
const messageReminderStartConfirm = 'Rappels programmés :bell:'

if (!token) {
console.log('Error: Specify token in environment')
console.log('Error: Slack token is not defined in environment')
process.exit(1)
}

const Botkit = require('botkit')

const controller = Botkit.slackbot({
debug: config.DEBUG
debug: debug
})

const bot = controller.spawn({
token: token
}).startRTM(function (err, bot, payload) {
Expand All @@ -35,15 +39,13 @@ const bot = controller.spawn({
}
})

const redis = require('redis').createClient(process.env.REDIS_URL)

/**
* Sends a message
* Reminder message
*/
const reminder = function () {
bot.api.chat.postMessage({
token: token,
channel: config.CHANNEL,
channel: channel,
text: messageReminder,
as_user: true
})
Expand All @@ -52,14 +54,14 @@ const reminder = function () {
/**
* Job schedule
*/
let job = schedule.scheduleJob(config.SCHEDULE, reminder)
let job = Schedule.scheduleJob(schedule, reminder)

redis.get('job', function (error, result) {
Redis.get('job', function (error, result) {
if (error) {
console.log(error)
throw error
}
if (config.DEBUG) {
if (debug) {
console.log('debug: REDIS `job` = `' + result + '`')
}
if (result === 'off') {
Expand All @@ -68,7 +70,7 @@ redis.get('job', function (error, result) {
})

/**
* Explain the user how to use the bot
* Explain how to use the bot
*/
controller.hears(['aide', 'help'], 'direct_message,direct_mention,mention', function (bot, message) {
bot.reply(message, messageHelp)
Expand All @@ -83,21 +85,20 @@ const utterances = {
* Cancel the next planned invocations
*/
controller.hears(['annule', 'cancel'], 'direct_message,direct_mention,mention', function (bot, message) {
// bot.reply(message, messageReminderCancel)
bot.startConversation(message, function (err, convo) {
if (!err) {
convo.ask(messageReminderCancelAsk, [
{
pattern: utterances.yes,
callback: function (response, convo) {
job.cancel()
redis.set('job', 'off', redis.print)
Redis.set('job', 'off', Redis.print)
convo.say(messageReminderCancelConfirm)
convo.next()
// message on channel
bot.api.chat.postMessage({
token: token,
channel: config.CHANNEL,
channel: channel,
text: messageReminderCancelConfirm,
as_user: true
})
Expand All @@ -123,8 +124,8 @@ controller.hears(['rappel', 'reminder'], 'direct_message,direct_mention,mention'
bot.startConversation(message, function (err, convo) {
if (!err) {
job.cancel()
job = schedule.scheduleJob(config.SCHEDULE, reminder)
redis.set('job', 'on', redis.print)
job = Schedule.scheduleJob(schedule, reminder)
Redis.set('job', 'on', Redis.print)
convo.say(messageReminderStartConfirm)
convo.next()
}
Expand Down

0 comments on commit c5f99ac

Please sign in to comment.