@@ -4,6 +4,8 @@ const { URLSearchParams } = require('url')
4
4
const fetch = require ( 'node-fetch' )
5
5
const parseHtml = require ( 'node-html-parser' ) . parse
6
6
const frontMatter = require ( 'front-matter' )
7
+ const tall = require ( 'tall' ) . tall
8
+ const nodemailer = require ( 'nodemailer' ) ;
7
9
8
10
const {
9
11
TRELLO_API_KEY : key ,
@@ -13,6 +15,15 @@ const {
13
15
TWEET_TO_TRELLO_SECRET : correctAppSecret
14
16
} = process . env
15
17
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
+
16
27
module . exports . isValidSecret = appSecret => {
17
28
if ( appSecret !== correctAppSecret ) return false
18
29
return true
@@ -21,9 +32,9 @@ module.exports.isValidSecret = appSecret => {
21
32
module . exports . createDataFromJSON = bodyContent => {
22
33
const parsed = JSON . parse ( bodyContent )
23
34
return {
24
- tweetText : null ,
25
- urlSource : parsed . url ,
26
- appSecret : parsed . secret
35
+ tweetText : null ,
36
+ urlSource : parsed . url ,
37
+ appSecret : parsed . secret
27
38
}
28
39
}
29
40
@@ -37,9 +48,9 @@ ${a[1]}
37
48
${ a [ 2 ] } `
38
49
const data = frontMatter ( str )
39
50
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
43
54
}
44
55
}
45
56
@@ -58,57 +69,68 @@ module.exports.createParams = ({ desc, urlSource, fromTweet, fromIos }) => {
58
69
module . exports . fetchHtml = async url => {
59
70
const response = await fetch ( url )
60
71
if ( ! response . ok ) {
61
- return false
72
+ return false
62
73
}
63
74
const data = await response . text ( )
64
75
return data
65
76
}
66
77
67
78
module . exports . createFormattedTextFromHtml = html => {
68
79
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
73
84
}
74
85
// parsed should be a formatted DOM element that node-html-parser generates
75
86
const parsed = parseHtml ( html , options )
76
87
// we don't need many line breaks
77
88
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' )
80
91
}
81
92
82
93
module . exports . combineText = ( tweetText , pageText ) => {
83
94
let text
84
95
if ( tweetText ) {
85
- text = `${ tweetText } \n\n---\n\n${ pageText } `
96
+ text = `${ tweetText } \n\n---\n\n${ pageText } `
86
97
} else {
87
- text = pageText
98
+ text = pageText
88
99
}
89
100
return text . slice ( 0 , 16384 )
90
101
}
91
102
92
103
module . exports . createTrelloCard = async params => {
93
104
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
97
108
} )
98
109
return response
99
110
}
100
111
101
112
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 ,
105
126
subject : `[TRBKM] Failed ${ message } ` ,
106
127
text : message
107
- }
108
- sendMail ( descriptor , e => {
109
- if ( e ) {
128
+ } , function ( error , info ) {
129
+ if ( error ) {
110
130
console . log ( 'ERR: mail sent falled' )
111
- } else {
131
+ console . log ( error )
132
+ } else {
133
+ console . log ( 'mail sent seems ok' )
112
134
}
113
- } )
135
+ } ) ;
114
136
}
0 commit comments