From c6369f7795503dc0c61bf601371a2c9e93b09ec7 Mon Sep 17 00:00:00 2001 From: JayWll Date: Fri, 17 Apr 2020 17:08:47 -0600 Subject: [PATCH] Added posting to twitter when the plant needs water --- nodeapp/app.js | 39 +++++++++++++++++++++++++++++++++++---- package.json | 3 ++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/nodeapp/app.js b/nodeapp/app.js index 755cd00..6117b2a 100644 --- a/nodeapp/app.js +++ b/nodeapp/app.js @@ -1,6 +1,7 @@ // Initialize project const db = require('./db/db') const path = require('path') +const twit = require('twit') require('dotenv').config({path: path.join(__dirname, '../.env')}) const express = require('express'); const app = express(); @@ -22,6 +23,16 @@ app.get('/newreading', (req, res) => { db.Settings.findAll().then((result) => { // Reset low moisture flag if it's set, and the reading is greater than high-trigger if (result.find(o => o.dataValues.key === 'low').dataValues.value !== '0' && req.query.v >= parseInt(result.find(o => o.dataValues.key === 'high-trigger').dataValues.value)) { + // Post a tweet about being watered + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + msg.post('statuses/update', { status: 'Thanks to whoever watered me just now!! So moist.' }) + // Update low to false db.Settings.update({ value: false }, { where: { key: 'low' } }) // Update alert to false @@ -37,14 +48,34 @@ app.get('/newreading', (req, res) => { // If the low moisture flag is set but an alert has not yet been sent, determine if an alert is necessarry if (result.find(o => o.dataValues.key === 'low').dataValues.value !== '0' && result.find(o => o.dataValues.key === 'alert').dataValues.value === '0') { const lowtriggered = new Date().getTime() - new Date(result.find(o => o.dataValues.key === 'low').dataValues.value).getTime() - const fivedays = 5 * 24 * 60 * 60 * 1000 + const fivedays = 2 * 24 * 60 * 60 * 1000 // If the first low reading was more than 5 days ago, send an alert if (lowtriggered > fivedays) { - console.log('More than 5 days!') // NEED TO REPLACE THIS WITH ALERT BEING SENT + // Pick a random message from the array below + const tweet = [ + 'I\'m getting pretty thirsty over here, @JayWll', + 'So. Very. Dry. You know I\'m a succulent not a cactus, right @JayWll? 🌵', + 'Hey @JayWll, water me. 🥛', + 'They say a man shall not live by bread alone, and a succulent won\'t live if you don\'t WATER ME @JAYWLL!!1! 🍞🚰💦', + '@JayWll seriously bro. A little water. That\'s all I ask.' + ] + + // Define options for the twitter API + const msg = new twit({ + consumer_key: process.env.CONSUMER_KEY, + consumer_secret: process.env.CONSUMER_SECRET, + access_token: process.env.ACCESS_TOKEN, + access_token_secret: process.env.ACCESS_TOKEN_SECRET + }) + + // Post a tweet + msg.post('statuses/update', { status: tweet[Math.floor(Math.random() * tweet.length)] }) + + // Flag that an alert has been sent db.Settings.update({ value: new Date().toISOString() }, { where: { key: 'alert' } }) - } - } + } else console.log('Low for less than 5 days') + } else console.log('No message needed') }) }); diff --git a/package.json b/package.json index b9164ae..07fea98 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "dotenv": "^8.2.0", "express": "^4.17.1", "sequelize": "*", - "sqlite3": "*" + "sqlite3": "*", + "twit": "^2.2.11" }, "engines": { "node": "10.x"