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

slack.js (2563B)


      1 /**
      2  * Slack-ChatBot Module
      3  *
      4  * @module Main
      5  * @class slack
      6  * @submodule slack
      7  * @author Marcel Radzio
      8  */
      9 //Load needed Features
     10 var command_config = require("../configs/commands.json");
     11 var params_config = require("../configs/slackServer.json");
     12 var SlackBot = require('slackbots');
     13 var S = require('string');
     14 var _ = require("lodash");
     15 
     16 //Set Params
     17 /**
     18  * Param Botname
     19  *
     20  * @property botname
     21  * @type String
     22  */
     23 var botname = params_config["botname"];
     24 /**
     25  * Param disconnect_meassage
     26  *
     27  * @property disconnect_meassage
     28  * @type String
     29  */
     30 var disconnect_meassage = params_config["disconnect_meassage"];
     31 var session;
     32 _.find(params_config["servers"], function (key) {
     33   serveraddress = key["serveraddress"];
     34   devChannel = key["devChannel"];
     35   token = key["token"];
     36   active = key["active"];
     37   debug = key["debug"];
     38   bot = new SlackBot({
     39     token: token,
     40     name: 'FreifunkBot'
     41   });
     42 });
     43 
     44 function getDateTime() {
     45     var date = new Date();
     46     var hour = date.getHours();
     47     hour = (hour < 10 ? "0" : "") + hour;
     48     var min  = date.getMinutes();
     49     min = (min < 10 ? "0" : "") + min;
     50     var sec  = date.getSeconds();
     51     sec = (sec < 10 ? "0" : "") + sec;
     52     var year = date.getFullYear();
     53     var month = date.getMonth() + 1;
     54     month = (month < 10 ? "0" : "") + month;
     55     var day  = date.getDate();
     56     day = (day < 10 ? "0" : "") + day;
     57     return day + "." + month + "." + year + "  "  + hour + ":" + min + ":" + sec;
     58 }
     59 
     60 module.exports = {
     61   /**
     62  	* Connects to Server
     63  	*
     64  	* @method slackPreload
     65 	*/
     66 	slackPreload: function () {
     67 	},
     68   slackSend: function (channel, message) {
     69     bot.postMessage(channel, message)
     70   }
     71 }
     72 
     73 // more information about additional params https://api.slack.com/methods/chat.postMessage
     74 var params = {
     75     icon_emoji: ':cat:'
     76 };
     77 
     78 bot.on('start', function() {
     79     // define existing username instead of 'user_name'
     80     bot.postMessageToUser('mtrnord', 'Starting at ' + getDateTime() + '!', params);
     81 });
     82 bot.on('message', function(data) {
     83     // all ingoing events https://api.slack.com/rtm
     84     console.log(data);
     85     var textmessage = data["text"];
     86     var channel = data["channel"];
     87     var subtype = data["subtype"];
     88     if (textmessage === null){}else{
     89       textmessage = textmessage.toLowerCase();
     90     }
     91     _.find(command_config["commands"], function (key) {
     92         if (subtype !== "bot_message"){
     93           if (S(textmessage).contains("!" + key["keyword"])) {
     94             bot.postMessage(channel, key["message"], params);
     95           }else {
     96             console.log("");
     97           }
     98         }
     99     });
    100 });