Added posting to twitter when the plant needs water
This commit is contained in:
parent
104a5faef7
commit
c6369f7795
@ -1,6 +1,7 @@
|
|||||||
// Initialize project
|
// Initialize project
|
||||||
const db = require('./db/db')
|
const db = require('./db/db')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
|
const twit = require('twit')
|
||||||
require('dotenv').config({path: path.join(__dirname, '../.env')})
|
require('dotenv').config({path: path.join(__dirname, '../.env')})
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -22,6 +23,16 @@ app.get('/newreading', (req, res) => {
|
|||||||
db.Settings.findAll().then((result) => {
|
db.Settings.findAll().then((result) => {
|
||||||
// Reset low moisture flag if it's set, and the reading is greater than high-trigger
|
// 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)) {
|
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
|
// Update low to false
|
||||||
db.Settings.update({ value: false }, { where: { key: 'low' } })
|
db.Settings.update({ value: false }, { where: { key: 'low' } })
|
||||||
// Update alert to false
|
// 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 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') {
|
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 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 the first low reading was more than 5 days ago, send an alert
|
||||||
if (lowtriggered > fivedays) {
|
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' } })
|
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')
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -13,7 +13,8 @@
|
|||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"sequelize": "*",
|
"sequelize": "*",
|
||||||
"sqlite3": "*"
|
"sqlite3": "*",
|
||||||
|
"twit": "^2.2.11"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "10.x"
|
"node": "10.x"
|
||||||
|
Loading…
Reference in New Issue
Block a user