-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.js
142 lines (121 loc) · 3.74 KB
/
emails.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
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
const fs = require('fs');
const moment = require('moment');
const path = require('path');
const HTML5ToPDF = require('html5-to-pdf');
const baseFilePath = '/var/tmp/emails';
const run = async (inputBody, outputFilename) => {
const html5toPDF = new HTML5ToPDF({
inputBody: inputBody,
outputPath: path.join(__dirname, 'tmp', outputFilename),
renderDelay: 50,
});
await html5toPDF.start();
await html5toPDF.build();
await html5toPDF.close();
console.log('Done generating PDF');
process.exit(0);
};
module.exports = {
list: [],
findAll: function(user, timespan) {
const now = Date.now();
const cutoff = now - (timespan * 1000 * 60);
console.log(user, cutoff);
let results = [];
if (!user) {
return;
} else {
var filelist = fs.readdirSync(baseFilePath);
var filtered = filelist.filter(file => file.startsWith(user) && file.endsWith('.json'));
var filteredWithData = filtered.map(file => this.buildFileInfo(file));
results = filteredWithData.filter(file => Date.parse(file.created) >= cutoff);
}
return results;
},
findById: function(user, id) {
if (!user) {
return;
} else {
var filelist = fs.readdirSync(baseFilePath);
var filtered = filelist.find(item => item.startsWith(user) && item.endsWith('.json'));
var filteredWithData = filtered.map(file => this.buildFileInfo(file));
var foundOne = filteredWithData.find(file => file.id === id);
return Promise.resolve(foundOne);
}
},
getFileHTML: async function(email, id) {
const outputFilename = `${email}-${id}`;
try {
const file = this.findById(email, id);
await run(file.content, outputFilename);
return outputFilename;
} catch (error) {
console.warn(error);
return null;
}
},
buildFileInfo: function (filename) {
const fileInfo = fs.statSync(baseFilePath + '/' + filename);
const file = JSON.parse(fs.readFileSync(baseFilePath + '/' + filename));
return {
name: filename,
id: file.responseId,
created: fileInfo.birthtime,
contents: file.json,
};
},
replyDialog: function(bot, message, options = {}) {
const results = this.findAll(options.user, options.time);
if (results.length) {
const selectResults = results.map(result => (
{
label: result.contents.Message.Subject.Data,
value: result.id,
}
));
var dialog = bot.createDialog(
'Emails I found:',
'get_email',
'View',
).addText('User Email', 'email', options.user)
.addSelect('Email Subject','subject','subject', selectResults);
bot.replyWithDialog(message, dialog.asObject());
} else {
bot.replyPrivateDelayed(message, {text: "S#!t, I couldn't find anything. Sorry!"});
}
},
buildFileResponse: function (fileInfo) {
var attachment = {
title: 'File Info',
title_link: 'Link:',
color: '#31D57C',
fields: [],
image_url: '',
};
const pdfOutput = this.getFileHTML
if (fileInfo.contents) {
attachment.image_url = path.join(__dirname, 'tmp', )
attachment.fields.push({
title: 'To:',
value: fileInfo.contents.Destination.ToAddresses.toString(),
});
attachment.fields.push({
title: 'Sent:',
value: moment(fileInfo.created).calendar(),
});
attachment.fields.push({
title: 'Subject:',
value: fileInfo.contents.Message.Subject.Data,
});
}
return attachment;
},
test: function() {
if (this.list.length === 0) {
fs.readFileSync('jokes.txt').toString().split('\n').forEach((line) => {
this.list.push(line);
});
}
return this.list[Math.floor(Math.random()*this.list.length)];
}
}