Skip to content

Commit d517c81

Browse files
committed
bump
1 parent 6978599 commit d517c81

File tree

6 files changed

+78
-30
lines changed

6 files changed

+78
-30
lines changed

functions/tweet-to-trello/package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/tweet-to-trello/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"front-matter": "^3.1.0",
1414
"node-fetch": "^2.3.0",
1515
"node-html-parser": "^1.2.12",
16+
"nodemailer": "^6.4.6",
1617
"sendmail": "^1.6.1",
1718
"tall": "^3.0.0"
1819
}

functions/tweet-to-trello/tweet-to-trello.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const {
77
isValidSecret,
88
createDataFromBodyText,
99
createDataFromJSON,
10-
notifyFailure
10+
notifyFailure,
11+
unshortenUrl
1112
} = require('./utils')
1213

1314
const raiseError = message => {
@@ -48,14 +49,27 @@ exports.handler = async (event, context) => {
4849
}
4950
}
5051

52+
// == expand url ==
53+
54+
let expandedUrl
55+
56+
try {
57+
expandedUrl = unshortenUrl(urlSource)
58+
} catch (error) {
59+
raiseError('ERR: tall failed. Anyway keep going.')
60+
expandedUrl = urlSource
61+
}
62+
63+
// == fetch formatted page text ==
64+
5165
let formattedPageText
5266

5367
try {
5468
// fetch target page's html as text
5569
const html = await fetchHtml(urlSource)
5670
formattedPageText = createFormattedTextFromHtml(html)
5771
} catch (err) {
58-
raiseError('ERR: fetching page failed')
72+
raiseError(`ERR: fetching page failed. ${urlSource}`)
5973
console.log(err)
6074
// something wrong
6175
return {
@@ -76,7 +90,7 @@ exports.handler = async (event, context) => {
7690

7791
// something wrong
7892
if (!response.ok) {
79-
raiseError('ERR: trello api says response.ok is false')
93+
raiseError(`ERR: trello api says response.ok is false ${urlSource}`)
8094
// NOT res.status >= 200 && res.status < 300
8195
const data = await response.json()
8296
console.log(data)
@@ -97,7 +111,7 @@ exports.handler = async (event, context) => {
97111
}
98112
} catch (err) {
99113
// something wrong
100-
raiseError('ERR: request failed on creating card')
114+
raiseError(`ERR: request failed on creating card ${urlSource}`)
101115
console.log(err.message)
102116
console.log(err)
103117
return {

functions/tweet-to-trello/utils.js

+48-26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const { URLSearchParams } = require('url')
44
const fetch = require('node-fetch')
55
const parseHtml = require('node-html-parser').parse
66
const frontMatter = require('front-matter')
7+
const tall = require('tall').tall
8+
const nodemailer = require('nodemailer');
79

810
const {
911
TRELLO_API_KEY: key,
@@ -13,6 +15,15 @@ const {
1315
TWEET_TO_TRELLO_SECRET: correctAppSecret
1416
} = process.env
1517

18+
module.exports.unshortenUrl = async url => {
19+
try {
20+
const unshortenUrl = await tall(url)
21+
console.log('Tall url', unshortenUrl)
22+
} catch(error) {
23+
console.log('GOT ERROR!', error)
24+
}
25+
}
26+
1627
module.exports.isValidSecret = appSecret => {
1728
if (appSecret !== correctAppSecret) return false
1829
return true
@@ -21,9 +32,9 @@ module.exports.isValidSecret = appSecret => {
2132
module.exports.createDataFromJSON = bodyContent => {
2233
const parsed = JSON.parse(bodyContent)
2334
return {
24-
tweetText: null,
25-
urlSource: parsed.url,
26-
appSecret: parsed.secret
35+
tweetText: null,
36+
urlSource: parsed.url,
37+
appSecret: parsed.secret
2738
}
2839
}
2940

@@ -37,9 +48,9 @@ ${a[1]}
3748
${a[2]}`
3849
const data = frontMatter(str)
3950
return {
40-
tweetText: data.body,
41-
urlSource: data.attributes.url,
42-
appSecret: data.attributes.secret
51+
tweetText: data.body,
52+
urlSource: data.attributes.url,
53+
appSecret: data.attributes.secret
4354
}
4455
}
4556

@@ -58,57 +69,68 @@ module.exports.createParams = ({ desc, urlSource, fromTweet, fromIos }) => {
5869
module.exports.fetchHtml = async url => {
5970
const response = await fetch(url)
6071
if (!response.ok) {
61-
return false
72+
return false
6273
}
6374
const data = await response.text()
6475
return data
6576
}
6677

6778
module.exports.createFormattedTextFromHtml = html => {
6879
const options = {
69-
script: false,
70-
style: false,
71-
pre: true,
72-
comment: false
80+
script: false,
81+
style: false,
82+
pre: true,
83+
comment: false
7384
}
7485
// parsed should be a formatted DOM element that node-html-parser generates
7586
const parsed = parseHtml(html, options)
7687
// we don't need many line breaks
7788
return parsed.text
78-
.replace(/\n\s+/g, '\n')
79-
.replace(/\n\n\n+/g, '\n\n')
89+
.replace(/\n\s+/g, '\n')
90+
.replace(/\n\n\n+/g, '\n\n')
8091
}
8192

8293
module.exports.combineText = (tweetText, pageText) => {
8394
let text
8495
if (tweetText) {
85-
text = `${tweetText}\n\n---\n\n${pageText}`
96+
text = `${tweetText}\n\n---\n\n${pageText}`
8697
} else {
87-
text = pageText
98+
text = pageText
8899
}
89100
return text.slice(0, 16384)
90101
}
91102

92103
module.exports.createTrelloCard = async params => {
93104
const response = await fetch('https://api.trello.com/1/cards', {
94-
method: 'post',
95-
headers: { Accept: 'application/json' },
96-
body: params
105+
method: 'post',
106+
headers: { Accept: 'application/json' },
107+
body: params
97108
})
98109
return response
99110
}
100111

101112
module.exports.notifyFailure = message => {
102-
const descriptor = {
103-
from: `"TRBKM" <${process.env.CONTACT_EMAIL}>`,
104-
to: process.env.CONTACT_EMAIL,
113+
const transporter = nodemailer.createTransport({
114+
host: 'smtp.gmail.com',
115+
port: 465,
116+
secure: true,
117+
auth: {
118+
user: process.env.GMAIL_SENDER_ADDRESS ,
119+
pass: process.env.GMAIL_SENDER_PASSWORD,
120+
}
121+
});
122+
123+
transporter.sendMail({
124+
from: `"TRBKM" <${process.env.GMAIL_SENDER_ADDRESS}>`,
125+
to: process.env.ERROR_REPORT_TO,
105126
subject: `[TRBKM] Failed ${message}`,
106127
text: message
107-
}
108-
sendMail(descriptor, e => {
109-
if (e) {
128+
}, function(error, info) {
129+
if (error) {
110130
console.log('ERR: mail sent falled')
111-
} else {
131+
console.log(error)
132+
} else {
133+
console.log('mail sent seems ok')
112134
}
113-
})
135+
});
114136
}

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"front-matter": "^3.1.0",
1414
"node-fetch": "^2.3.0",
1515
"node-html-parser": "^1.2.12",
16+
"nodemailer": "^6.4.6",
1617
"sendmail": "^1.6.1",
1718
"tall": "^3.0.0"
1819
}

0 commit comments

Comments
 (0)