philsco
Published

McHomeBot - Slack bot posting Webcam snapshots

A Slack bot running on your home machine and able to post Webcam snapshots and the IP of your home computer to a Slack channel.

BeginnerProtip4 hours1,258
McHomeBot - Slack bot posting Webcam snapshots

Story

Read more

Code

McHomeBot

JavaScript
Slack bot leveraging the Slack API as well as BotKit - running on NodeJS
const Botkit = require('botkit');
const builder = require('botbuilder');
const os = require('os');
const http = require('http');
const fileSystem = require('fs')
const path = require('path');
const fs = require('fs');
const request = require('request');

if (!process.env.mchomebot) {
    console.log('Error: Specify token in environment');
    process.exit(1);
}

var controller = Botkit.slackbot({
    debug: true
});

var bot = controller.spawn({
    token: process.env.mchomebot
}).startRTM();

controller.hears(['hello', 'hi', 'who are you', 'help'], 'direct_message,direct_mention,mention', function(bot, message) {
    bot.api.reactions.add({
        timestamp: message.ts,
        channel: message.channel,
        name: 'robot_face',
    }, function(err, res) {
        if (err) {
            bot.botkit.log('Failed to add emoji reaction :(', err);
        }
    });

    bot.reply(message, 'Hi. I am a proxy to your home computer.' )
});

controller.hears(['call me (.*)', 'my name is (.*)'], 'direct_message,direct_mention,mention', function(bot, message) {
    var name = message.match[1];
    controller.storage.users.get(message.user, function(err, user) {
        if (!user) {
            user = {
                id: message.user,
            };
        }
        user.name = name;
        controller.storage.users.save(user, function(err, id) {
            var userCount = controller.storage.users.all(function (data) {
              console.dir(data);
            });
            bot.startConversation(message,function(err,convo) {
              console.log(controller.storage.users)

              convo.say('Got it. I will call you ' + user.name + ' from now on.');
            });
        });
    });
});


controller.hears(['Home IP'],'direct_message,direct_mention,mention', function(bot, message) {
    http.get({'host': 'api.ipify.org', 'port': 80, 'path': '/'}, function(resp) {
        resp.on('data', function(ip) {
            var replyMess = 'Your Home IP address is: '+ip.toString('utf8');
              bot.reply(message, replyMess);
        });
        resp.on('error', function (err) {
              bot.reply(message, "Experienced trouble getting the IP. Please try again.");
        })
    });
});

controller.hears(['Home Cam'],'direct_message,direct_mention,mention', function(bot, message) {
    bot.reply(message, 'OK, give me a few seconds to access your home camera.' )
    var spawn = require("child_process").spawn,child;
    var output = "";
    child = spawn("powershell.exe",[".\\RobotEyez.exe /width 640 /height 480 /bmp"]);
    child.stdout.on("data",function(data){
        output += data;
    });
    child.stderr.on("data",function(data){
        output += data;
        console.log("Powershell Errors: " + data);
    });
    child.on("exit",function(){
        errState = false;
        console.log("Powershell Script finished");
    });
    child.on('close', (code) => {
        console.log(`child process exited with code ${code}`);
        if (output.match(/frame\.bmp/)) {
            request.post({
                url: 'https://slack.com/api/files.upload',
                formData: {
                    token: bot.config.token,
                    title: "Home Cam1",
                    filename: "frame.bmp",
                    filetype: "bmp",
                    channels: message.channel,
                    file: fs.createReadStream('frame.bmp'),
                },
            }, function (err, response) {
                console.log(JSON.parse(response.body));
            });
        } else {
            bot.reply(message, "Experienced trouble retrieving the image.");
        }
    });
    child.stdin.end(); 
});

Credits

philsco

philsco

2 projects • 1 follower

Comments