-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14c396
commit c5f99ac
Showing
7 changed files
with
68 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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='' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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": { | ||
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' + | ||
|
@@ -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) { | ||
|
@@ -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 | ||
}) | ||
|
@@ -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') { | ||
|
@@ -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) | ||
|
@@ -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 | ||
}) | ||
|
@@ -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() | ||
} | ||
|