freifunk-bot

Modular parser for the hackerspace door status with handlers for multiple services
git clone git://archive.git.mtrnord.blog/MTRNord/freifunk-bot.git
Log | Files | Refs | README | LICENSE

commit f184b6916ebdb3d59d66a9fd01b6ce665fb66b58
parent 61c4faa4c980ada9332bfa9d41242f7f7c92eea2
Author: MTRNord <mtrnord1@gmail.com>
Date:   Fri, 11 Mar 2016 19:58:45 +0100

Add slack integration

Diffstat:
Mconfigs/commands.json | 6+++---
Rconfigs/params.json -> configs/ircServer.json | 0
Aconfigs/slackServer.json | 12++++++++++++
Mhandlers/irc.js | 56+++++++++++++++++++++++++++++++-------------------------
Ahandlers/slack.js | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.js | 9+++++----
Mpackage.json | 6++++--
7 files changed, 157 insertions(+), 34 deletions(-)

diff --git a/configs/commands.json b/configs/commands.json @@ -2,13 +2,13 @@ "commands": [ { "keyword": "help", - "message": "Here is your Help!\nCommand List:\n- !help - Shows this page.\n- !DoorStatus - Shows the actual Door Status in an PM\n- !DoorStatus channel - Shows the actual Door Status in the channel from where it was run\n- !where - Shows the address of the Nordlab e.V.\n- !who - Shows who is allowed to come to the Nordlab e.V.\n- !when - Shows who the Nordlab e.V. Hackerspace usually is open\n- !source - Shows where the source of this bot is.\n- !license - What is the license of the bot?\n- !bug - Tell me the Bugs you have found :)\n- !afk - Tell the chat that you are away\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - List of things that have to be done", + "message": "Here is your Help!\nCommand List:\n- !DoorStatus - Shows the actual Door Status in an PM\n- !DoorStatus channel - Shows the actual Door Status in the channel from where it was run\n- !where - Shows the address of the Nordlab e.V.\n- !who - Shows who is allowed to come to the Nordlab e.V.\n- !when - Shows who the Nordlab e.V. Hackerspace usually is open\n- !source - Shows where the source of this bot is.\n- !license - What is the license of the bot?\n- !bug - Tell me the Bugs you have found :)\n- !afk - Tell the chat that you are away\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - List of things that have to be done", "argument": "1", "target": "from" }, { "keyword": "hilfe", - "message": "Hier ist die Hilfe!\nBefehl-Liste:\n- !hilfe - Zeigt diese Seite an.\n- !DoorStatus - Sendet eine PM mit dem aktuellen Status\n- !DoorStatus channel - Sendet in den Channel wo der Befehl ausgeführt wurde den aktuellen Status\n- !where - Zeigt die Addresse vom Nordlab e.V.\n- !who - Zeigt wer alles in den Nordlab e.V. kommen darf\n- !when - Zeigt wann der Nordlab e.V. Hackerspace geöffnet hat\n- !source - Zeigt wo die Sourcen des Bot's sind.\n- !license - HILFE! Welche Lizenz hat der Bot?\n- !bug - Nenne mir Fehler die du gefunden hast :)\n- !afk - Teile dem Chat deine Abwesendheit mit\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - Dinge die ich noch tun muss", + "message": "Hier ist die Hilfe!\nBefehl-Liste:\n- !DoorStatus - Sendet eine PM mit dem aktuellen Status\n- !DoorStatus channel - Sendet in den Channel wo der Befehl ausgeführt wurde den aktuellen Status\n- !where - Zeigt die Addresse vom Nordlab e.V.\n- !who - Zeigt wer alles in den Nordlab e.V. kommen darf\n- !when - Zeigt wann der Nordlab e.V. Hackerspace geöffnet hat\n- !source - Zeigt wo die Sourcen des Bot's sind.\n- !license - HILFE! Welche Lizenz hat der Bot?\n- !bug - Nenne mir Fehler die du gefunden hast :)\n- !afk - Teile dem Chat deine Abwesendheit mit\n- !freifunk - Freifunk Memes \\(^^)/\n- !todo - Dinge die ich noch tun muss", "argument": "1", "target": "from" }, @@ -44,7 +44,7 @@ }, { "keyword": "bug", - "message": "You have found an bug? :O Send it right now to https://github.com/MTRNord/nordlab-hackerspace-door/issues", + "message": "You have found a bug? :O Send it right now to https://github.com/MTRNord/nordlab-hackerspace-door/issues", "argument": "0", "target": "from" }, diff --git a/configs/params.json b/configs/ircServer.json diff --git a/configs/slackServer.json b/configs/slackServer.json @@ -0,0 +1,12 @@ +{ + "disconnect_meassage": "Bot was stopped!", + "servers": [ + { + "servername": "MTRNord", + "devChannel": "#bottest", + "token": "xoxb-26176005733-2y9kszxvi0T8isJlwwg1tx9k", + "active": 1, + "debug": 0 + } + ] +} diff --git a/handlers/irc.js b/handlers/irc.js @@ -9,8 +9,9 @@ //Load needed Features var irc = require('irc'); var request = require('request'); +var S = require('string'); var command_config = require("../configs/commands.json"); -var params_config = require("../configs/params.json"); +var params_config = require("../configs/ircServer.json"); //Set Params /** @@ -80,8 +81,8 @@ for (var key in params_config["servers"]) { floodProtection: true, floodProtectionDelay: 1000, }); + clients.push(bot[key]); } - clients.push(bot[key]); } } @@ -134,24 +135,24 @@ module.exports = { * @method ircPreload */ ircPreload: function () { - addListener('error', function(message) { - setTimeout(function() { - console.log("wait 7sek"); - }, 7000); + addListener('error', function(message) { + setTimeout(function() { + console.log("wait 7sek"); + }, 7000); - send('nick', botname); - for (var key in params_config["servers"]) { - if (params_config["servers"].hasOwnProperty(key)) { - /** - * Password for the NickServ - * - * @property nickserv_pass - * @type String - */ - var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; - say('NickServ', 'identify ' + nickserv_pass); - } - } + send('nick', botname); + for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + /** + * Password for the NickServ + * + * @property nickserv_pass + * @type String + */ + var nickserv_pass = params_config["servers"][key]["nickserv_pass"]; + say('NickServ', 'identify ' + nickserv_pass); + } + } }); setTimeout(function() { console.log("wait 7sek"); @@ -239,8 +240,7 @@ module.exports = { message = message.toLowerCase(); for (var key in command_config["commands"]) { if (command_config["commands"].hasOwnProperty(key)) { - console.log("!" + JSON.stringify(command_config["commands"][key]["keyword"])); - if (message == ("!" + command_config["commands"][key]["keyword"])) { + if (S(message).contains("!" + command_config["commands"][key]["keyword"])) { if (command_config["commands"][key]["before"] == "from") { if (command_config["commands"][key]["target"] == "from") { say(from, from + command_config["commands"][key]["message"]); @@ -275,14 +275,14 @@ module.exports = { */ var channel = message.split(" "); channel = cleanArray(channel); - if (channel[0] + " " + channel[1] == "!source this") { + if (S(channel[0] + " " + channel[1]).contains("!source this")) { console.log("!source " + channel[1]); if (channel[1] == "this") { join(to); say(to, "You can find the Source of this bot at https://github.com/MTRNord/nordlab-hackerspace-door"); } } - if (message == "!doorstatus") { + if (S(message).contains("!doorstatus")) { request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { if (!error && response.statusCode == 200) { /** @@ -316,7 +316,7 @@ module.exports = { } }).setMaxListeners(0); } - if (channel[0] + " " + channel[1] == "!doorstatus this") { + if (S(channel[0] + " " + channel[1]).contains("!doorstatus this")) { request.get('http://www.nordlab-ev.de/doorstate/status.txt', function (error, response, body) { if (!error && response.statusCode == 200) { /** @@ -371,7 +371,7 @@ module.exports = { } }).setMaxListeners(0); } - if (message == "!kill") { + if (S(message).contains("!kill")) { if ((from == "DasNordlicht") || (from == "MTRNord")) { console.log(from + ' => ' + to + ': ' + message); if (process.platform === "win32") { @@ -384,6 +384,12 @@ module.exports = { } } } + if (S(message).contains("!join")) { + if ((from == "DasNordlicht") || (from == "MTRNord")) { + console.log(from + ' => ' + to + ': ' + message); + join(channel[1]); + } + } }) }, /** diff --git a/handlers/slack.js b/handlers/slack.js @@ -0,0 +1,102 @@ +/** + * Slack-ChatBot Module + * + * @module Main + * @class slack + * @submodule slack + * @author Marcel Radzio + */ +//Load needed Features +var command_config = require("../configs/commands.json"); +var params_config = require("../configs/slackServer.json"); +var SlackBot = require('slackbots'); +var S = require('string'); + +//Set Params +/** + * Param Botname + * + * @property botname + * @type String + */ +var botname = params_config["botname"]; +/** + * Param disconnect_meassage + * + * @property disconnect_meassage + * @type String + */ +var disconnect_meassage = params_config["disconnect_meassage"]; +var session; +for (var key in params_config["servers"]) { + if (params_config["servers"].hasOwnProperty(key)) { + var serveraddress = params_config["servers"][key]["serveraddress"]; + var devChannel = params_config["servers"][key]["devChannel"]; + var token = params_config["servers"][key]["token"]; + var active = params_config["servers"][key]["active"]; + var debug = params_config["servers"][key]["debug"];// create a bot + var bot = new SlackBot({ + token: token, + name: 'FreifunkBot' + }); + } +} +function getDateTime() { + var date = new Date(); + var hour = date.getHours(); + hour = (hour < 10 ? "0" : "") + hour; + var min = date.getMinutes(); + min = (min < 10 ? "0" : "") + min; + var sec = date.getSeconds(); + sec = (sec < 10 ? "0" : "") + sec; + var year = date.getFullYear(); + var month = date.getMonth() + 1; + month = (month < 10 ? "0" : "") + month; + var day = date.getDate(); + day = (day < 10 ? "0" : "") + day; + return day + "." + month + "." + year + " " + hour + ":" + min + ":" + sec; +} +module.exports = { + /** + * Connects to Server + * + * @method slackPreload + */ + slackPreload: function () { + } +} + +// more information about additional params https://api.slack.com/methods/chat.postMessage +var params = { + icon_emoji: ':cat:' +}; + +bot.on('start', function() { + // define existing username instead of 'user_name' + bot.postMessageToUser('mtrnord', 'Starting at ' + getDateTime() + '!', params); +}); +bot.on('message', function(data) { + // all ingoing events https://api.slack.com/rtm + console.log(data); + var textmessage = data["text"]; + var channel = data["channel"]; + var subtype = data["subtype"]; + if (textmessage != null){ + textmessage = textmessage.toLowerCase(); + } + // if (S(textmessage).contains('!help')) { + // console.log(channel); + // bot.postMessage(channel, 'help!', params); + // } + for (var key in command_config["commands"]) { + if (command_config["commands"].hasOwnProperty(key)) { + if (subtype != "bot_message"){ + if (S(textmessage).contains("!" + command_config["commands"][key]["keyword"])) { + bot.postMessage(channel, command_config["commands"][key]["message"], params); + }else { + console.log(""); + } + } + } + } +}); diff --git a/main.js b/main.js @@ -1,6 +1,6 @@ /** * Main-functionality for handling the Modules :) - * + * * @main Main * @module Main * @author Marcel Radzio @@ -11,12 +11,13 @@ var git = require('simple-git'); var schedule = require('node-schedule'); var argv = require('yargs').argv; var fs = require('fs'); -var params_config = require("./configs/params.json"); +var params_config = require("./configs/ircServer.json"); var autoupdate = params_config["autoupdate"]; //LOAD MODULES DOWN HERE var pushbullet = require('./handlers/pushbullet.js'); var irc = require('./handlers/irc.js'); +var slack = require('./handlers/slack.js'); //Constants var SIGINT = "SIGINT"; @@ -25,7 +26,7 @@ var door_status2 = "1"; //Startup irc.ircPreload(); - + /** * Get door-status from the Web * @@ -112,7 +113,7 @@ process.on(SIGINT, function () { //Activate IRC-Bot Command Handler irc.ircBotCommands(); - + /** * Update - Pull last master from Github * diff --git a/package.json b/package.json @@ -20,11 +20,13 @@ "notification" ], "dependencies": { - "request": "*", "curlrequest": "*", "irc": "0.4.0", - "simple-git": "*", "node-schedule": "*", + "request": "*", + "simple-git": "*", + "slackbots": "^0.5.1", + "string": "^3.3.1", "yargs": "*" }, "preferGlobal": false,