-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
60 lines (53 loc) · 1.38 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const request = require('request');
const nodemailer = require('nodemailer');
const fs = require('fs');
require('dotenv').config();
const smtpTransport = nodemailer.createTransport({
service: process.env.SERVICE,
auth: {
user: process.env.FROM_EMAIL,
pass: process.env.FROM_EMAIL_PASSWORD
}
});
const options = {
uri: 'https://icanhazdadjoke.com/',
method: 'GET',
headers: {
'Accept': 'text/plain'
}
}
getJoke();
setInterval(getJoke, 3600000);
function getJoke(){
request(options, function (error, response, body) {
if(error) console.log('error:', error);
else {
var canSend = true;
fs.readFileSync('sentjokes.txt').toString().split('\n').forEach(function(line){
if(line==body){
canSend = false;
}
});
if(canSend){
sendText(body);
}
}
});
}
function sendText(joke){
var mailOptions = {
from: process.env.FROM_EMAIL,
to: process.env.TO_EMAIL,
text: joke,
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent.");
}
});
fs.appendFile('sentjokes.txt',joke + "\n", function(){
console.log('Wrote to file.');
});
}