forked from akylus/KabutR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (140 loc) · 4.03 KB
/
index.js
File metadata and controls
164 lines (140 loc) · 4.03 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const tokenJson = require('./token.json');
const credentials = require('./credentials.json');
const mailTimes = require('./mailTimes.json');
const mailTo = require('./mailTo.json');
const nodemailer = require('nodemailer');
let telegram = require('telegram-bot-api');
let CronJob = require('cron').CronJob;
const botToken = tokenJson.token;
console.log("Listening...");
let mailerConfig = {
service: 'Godaddy',
host: "smtpout.secureserver.net",
secureConnection: true,
port: 587,
auth: {
user: credentials.email,
pass: credentials.password
}
};
let transporter = nodemailer.createTransport(mailerConfig);
let mailOptions = {
from: mailerConfig.auth.user,
to: mailTo.mentor,
cc: mailTo.trainer,
bcc: mailTo.self,
subject: '',
text: ''
};
let api = new telegram({
token: botToken,
updates: {
enabled: true
}
});
const morningReminder = () => {
sendNewMessage('What are the tasks for today?');
}
const eveningReminder = () => {
sendNewMessage('What have you done today?');
}
const sendMail = () => {
if(mailOptions.text !== '') {
console.log("in mail", mailOptions.text);
transporter.sendMail(mailOptions, function (error) {
if (error) {
console.log('error:', error);
sendNewMessage(error.toString());
} else {
sendNewMessage('Sent Successfully!');
}
});
}
else
sendNewMessage('List not updated. Kindly send a mail manually');
mailOptions.subject = '';
mailOptions.text = '';
}
const sendNewMessage = (msg) => {
api.sendMessage({
chat_id : credentials.chatId,
text : msg
})
}
const formatText = (text) => {
text = text.split("\n");
text = text.slice(1,text.length);
let i = 0;
finalText = text.map(listItem => {
return (`${++i}. ${listItem}\n`)
})
finalText = finalText.join("");
return finalText
}
const getCurrentDate = () => {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
today = dd + '/' + mm;
return today;
}
api.on('message', function(message)
{
let textReceived = message.text;
let firstLetter = textReceived.slice(0, 1);
let remainingText = textReceived.slice(1, textReceived.length);
let formattedText = formatText(remainingText);
let date = getCurrentDate();
if(firstLetter === '@') {
finalText = "Here's what I am planning on getting done today: \n" + formattedText + "\nThank you,\nRegards,\nKaustubh."
mailOptions.subject = "Work to do | " + date;
mailOptions.text = finalText.toString();
console.log(mailOptions.subject);
console.log(mailOptions.text);
}
else if(firstLetter === '*') {
finalText = "Here's what I have done today: \n" + formattedText + "\nThank you,\nRegards,\nKaustubh."
mailOptions.subject = "Daily Work Report | " + date;
mailOptions.text = finalText.toString();
console.log(mailOptions.subject);
console.log(mailOptions.text);
}
else if(textReceived === 'ping')
sendNewMessage('pong')
});
var morningReminderJob = new CronJob(
mailTimes.morningReminderTime,
function() {
morningReminder();
},
null,
true,
'Asia/Kolkata'
);
var morningMailJob = new CronJob(
mailTimes.morningMailTime,
function() {
sendMail();
},
null,
true,
'Asia/Kolkata'
);
var eveningReminderJob = new CronJob(
mailTimes.eveningReminderTime,
function() {
eveningReminder();
},
null,
true,
'Asia/Kolkata'
);
var eveningMailJob = new CronJob(
mailTimes.eveningMailTime,
function() {
sendMail();
},
null,
true,
'Asia/Kolkata'
);