Skip to content

Commit

Permalink
Receive commands from WhatsApp
Browse files Browse the repository at this point in the history
Send image with alert
  • Loading branch information
ThatGuyHughesy committed Jun 13, 2019
1 parent b727b44 commit 7cd8864
Show file tree
Hide file tree
Showing 6 changed files with 3,274 additions and 458 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,38 @@ Sign up for [Twilio](https://www.twilio.com/try-twilio) and activate the [Sandbo
Once you have your credentials, create `.env` with the following:

```bash
ACCOUNT_SID=<TWILIO_ACCOUNT_SID_GOES_HERE>
AUTH_TOKEN=<TWILIO_AUTH_TOKEN_GOES_HERE>
WHATSAPP_FROM=<TWILIO_SANDBOX_NUMBER_GOES_HERE>
WHATSAPP_TO=<YOUR_NUMBER_GOES_HERE>
TWILIO_ACCOUNT_SID=<TWILIO_ACCOUNT_SID_GOES_HERE>
TWILIO_AUTH_TOKEN=<TWILIO_AUTH_TOKEN_GOES_HERE>
TWILIO_WHATSAPP_FROM=<TWILIO_SANDBOX_NUMBER_GOES_HERE>
TWILIO_WHATSAPP_TO=<YOUR_NUMBER_GOES_HERE>
NGROK_AUTH_TOKEN=<NGROK_AUTH_TOKEN_GOES_HERE>
NGROK_SUBDOMAIN=<NGROK_SUBDOMAIN_GOES_HERE>
NGROK_URL=<NGROK_URL_GOES_HERE>
NGROK_REGION=<NGROK_REGION_GOES_HERE>
NGROK_USERNAME=<NGROK_USERNAME_GOES_HERE>
NGROK_PASSWORD=NGROK_PASSWORD_GOES_HERE>
SERVER_PORT=<SERVER_PORT_GOES_HERE>
```

Example `.env`

```bash
TWILIO_ACCOUNT_SID="44370743a981sdf18074ee2d7c87"
TWILIO_AUTH_TOKEN="f32dcbf09af4190caab20f3ecc0312"
TWILIO_WHATSAPP_FROM="1410000001"
TWILIO_WHATSAPP_TO="353870000001"
NGROK_AUTH_TOKEN="84shw61df31ud6s_kd73gr9hhd613"
NGROK_SUBDOMAIN="watchdog"
NGROK_URL="https://watchdog.ngrok.io"
NGROK_REGION="us"
NGROK_USERNAME="watchdog"
NGROK_PASSWORD="watchdog"
SERVER_PORT="5000"
```

Run:

$ npm start
$ npm run start

## Testing

Expand Down
37 changes: 37 additions & 0 deletions detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require("dotenv").config();

const { defaultOpts, createWebcam, captureImage } = require("./webcam");
const { createClient, sendAlert } = require("./twilio");
const { loadImage, saveImage, detectFaces, drawDetection } = require("./face");

const webcam = createWebcam(defaultOpts);

const twilioClient = createClient(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);

async function detect() {
captureImage(webcam, "capture", async (err, path) => {
const image = await loadImage(path);
const detections = await detectFaces(image);

if (detections.length > 0) {
const detection = await drawDetection(image, detections);

await saveImage(
`${__dirname}/images/detection.jpg`,
detection.toBuffer("image/jpeg")
);

await sendAlert(twilioClient, {
from: `whatsapp:+${process.env.TWILIO_WHATSAPP_FROM}`,
to: `whatsapp:+${process.env.TWILIO_WHATSAPP_TO}`,
body: "There's someone at your laptop!",
mediaUrl: `${process.env.NGROK_URL}/detection.jpg`
});
}
});
}

module.exports.detect = detect;
4 changes: 2 additions & 2 deletions face.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ async function loadImage(filename) {
return image;
}

function saveImage(filename, buf) {
fs.writeFileSync(filename, buf);
async function saveImage(filename, buf) {
return fs.writeFileSync(filename, buf);
}

async function detectFaces(image) {
Expand Down
70 changes: 47 additions & 23 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
require("dotenv").config();

const { defaultOpts, createWebcam, captureImage } = require("./webcam");
const { loadImage, saveImage, detectFaces, drawDetection } = require("./face");
const { createClient, sendAlert } = require("./twilio");

const webcam = createWebcam(defaultOpts);
const twilioClient = createClient(
process.env.ACCOUNT_SID,
process.env.AUTH_TOKEN
);

captureImage(webcam, "capture", async (err, path) => {
const image = await loadImage(path);
const detections = await detectFaces(image);

if (detections.length > 0) {
sendAlert(twilioClient, {
from: `whatsapp:+${process.env.WHATSAPP_FROM}`,
to: `whatsapp:+${process.env.WHATSAPP_TO}`,
body: "There's someone at your laptop!"
});

const detection = await drawDetection(image, detections);
saveImage("detection.jpg", detection.toBuffer("image/jpeg"));
const express = require("express");
const bodyParser = require("body-parser");
const ngrok = require("ngrok");
const { MessagingResponse } = require("twilio").twiml;
const { CronJob } = require("cron");

const { detect } = require("./detect");

const app = express();
const twiml = new MessagingResponse();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(`${__dirname}/images`));

const detectionJob = new CronJob("*/30 * * * * *", () => {
detect();
});

app.post("/incoming", (req, res) => {
switch (req.body.Body.toLowerCase()) {
case "start":
detectionJob.start();
twiml.message("Watchdog is keeping your laptop safe!");
break;
case "pause":
detectionJob.stop();
twiml.message("Watchdog is no longer keeping your laptop safe!");
break;
default:
twiml.message("This command is invalid.");
}

res.writeHead(200, { "Content-Type": "text/xml" });
res.end(twiml.toString());
});

app.post("/status");

app.listen(process.env.SERVER_PORT);

ngrok.connect({
proto: "http",
inspect: false,
addr: process.env.SERVER_PORT,
subdomain: process.env.NGROK_SUBDOMAIN,
authtoken: process.env.NGROK_AUTH_TOKEN,
region: process.env.NGROK_REGION,
auth: `${process.env.NGROK_USERNAME}:${process.env.NGROK_PASSWORD}`
});
Loading

0 comments on commit 7cd8864

Please sign in to comment.