-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (131 loc) · 3.63 KB
/
index.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
143
144
145
146
147
const simpleParser = require("mailparser").simpleParser;
const cheerio = require("cheerio");
const AWS = require("aws-sdk");
const Twitter = require("twitter");
AWS.config.update({ region: "us-west-2" });
const s3 = new AWS.S3();
let $ = null;
const twitter = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
const amex = (jq) => {
const words = [];
let startIndex = null;
jq("b").each(function (i, elem) {
const text = jq(this).text();
if (text.indexOf("your Card") > -1) {
startIndex = i + 1;
}
words[i] = text;
});
if (startIndex !== null) {
const merchant = words[startIndex];
const amount = words[startIndex + 1].replace("*", "");
return `${merchant} — ${amount}`;
} else {
return null;
}
};
const citibank = (jq) => {
const merchant = jq("td")
.filter((i, el) => jq(el).text().startsWith("Merchant"))
.closest("table")
.closest("td")
.next("td")
.text()
.trim();
const amount = jq("span")
.filter((i, el) => jq(el).text().startsWith("Amount:"))
.text()
.replace("Amount: ", "")
.trim();
return `${merchant} — ${amount}`;
};
const square = ($, subject) => {
let merchant = null;
const matches = subject.match(/(?<=Receipt from).*/);
if (matches && matches[0]) {
merchant = matches[0].trim();
}
const amount = $("span")
.filter((i, el) => $(el).text() === "$")
.parent()
.text()
.trim();
return `${merchant} — ${amount}`;
};
const chase = (text) => {
let merchant = null;
const regex = /(A charge of)(.+)(at)(.+) (has)/;
const results = regex.exec(text);
if (results.length == 6) {
// we remove USD so that way non US currencies will render the type
const amount = results[2]
.replace(" (", "")
.replace(") ", "")
.replace(/USD/, "")
.trim();
merchant = results[4].trim();
return `${merchant} — ${amount}`;
} else {
console.log("Regex didn't match");
return null;
}
};
const main = async (message) => {
const { html, subject, text } = await simpleParser(message);
$ = cheerio.load(html);
let response = null;
if (subject.indexOf("Transaction Alert from Chase") > -1) {
console.log("Chase");
response = chase(text);
} else if (html !== false) {
if (html.indexOf("American Express") > -1) {
console.log("American Express");
response = amex($);
} else if (html.indexOf("Citibank") > -1) {
console.log("Citibank");
response = citibank($);
} else if (html.indexOf("Square") > -1) {
console.log("Square");
response = square($, subject);
} else {
console.log("Couldn't determine bank");
}
} else {
console.log("Couldn't find HTML in email. Can't parse.");
}
console.log(response);
if (response !== null) {
return new Promise((resolve) => {
twitter.post("statuses/update", { status: response }, (err, data) => {
if (err) {
console.error(err);
}
resolve(response);
});
});
}
return response;
};
exports.handler = async (event) => {
const messageId = event["Records"][0].ses.mail.messageId;
const params = {
Bucket: "transaction-emails",
Key: messageId,
};
console.log("Message ID", messageId);
return new Promise((resolve) => {
s3.getObject(params, async function (err, data) {
if (err) {
console.log(err, err.stack);
} else {
const res = await main(data.Body);
resolve(res);
}
});
});
};