-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
91 lines (82 loc) · 2.81 KB
/
index.mjs
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
import Mustache from 'mustache'
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses"
const sesClient = new SESClient({ region: "us-east-1" })
const createSendEmailCommand = (toAddress, fromAddress, subject, htmlBody, textBody) => {
return new SendEmailCommand({
Source: fromAddress,
Destination: {
ToAddresses: [toAddress]
},
Message: {
Subject: {
Charset: "UTF-8",
Data: subject
},
Body: {
Html: {
Charset: "UTF-8",
Data: htmlBody
},
Text: {
Charset: "UTF-8",
Data: textBody
}
}
}
})
}
export const handler = async (event) => {
const authParams = new URLSearchParams()
authParams.append("grant_type", "client_credentials")
authParams.append("client_id", process.env["SPOTIFY_CLIENT_ID"])
authParams.append("client_secret", process.env["SPOTIFY_CLIENT_SECRET"])
const authResponse = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: authParams
})
const authTokenInfo = await authResponse.json()
const playlistQueryParams = new URLSearchParams({
fields: "tracks.items(track(name,artists(name)))"
})
const playlistURL = `https://api.spotify.com/v1/playlists/${process.env["SPOTIFY_PLAYLIST_ID"]}?${playlistQueryParams}`
const playlistResponse = await fetch(playlistURL, {
headers: {
"Authorization": `Bearer ${authTokenInfo.access_token}`
}
})
const playlistInfo = await playlistResponse.json()
const tracks = playlistInfo.tracks.items.map(t => {
return {
name: t.track.name,
artist: t.track.artists.map(a => a.name).join(", ")
}
});
const htmlTemplate = `<p>Hola! Estas son las canciones que más has escuchado este mes.</p>
<p><strong>On Repeat this month:</strong>
<ol>
{{#tracks}}
<li>{{name}} - {{artist}}</li>
{{/tracks}}
</ol>
</p>
<p>Nos vemos</p>
<p>-- musicbot 🤖</p>`
const textTemplate = `Hola! Estas son las canciones que más has escuchado este mes.
On Repeat this month:
{{#tracks}}
- {{name}} - {{artist}}
{{/tracks}}
Nos vemos
-- musicbot *[º-º]*`
const sendEmailCommand = createSendEmailCommand(process.env["EMAIL_TO_ADDRESS"], process.env["EMAIL_FROM_ADDRESS"],
"this month in music", Mustache.render(htmlTemplate, {tracks}), Mustache.render(textTemplate, {tracks}))
try {
return await sesClient.send(sendEmailCommand)
} catch (e) {
console.error("Error sending email:", e)
return e
}
};